1. 程式人生 > >python 飛機大戰

python 飛機大戰

#圖片素材包在最後上傳

這個小專案使用pygame實現

python版本3.6   作業系統centos7

 

第一步首先要建立一個地圖

import pygame           

#建立一個window 
screen = pygame.display.set_mode((480,852),0,32)
                 
#為window選擇background
background = pygame.image.load("/root/Desktop/feiji/background.png")

#將背景插入window中
screen.blit(background, (0,0))

pygame.display.update()                                               

 

第二步建立一個自己的飛機 先建立他的類 然後定義飛機在哪塊出現。。。座標表示飛機左上角出現的位置

import pygame                                                                   
from pygame.locals import *
import time
               
class HeroPlane(object):
    def __init__(self,screen_temp):
        self.x = 210 
        self.y = 700 
        self.screen = screen_temp
        self.image = pygame.image.load("/root/Desktop/feiji/hero1.png")
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
        
def main():
    #建立一個window 
    screen = pygame.display.set_mode((480,852),0,32)
                     
    #為window選擇background
    background = pygame.image.load("/root/Desktop/feiji/background.png")
        
    #將背景插入window中
    screen.blit(background, (0,0))
     
    #建立飛機
    hero = HeroPlane(screen)
    hero.display()
    pygame.display.update()
    time.sleep(2)
     
     
if __name__ == "__main__":
    main()               

第三步 讓飛機左右移動 通過讀取鍵盤 左右鍵或者a b鍵來實現飛機圖片座標的變化

import pygame
from pygame.locals import *
import time

class HeroPlane(object):
    def __init__(self, screen_temp):
        self.x = 210
        self.y = 700
        self.screen = screen_temp
        self.image = pygame.image.load("./feiji/hero1.png")

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

    def move_left(self):
        self.x -= 5

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

def key_control(hero_temp):

    #獲取事件,比如按鍵等
    for event in pygame.event.get():

        #判斷是否是點選了退出按鈕
        if event.type == QUIT:
            print("exit")
            exit()
        #判斷是否是按下了鍵
        elif event.type == KEYDOWN:
            #檢測按鍵是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                hero_temp.move_left()
            #檢測按鍵是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                hero_temp.move_right()
            #檢測按鍵是否是空格鍵
            elif event.key == K_SPACE:
                print('space')

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

    #2. 建立一個背景圖片
    background = pygame.image.load("./feiji/background.png")

    #3. 建立一個飛機物件
    hero = HeroPlane(screen)

    while True:
        screen.blit(background, (0,0))
        hero.display()
        pygame.display.update()
        key_control(hero)
        time.sleep(0.01)

if __name__ == "__main__":
    main()

第四步 發射子彈

import pygame
from pygame.locals import *
import time

class HeroPlane(object):
    def __init__(self, screen_temp):
        self.x = 210
        self.y = 700
        self.screen = screen_temp
        self.image = pygame.image.load("./feiji/hero1.png")
        self.bullet_list = []#儲存發射出去的子彈物件引用

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))
        
        for bullet in self.bullet_list:
            bullet.display()
            bullet.move()

    def move_left(self):
        self.x -= 5

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

    def fire(self):
        self.bullet_list.append(Bullet(self.screen, self.x, self.y))

class Bullet(object):
    def __init__(self, screen_temp, x, y):
        self.x = x+40
        self.y = y-20
        self.screen = screen_temp
        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-=20

def key_control(hero_temp):

    #獲取事件,比如按鍵等
    for event in pygame.event.get():

        #判斷是否是點選了退出按鈕
        if event.type == QUIT:
            print("exit")
            exit()
        #判斷是否是按下了鍵
        elif event.type == KEYDOWN:
            #檢測按鍵是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                hero_temp.move_left()
            #檢測按鍵是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                hero_temp.move_right()
            #檢測按鍵是否是空格鍵
            elif event.key == K_SPACE:
                print('space')
                hero_temp.fire()

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

    #2. 建立一個背景圖片
    background = pygame.image.load("./feiji/background.png")

    #3. 建立一個飛機物件
    hero = HeroPlane(screen)

    while True:
        screen.blit(background, (0,0))
        hero.display()
        pygame.display.update()
        key_control(hero)
        time.sleep(0.01)

if __name__ == "__main__":
    main()

第五步 建立敵機

import pygame
from pygame.locals import *
import time
    
