python大法好——飛機大戰完整吧
阿新 • • 發佈:2018-08-11
rom type index Coding 記錄 說明 fir 檢測 als
# -*- coding:utf-8 -*-
import pygame
from pygame.locals import *
import time
‘‘‘
說明
1.按下b鍵,讓玩家飛機爆炸
2.爆炸效果的原理是:換圖片
‘‘‘
class Hero(object):
def __init__(self, screen_temp):
self.x = 210
self.y = 700
self.image = pygame.image.load("./feiji/hero1.png")
self.screen = screen_temp
self.bullet_list = []#用來存儲子彈對象的引用
#爆炸效果用的如下屬性
self.hit = False #表示是否要爆炸
self.bomb_list = [] #用來存儲爆炸時需要的圖片
self.__crate_images() #調用這個方法向bomb_list中添加圖片
self.image_num = 0#用來記錄while True的次數,當次數達到一定值時才顯示一張爆炸的圖,然後清空,,當這個次數再次達到時,再顯示下一個爆炸效果的圖片
self.image_index = 0#用來記錄當前要顯示的爆炸效果的圖片的序號
def __crate_images(self):self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n1.png"))
self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n2.png"))
self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n3.png"))
self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n4.png"))
def display(self):
"""顯示玩家的飛機"""
#如果被擊中,就顯示爆炸效果,否則顯示普通的飛機效果
if self.hit == True:
self.screen.blit(self.bomb_list[self.image_index], (self.x, self.y))
self.image_num+=1
if self.image_num == 7:
self.image_num=0
self.image_index+=1
if self.image_index>3:
time.sleep(1)
exit()#調用exit讓遊戲退出
#self.image_index = 0
else:
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 -= 8
def move_right(self):
self.x += 8
def fire(self):
"""通過創建一個子彈對象,完成發射子彈"""
print("-----1----")
bullet = Bullet(self.screen, self.x, self.y)#創建一個子彈對象
self.bullet_list.append(bullet)
def bomb(self):
self.hit = True
class Bullet(object):
def __init__(self, screen_temp, x_temp, y_temp):
self.x = x_temp+40
self.y = y_temp-20
self.image = pygame.image.load("./feiji/bullet.png")
self.screen = screen_temp
def display(self):
self.screen.blit(self.image, (self.x, self.y))
def move(self):
self.y -= 4
class EnemyPlane(object):
def __init__(self, screen_temp):
self.x = 0
self.y = 0
self.image = pygame.image.load("./feiji/enemy0.png")
self.screen = screen_temp
#self.bullet_list = []#用來存儲子彈對象的引用
self.direction = "right"#用來設置這個飛機默認的移動方向
def display(self):
"""顯示敵人的飛機"""
self.screen.blit(self.image,(self.x, self.y))
def move(self):
if self.direction == "right":
self.x+=2
elif self.direction == "left":
self.x-=2
if self.x>480-50:
self.direction="left"
elif self.x<0:
self.direction="right"
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()
elif event.key == K_b:
print(‘b‘)
hero_temp.bomb()
def main():
screen = pygame.display.set_mode((480,852),0,32)
background = pygame.image.load("./feiji/background.png")
#創建玩家飛機
hero = Hero(screen)
#創建敵機
enemy = EnemyPlane(screen)
while True:
screen.blit(background,(0,0))
hero.display()
enemy.display()
enemy.move()
pygame.display.update()
key_control(hero)
if __name__ == "__main__":
main()
python大法好——飛機大戰完整吧