Ⅰ 如何為pygame的飛機大戰增加一個分數排行榜python
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 Proctions - 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 Proctions - 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()
Ⅱ 怎麼用python學飛機大戰
1、我方飛機模塊
import pygame
class MyPlane(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image1 = pygame.image.load(r'E:\learn pygame\image\me1.png').convert_alpha()
self.image2 = pygame.image.load(r'E:\learn pygame\image\me2.png').convert_alpha()
self.image3 = pygame.image.load(r'E:\learn pygame\image\me_protect.png').convert_alpha()
self.destroy_images = []
self.destroy_images.extend([\
pygame.image.load(r'E:\learn pygame\image\me_destroy_1.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\me_destroy_2.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\me_destroy_3.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\me_destroy_4.png').convert_alpha() \
])
self.rect = self.image1.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.rect.left,self.rect.top = (self.width - self.rect.width) // 2,self.height - self.rect.height - 60
self.speed = 10
self.active = True
self.invincible = False
self.mask = pygame.mask.from_surface(self.image1)
def moveUp(self):
if self.rect.top > 0:
self.rect.top -= self.speed
else:
self.rect.top = 0
def moveDown(self):
if self.rect.bottom < self.height - 30:
self.rect.top += self.speed
else:
self.rect.bottom = self.height - 30
def moveLeft(self):
if self.rect.left > 0:
self.rect.left -= self.speed
else:
self.rect.left = 0
def moveRight(self):
if self.rect.right < self.width:
self.rect.left += self.speed
else:
self.rect.right = self.width
def reset(self):
self.rect.left,self.rect.top = (self.width - self.rect.width) // 2,self.height - self.rect.height - 30
self.active = True
self.invincible = True
2、敵機與背景動圖模塊
import pygame
from random import *
class SmallEnemy(pygame.sprite.Sprite):
energy = 1
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\enemy1.png').convert_alpha()
self.destroy_images = []
self.destroy_images.extend([\
pygame.image.load(r'E:\learn pygame\image\enemy1_down1.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy1_down2.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy1_down3.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy1_down4.png').convert_alpha() \
])
self.rect = self.image.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.speed = 2
self.active = True
self.rect.left,self.rect.top = \
randint(0,self.width - self.rect.width), \
randint(-5 * self.height, 0)
self.mask = pygame.mask.from_surface(self.image)
self.energy = SmallEnemy.energy
self.hit = False
def move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.active = True
self.energy = SmallEnemy.energy
self.rect.left,self.rect.top = \
randint(0,self.width - self.rect.width), \
randint(-5 * self.height, 0)
class MidEnemy(pygame.sprite.Sprite):
energy = 4