1. 程式人生 > >用Python編寫飛機小遊戲演示程式碼

用Python編寫飛機小遊戲演示程式碼

跟著視訊打的,沒有做完整 plane_main檔案

import pygame
from plane_sprites import *
class PlaneGame(object):
    def __init__(self):
        #遊戲視窗
        self.screen=pygame.display.set_mode(SCREEN.size)
        self.clock=pygame.time.Clock()
        self.__creat_sprites()
        pygame.time.set_timer(CREAT_ENEMY_EVENT,1000)
        pygame.time.set_timer(HERO_FIRE,500)

    def __creat_sprites(self):
        bg1=Background()
        bg2=Background(True)
        self.bg_group=pygame.sprite.Group(bg1,bg2)
        self.enemy_group=pygame.sprite.Group()
        self.hero=Hero()
        self.hero_group=pygame.sprite.Group(self.hero)

    def start_game(self):
        while True:
            self.clock.tick(FPS)
            self.__event_handler()
            self.__check_collide()
            self.__update_sprites()
            pygame.display.update()

    def __event_handler(self):
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                PlaneGame.__game_over(self)
            elif event.type==CREAT_ENEMY_EVENT:
                enemy=Enemy()
                self.enemy_group.add(enemy)
            elif event.type==HERO_FIRE:
                self.hero.fire()
            # elif event.type==pygame.KEYDOWN and event.key==pygame.K_RIGHT:
            #     print("向右移動")
            # elif event.type==pygame.KEYDOWN and event.key==pygame.K_LEFT:
            #     print("向左移動")
        key_event=pygame.key.get_pressed()
        if key_event[pygame.K_RIGHT]:
            self.hero.speed=2
        elif key_event[pygame.K_LEFT]:
            self.hero.speed=-2
        else:
            self.hero.speed=0
    def __check_collide(self):
        pygame.sprite.groupcollide(self.enemy_group,self.hero.bullet_group,True,True)
        en=pygame.sprite.spritecollide(self.hero,self.enemy_group,True)
        if len(en)>0:
            self.hero.kill()
            PlaneGame.__game_over()
    def __update_sprites(self):
        self.bg_group.update()
        self.bg_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.bullet_group.update()
        self.hero.bullet_group.draw(self.screen)
    def __game_over(self):
        print("退出遊戲")
        pygame.quit()
        exit()






if __name__ == '__main__':
    print("遊戲開始。。。")
    game=PlaneGame()
    game.start_game()

plane_sprites檔案

import random
import pygame

SCREEN=pygame.Rect(0,0,480,700)
FPS=60
CREAT_ENEMY_EVENT=pygame.USEREVENT
HERO_FIRE=pygame.USEREVENT+1
class GameSprite(pygame.sprite.Sprite):
    """飛機大戰精靈組"""
    def __init__(self,image_name,speed=1):
        super().__init__()
        self.image=pygame.image.load(image_name)
        self.rect=self.image.get_rect()
        self.speed=speed
    def update(self):
        self.rect.y+=self.speed
class Background(GameSprite):
    def __init__(self,is_alt=False):
        super().__init__("./images/background.png")
        if is_alt:
            self.rect.y=-self.rect.height
    def update(self):
        super().update()
        if self.rect.y>SCREEN.height:
            self.rect.y=-self.rect.height
class Enemy(GameSprite):
    """敵機精靈"""
    def __init__(self):
        #建立敵機精靈
        super().__init__("./images/enemy1.png")
        #隨機敵機速度
        self.speed=random.randint(1,3)
        #隨機位置
        self.bottom=0
        max_a=SCREEN.width-self.rect.width
        self.rect.x=random.randint(0,max_a)
    def update(self):
        super().update()
        if self.rect.y>=SCREEN.height:
            #print("刪除")
            #kill方法可以將精靈從所有精靈組中移除
            self.kill()
    def __del__(self):
        #print("刪除%s" %self.rect)
        pass
class Hero(GameSprite):
    def __init__(self):
        super().__init__("./images/me1.png",0)
        self.rect.centerx=SCREEN.centerx
        self.rect.bottom=SCREEN.bottom-120
        self.bullet_group=pygame.sprite.Group()
    def update(self):
        self.rect.x+=self.speed
        if self.rect.x<0:
            self.rect.x=0
        elif self.rect.right>SCREEN.right:
            self.rect.right=SCREEN.right
    def fire(self):
        #建立精靈物件
        for i in (0,1,2):
            bullet=Bullet()
            #定義位置
            bullet.rect.bottom=self.rect.y-i*20
            bullet.rect.centerx=self.rect.centerx
            #追加進精靈組
            self.bullet_group.add(bullet)
class Bullet(GameSprite):
    def __init__(self):
        super().__init__("./images/bullet1.png",speed=-2)
    def update(self):
        super().update()
        if self.rect.bottom<0:
            self.kill()
    def __del__(self):
        print("刪除")