1. 程式人生 > >Python實現簡單遊戲:飛機大戰

Python實現簡單遊戲:飛機大戰

程式碼只寫到自己發射子彈和敵機自動發射子彈,未完待續…

裡面的飛機圖片檔案需要你們自己下載

import pygame
import time
from pygame.locals import *
import random


class plane(object):
    def __init__(self,x,y,screen,image):
        self.bullet_list = []
        self.x = x
        self.y = y
        self.screen = screen
        self.image = pygame.image.load(image)

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
        for bullet in self.bullet_list:
            #讓我方子彈顯示
            bullet.display()
            #子彈移動
            bullet.move()
            if bullet.judge():
                self.bullet_list.remove(bullet)

#飛機類
class Hero(plane):
    #顯示飛機
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
        for bullet in self.bullet_list:
            #讓我方子彈顯示
            bullet.display()
            #子彈移動
            bullet.move()
            if bullet.judge():
                self.bullet_list.remove(bullet)

    #移動飛機
    def move_left(self):
        self.x -= 10

    def move_right(self):
        self.x += 10

    def move_up(self):
        self.y -= 10

    def move_down(self):
        self.y += 10

    #發射子彈
    def fire(self):
        self.bullet_list.append(Bullet(self.screen,self.x,self.y))



#子彈類
class Bullet(object):
    def __init__(self,screen,x,y):
        self.x = x+40
        self.y = y-20
        self.screen = screen
        self.image = pygame.image.load('./feiji/bullet.png')

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))

    #我方飛機速度
    def move(self):
        self.y -= 10

    #我方飛機子彈越界刪除
    def judge(self):
        if self.y < 0:
            return True
        else:
            return False

#敵機類
class EnemyPlan(plane):
    def __init__(self,x,y,screen,image):
        self.direction = "right"
        plane.__init__(self,x,y,screen,image)


    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
        for bullet in self.bullet_list:
            #讓敵方子彈顯示
            bullet.display()
            #敵方子彈移動
            bullet.move()
            if bullet.judge():
                self.bullet_list.remove(bullet)

    #敵方飛機越界返回
    def move(self):
        self.y += 1
        if self.direction == "right":
            self.x += 5
        elif self.direction == "left":
            self.x -= 5

        if self.x > 340:
            self.direction = "left"
        elif self.x < 0:
            self.direction = "right"

    # 發射子彈
    def fire(self):
        if random.randint(1,50) == 30:
            self.bullet_list.append(Enemy_Bullet(self.screen, self.x, self.y))

#敵機子彈類
class Enemy_Bullet(object):
    def __init__(self,screen,x,y):
        self.x = x+20
        self.y = y+50
        self.screen = screen
        self.image = pygame.image.load('./feiji/bullet1.png')

    #顯示敵方子彈
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))

    #子彈移動速度
    def move(self):
        self.y += 10

    #邊界刪除子彈
    def judge(self):
        if self.y > 600:
            return True
        else:
            return False

#鍵盤控制
def key_contro(hero):
    for event in pygame.event.get():
        # 判斷是否是點選了退出按鈕
        if event.type == QUIT:
            print("exit")
            exit()
            # break
            # return ""
        # 判斷是否是按下了鍵
        elif event.type == KEYDOWN:
            # 檢測按鍵是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                if hero.x > 0:
                    print('left')
                    hero.move_left()
            # 檢測按鍵是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                if hero.x < 300:
                    print('right')
                    hero.move_right()
            elif event.key == K_w or event.key == K_UP:
                if hero.y > 0:
                    print('up')
                    hero.move_up()
            elif event.key == K_s or event.key == K_DOWN:
                if hero.y < 500:
                    print('down')
                    hero.move_down()
            elif event.key == K_SPACE:
                print('fire')
                hero.fire()

def main():
    # 1.建立視窗
    screen = pygame.display.set_mode((400,600),0,32)

    #建立背景圖片
    backgroup = pygame.image.load('./feiji/background.png')

    #建立飛機
    hero = Hero(150,470,screen,'./feiji/hero.gif')

    #建立敵方飛機
    enemy = EnemyPlan(0,0,screen,'./feiji/enemy0.png')

    while True:
        #設定背景圖片
        screen.blit(backgroup,(0,0))
        #顯示我方飛機
        hero.display()
        # 顯示敵方飛機
        enemy.display()
        # 敵方飛機移動
        enemy.move()
        # 敵方子彈發射
        enemy.fire()




        #鍵盤響應操作
        key_contro(hero)

        pygame.display.update()

        time.sleep(0.01)

if __name__ == '__main__':
    main()