如何为pygame的飞机大战增加一个分数排行榜?python 210
本人为在读学生,老师作业为制作一个pygame的飞机大战游戏,玩家发射子弹每次击中一个敌人获得一分数,目前已经有单局总分显示模块,如何在此基础上增加一个分数排行版功能,实...
本人为在读学生,老师作业为制作一个pygame的飞机大战游戏,玩家发射子弹每次击中一个敌人获得一分数,目前已经有单局总分显示模块,如何在此基础上增加一个分数排行版功能,实现游玩多次将游戏的分数储存下来,并在游戏中展示成一个排行榜。 分数模块代码如下:
展开
1个回答
2020-07-18 · 百度知道官方认证企业
腾讯电脑管家
腾讯电脑管家是腾讯公司推出的免费安全管理软件,能有效预防和解决计算机上常见的安全风险,并帮助用户解决各种电脑“疑难杂症”、优化系统和网络环境,是中国综合能力最强、最稳定的安全软件。
向TA提问
关注
展开全部
import pygame
from plane_sprites import*
from Background import BackGround
from Enemy import Enemy
from Hero import Hero
from time import sleep
from Bullet import Bullet
class Plane_main(object):
def __init__(self):
print('游戏初始化')
self.screen = pygame.display.set_mode(SCREEN_RECT.size)
self.clock = pygame.time.Clock()
self.__create_sprites()
pygame.time.set_timer(CREATE_ENEMY_EVENT,1000)
pygame.time.set_timer(HERO_FIRE_EVENT, 500)
self.count = 0
self.score = 0
def __create_sprites(self):
# 背景组
self.back_group = pygame.sprite.Group()
bg1 = BackGround()
bg2 = BackGround(True)
pygame.mixer.init()
pygame.mixer.music.load("./Capo Productions - Journey 00_00_00-00_00_59.ogg")
pygame.mixer.music.play(-1)
self.back_group.add(bg1,bg2)
# 敌机组
self.enemy_group = pygame.sprite.Group()
#敌级销毁组
self.enemy1_down_group=pygame.sprite.Group()
# 英雄组
self.hero_group = pygame.sprite.Group()
self.hero = Hero()
self.hero_group.add(self.hero)
def start_game(self):
pygame.init()
print("开始游戏...")
# pygame.mixer.init()
# pygame.mixer.music.load("./Capo Productions - Journey 00_00_00-00_00_59.ogg")
# pygame.mixer.music.play(-1)
# gameIcon = pygame.image.load("./name.png")
# pygame.display.set_icon(gameIcon)
while True:
self.count+=1
# 1. 设置刷新帧率
self.clock.tick(10)
# 2. 事件监听
self.__event_handler()
# 3. 碰撞检测
self.__check_collide()
# 4. 更新精灵组
self.__update_sprites()
# 5. 更新屏幕显示
pygame.display.update()
def __event_handler(self):
"""事件监听"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
Plane_main.__game_over()
elif event.type == CREATE_ENEMY_EVENT:
self.enemy_group.add(Enemy())
elif event.type == HERO_FIRE_EVENT:
self.hero.fire()
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
print('向右移动')
self.hero.speed = 5
self.hero.speed1 = 0
elif keys[pygame.K_LEFT]:
print('向左移动')
self.hero.speed = -5
self.hero.speed1 = 0
elif keys[pygame.K_UP]:
print('向上移动')
self.hero.speed1 = -5
self.hero.speed = 0
elif keys[pygame.K_DOWN]:
print('向下移动')
self.hero.speed1= 5
self.hero.speed = 0
else:
self.hero.speed = 0
self.hero.speed1 = 0
if event.type == HERO_FIRE_EVENT:
self.hero.fire()
print('发射子弹!!!!!')
def __check_collide(self):
"""碰撞检测"""
# 1. 子弹摧毁敌机
enemy_down = pygame.sprite.groupcollide(self.enemy_group, self.hero.bullets,True, True)
enemy1_down_group.add(enemy_down)
# 2. 敌机撞毁英雄
enemies = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
# 判断列表时候有内容`
if len(enemies) > 0:
sleep(3)
# # 让英雄牺牲
self.hero.kill()
# 结束游戏
Plane_main.__game_over()
def __update_sprites(self):
self.back_group.update()
self.back_group.draw(self.screen)
self.enemy_group.update()
self.enemy_group.draw(self.screen)
self.hero_group.update()
self.hero_group.draw(self.screen)
self.hero.bullets.update()
self.hero.bullets.draw(self.screen)
self.drawText(str(self.score),SCREEN_RECT.width - 50,50)
for enemy1_down in enemy1_down_group:
self.screen.blit(enemy1_down_surface[enemy1_down.down_index],enemy1_down.rect)
if self.count % 5 ==0:
enemy1_down.down_index += 1
if enemy1_down.down_index ==3:
# pygame.mixer.music.load("./baozha.ogg")
# pygame.mixer.music.play()
self.score+=5
enemy1_down_group.remove(enemy1_down)
print(self.score)
#更新精灵组
# for group in [self.back_group, self.enemy_group, self.hero_group,self.hero.bullets]:
# group.update()
# group.draw(self.screen)
@staticmethod
def __game_over():
"""游戏结束"""
print("游戏结束")
pygame.quit()
exit()
def drawText(self,text,posx,posy,textHeight=48,fontColor=(0,0,0),backgroundColor=(111,225,1)):
fontObj = pygame.font.Font('freesansbold.ttf',textHeight)
textSurfaceObj = fontObj.render(text,13,fontColor,backgroundColor)
textRectObj = textSurfaceObj.get_rect()
textRectObj.center = (posx,posy)
self.screen.blit(textSurfaceObj,textRectObj)
#判断当前执行的文件是否是主文件
#如果不是 则if下的代码不会执行
if __name__ == '__main__':
game = Plane_main()
game.start_game()
from plane_sprites import*
from Background import BackGround
from Enemy import Enemy
from Hero import Hero
from time import sleep
from Bullet import Bullet
class Plane_main(object):
def __init__(self):
print('游戏初始化')
self.screen = pygame.display.set_mode(SCREEN_RECT.size)
self.clock = pygame.time.Clock()
self.__create_sprites()
pygame.time.set_timer(CREATE_ENEMY_EVENT,1000)
pygame.time.set_timer(HERO_FIRE_EVENT, 500)
self.count = 0
self.score = 0
def __create_sprites(self):
# 背景组
self.back_group = pygame.sprite.Group()
bg1 = BackGround()
bg2 = BackGround(True)
pygame.mixer.init()
pygame.mixer.music.load("./Capo Productions - Journey 00_00_00-00_00_59.ogg")
pygame.mixer.music.play(-1)
self.back_group.add(bg1,bg2)
# 敌机组
self.enemy_group = pygame.sprite.Group()
#敌级销毁组
self.enemy1_down_group=pygame.sprite.Group()
# 英雄组
self.hero_group = pygame.sprite.Group()
self.hero = Hero()
self.hero_group.add(self.hero)
def start_game(self):
pygame.init()
print("开始游戏...")
# pygame.mixer.init()
# pygame.mixer.music.load("./Capo Productions - Journey 00_00_00-00_00_59.ogg")
# pygame.mixer.music.play(-1)
# gameIcon = pygame.image.load("./name.png")
# pygame.display.set_icon(gameIcon)
while True:
self.count+=1
# 1. 设置刷新帧率
self.clock.tick(10)
# 2. 事件监听
self.__event_handler()
# 3. 碰撞检测
self.__check_collide()
# 4. 更新精灵组
self.__update_sprites()
# 5. 更新屏幕显示
pygame.display.update()
def __event_handler(self):
"""事件监听"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
Plane_main.__game_over()
elif event.type == CREATE_ENEMY_EVENT:
self.enemy_group.add(Enemy())
elif event.type == HERO_FIRE_EVENT:
self.hero.fire()
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
print('向右移动')
self.hero.speed = 5
self.hero.speed1 = 0
elif keys[pygame.K_LEFT]:
print('向左移动')
self.hero.speed = -5
self.hero.speed1 = 0
elif keys[pygame.K_UP]:
print('向上移动')
self.hero.speed1 = -5
self.hero.speed = 0
elif keys[pygame.K_DOWN]:
print('向下移动')
self.hero.speed1= 5
self.hero.speed = 0
else:
self.hero.speed = 0
self.hero.speed1 = 0
if event.type == HERO_FIRE_EVENT:
self.hero.fire()
print('发射子弹!!!!!')
def __check_collide(self):
"""碰撞检测"""
# 1. 子弹摧毁敌机
enemy_down = pygame.sprite.groupcollide(self.enemy_group, self.hero.bullets,True, True)
enemy1_down_group.add(enemy_down)
# 2. 敌机撞毁英雄
enemies = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
# 判断列表时候有内容`
if len(enemies) > 0:
sleep(3)
# # 让英雄牺牲
self.hero.kill()
# 结束游戏
Plane_main.__game_over()
def __update_sprites(self):
self.back_group.update()
self.back_group.draw(self.screen)
self.enemy_group.update()
self.enemy_group.draw(self.screen)
self.hero_group.update()
self.hero_group.draw(self.screen)
self.hero.bullets.update()
self.hero.bullets.draw(self.screen)
self.drawText(str(self.score),SCREEN_RECT.width - 50,50)
for enemy1_down in enemy1_down_group:
self.screen.blit(enemy1_down_surface[enemy1_down.down_index],enemy1_down.rect)
if self.count % 5 ==0:
enemy1_down.down_index += 1
if enemy1_down.down_index ==3:
# pygame.mixer.music.load("./baozha.ogg")
# pygame.mixer.music.play()
self.score+=5
enemy1_down_group.remove(enemy1_down)
print(self.score)
#更新精灵组
# for group in [self.back_group, self.enemy_group, self.hero_group,self.hero.bullets]:
# group.update()
# group.draw(self.screen)
@staticmethod
def __game_over():
"""游戏结束"""
print("游戏结束")
pygame.quit()
exit()
def drawText(self,text,posx,posy,textHeight=48,fontColor=(0,0,0),backgroundColor=(111,225,1)):
fontObj = pygame.font.Font('freesansbold.ttf',textHeight)
textSurfaceObj = fontObj.render(text,13,fontColor,backgroundColor)
textRectObj = textSurfaceObj.get_rect()
textRectObj.center = (posx,posy)
self.screen.blit(textSurfaceObj,textRectObj)
#判断当前执行的文件是否是主文件
#如果不是 则if下的代码不会执行
if __name__ == '__main__':
game = Plane_main()
game.start_game()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询