class HeroPlane(object):
    def __init__(self,screen_temp):
        self.x = 210
        self.y = 700
        self.screen = screen_temp
        self.image = pygame.image.load("/root/Desktop/feiji/hero1.png
        self.bulletlist =[]
    
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
        for bullet in self.bulletlist:
            bullet.display()
            bullet.move()
    
    def move_left(self):
        self.x -=5
    
    def move_right(self):
        self.x +=5
    
    def fire(self):                                                  
        self.bulletlist.append(Bullet(self.screen,self.x,self.y))
    
    
class EnemyPlane(object):
    def __init__(self,screen_temp):
        self.x = 0
        self.y = 0
        self.screen = screen_temp
       self.image = pygame.image.load("/root/Desktop/feiji/enemy0.pn
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
   
   
class Bullet(object):
    def __init__(self,screen_temp,x,y):
        self.x = x+40
        self.y = y-20
        self.screen = screen_temp
        self.image = pygame.image.load("/root/Desktop/feiji/bullet.pn
        
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
    
    def move(self):
        self.y -= 20
   
        
def key_control(hero_temp):
    for event in pygame.event.get():
        #判斷是否是點選了退出按鈕
        if event.type == QUIT:
            print("exit")
            exit()
        #判斷是否是按下了鍵                                          
        elif event.type == KEYDOWN:
            #檢測按鍵是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                hero_temp.move_left()
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                hero_temp.move_right()
            #檢測按鍵是否是空格鍵
            elif event.key == K_SPACE:
                hero_temp.fire()
                print('space')
    
    
def main():
    #creat window
    screen = pygame.display.set_mode((480,852),0,32)
    
    #creat backgroud
    background = pygame.image.load("/root/Desktop/feiji/background.pn
    #creat heroplan
    hero = HeroPlane(screen)
    enemy = EnemyPlane(screen)
    while True:
        screen.blit(background, (0,0))
        hero.display()
        enemy.display()                                              
        pygame.display.update()
        key_control(hero)
        time.sleep(0.01)
    
if __name__=="__main__":
    main()

第六步  讓敵機移動 (到盡頭後反方向移動)

import pygame
from pygame.locals import *
import time

class HeroPlane(object):
	def __init__(self,screen_temp):
		self.x = 210
		self.y = 700
		self.screen = screen_temp
		self.image = pygame.image.load("/root/Desktop/feiji/hero1.png")
		self.bulletlist =[]

	def display(self):
		self.screen.blit(self.image,(self.x,self.y))
		for bullet in self.bulletlist:
			bullet.display()
			bullet.move()

	def move_left(self):
		self.x -=10

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

	def fire(self):
		self.bulletlist.append(Bullet(self.screen,self.x,self.y))


class EnemyPlane(object):
	def __init__(self,screen_temp):
		self.x = 0
		self.y = 0
		self.screen = screen_temp
		self.image = pygame.image.load("/root/Desktop/feiji/enemy0.png")
		self.direction = 'right'

	def display(self):
		self.screen.blit(self.image,(self.x,self.y))
	
	def move(self):
		if self.direction == "right":
			self.x += 5 
		elif self.direction == "left":
			self.x -= 5
		if self.x > 480-50:
			self.direction = "left"
		elif self.x < 0:
			self.direction = "right"

class Bullet(object):
	def __init__(self,screen_temp,x,y):
		self.x = x+40
		self.y = y-20
		self.screen = screen_temp
		self.image = pygame.image.load("/root/Desktop/feiji/bullet.png")
		
	def display(self):
		self.screen.blit(self.image,(self.x,self.y))
	
	def move(self):
		self.y -= 20

		
def key_control(hero_temp):
	for event in pygame.event.get():
        #判斷是否是點選了退出按鈕
		if event.type == QUIT:
			print("exit")
			exit()
        #判斷是否是按下了鍵
		elif event.type == KEYDOWN:
            #檢測按鍵是否是a或者left
			if event.key == K_a or event.key == K_LEFT:
				print('left')
				hero_temp.move_left()
            #檢測按鍵是否是d或者right
			elif event.key == K_d or event.key == K_RIGHT:
				print('right')
				hero_temp.move_right()
            #檢測按鍵是否是空格鍵
			elif event.key == K_SPACE:
				hero_temp.fire()
				print('space')

	
def main():
	#creat window
	screen = pygame.display.set_mode((480,852),0,32)
    
	#creat backgroud
	background = pygame.image.load("/root/Desktop/feiji/background.png")
	#creat heroplan
	hero = HeroPlane(screen)
	enemy = EnemyPlane(screen)
	while True:
		screen.blit(background, (0,0))
		hero.display()
		enemy.display()
		enemy.move()
		pygame.display.update()
		key_control(hero)
		time.sleep(0.01)

if __name__=="__main__":
	main()