添加开始和结束界面:
添加了开始和结束界面的代码示例:
from random import randint
import sys
import pygame
'''
Pygame 是一个专门用来开发游戏的 Python 模块,主要为开发、设计 2D 电子游戏而生,
它是一个免费、开源的第三方软件包,支持多种操作系统,具有良好的跨平台性(比如 Windows、Linux、Mac 等)。
Pygame 是 Pete Shinners 在 SDL(Simple DirectMedia Layer,一套开源的跨平台多媒体开发库)
基础上开发而来,其目的是取代 PySDL
安装
pip install pygame
'''
pygame.init()
width,height = 1000,500
screen = pygame.display.set_mode([width,height])
pygame.display.set_caption('学习小游戏')
#创建精灵
class Gold(pygame.sprite.Sprite):
def __init__(self,image,pos):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image) #加载精灵图片
self.image = pygame.transform.scale(self.image,(20,20)).convert() #改变图片大小
self.rect = self.image.get_rect() #获取图片矩形
self.rect.topleft = pos #设置图片位置
def update(self,speed):
self.speed = speed
self.rect.top += self.speed #向下移动 top一直增加
if self.rect.top >= height:
self.kill() # 超过了屏幕 删除
gold_img = r'H:\temp\sz.jpg'
hero_img = r'H:\temp\dw.jpg'
#设置字体
my_font = pygame.font.SysFont(['方正粗黑宋简体','microsoftsansserif'],20)
#创建一个精灵 放到底部
class Hero(pygame.sprite.Sprite):
def __init__(self,image,pos):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image) #加载精灵图片
self.image = pygame.transform.scale(self.image,(40,40)).convert() #改变图片大小
self.rect = self.image.get_rect() #获取图片矩形
self.rect.center = pos #设置图片位置
def move(self,speed,where):
self.speed = speed
if where == 'right' and self.rect.left <= (width - 40):
self.image = pygame.image.load(hero_img) #加载精灵图片
self.image = pygame.transform.scale(self.image,(40,40)).convert() #改变图片大小
self.rect.left += self.speed #向右移动 left一直增加
elif self.rect.left >= 0 and where == 'left':
self.image = pygame.image.load(gold_img) #加载精灵图片
self.image = pygame.transform.scale(self.image,(40,40)).convert() #改变图片大小
self.rect.left -= self.speed #向左移动 left一直减少
def draw(self,screen):
screen.blit(self.image,self.rect)
def startInterface():
clock = pygame.time.Clock()
while True:
screen.fill((0,0,255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
start_text = my_font.render('please press space key to start', True, (0, 0, 0))
start_rect = start_text.get_rect()
start_rect.center = (width/2,height/2)
screen.blit(start_text, start_rect)
if pygame.key.get_pressed()[pygame.K_SPACE]:
print('keydown')
return
clock.tick(10)
pygame.display.flip()
def endInterface(score):
clock = pygame.time.Clock()
while True:
screen.fill((0,255,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
score_text = my_font.render('your final score is :{}'.format(score), True, (0, 0, 0))
score_rect = score_text.get_rect()
score_rect.center = (width/2 - score_rect.height,height/2- score_rect.height)
screen.blit(score_text, score_rect)
start_text = my_font.render('press esc to quit; press enter to continue', True, (0, 0, 0))
start_rect = start_text.get_rect()
start_rect.center = (width/2,height/2)
screen.blit(start_text, start_rect)
if pygame.key.get_pressed()[pygame.K_RETURN]:
return
if pygame.key.get_pressed()[pygame.K_ESCAPE]:
pygame.quit()
sys.exit()
clock.tick(10)
pygame.display.flip()
def run():
#开始界面
startInterface()
before_start = pygame.time.get_ticks()
#加载背景音乐
pygame.mixer.init()
pygame.mixer.music.load(r'H:\temp\bg.mp3')
pygame.mixer.music.play(-1)
#加载音效
music_dd = pygame.mixer.Sound(r'H:\temp\dd.mp3')
#初始位置居中显示
hero = Hero(hero_img,(width/2,height-22))
#创建精灵组
group = pygame.sprite.Group()
#控制一下精灵出现的数量和频率
gold_count = 0
gold_frep = randint(10,20) #10到20帧出现一个
score = 0 #初始得分为0
#创建时钟
clock = pygame.time.Clock()
while True:
#填充屏幕为白色
screen.fill((255,255,255))
clock.tick(40) #每秒刷新60帧
# --倒计时信息
countdown_text = 'count down: ' + str((90000+before_start - pygame.time.get_ticks()) // 60000) + ":" + str((90000+before_start - pygame.time.get_ticks()) // 1000 % 60).zfill(2)
countdown_text = my_font.render(countdown_text, True, (0, 0, 0))
countdown_rect = countdown_text.get_rect()
countdown_rect.topright = [950, 5]
screen.blit(countdown_text, countdown_rect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]:
hero.move(5,'left')
if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]:
hero.move(5,'right')
gold_count += 1
if gold_count > gold_frep:
gold_frep = randint(10,20) #10到20帧出现一个
#添加精灵到精灵组
gold = Gold(gold_img,(randint(0,width),0))
group.add(gold)
gold_count = 0 #恢复0
#碰撞检测
for gold_of_group in group:
if pygame.sprite.collide_mask(hero,gold_of_group): #判断是否碰撞
group.remove(gold_of_group) #移除出组
music_dd.play() #播放音效
score+=1 #加分
#将分数显示到窗口中
score_text = 'score:{}'.format(score)
score_text = my_font.render(score_text,True,(0,0,0))
score_rect = score_text.get_rect()
score_rect.topleft = [5,5]
screen.blit(score_text,score_rect)
hero.draw(screen)
group.update(5) #更新位置
group.draw(screen) #放到屏幕上
# --判断游戏是否结束
if pygame.time.get_ticks() >= 90000 + before_start:
pygame.mixer.music.stop() #停止音乐
return score
pygame.display.update()
if __name__ == '__main__':
while True:
score = run()
endInterface(score)