python飞机大战

'''
新手刚学python,仿着老师敲的代码。
1、敌方飞机只能左右徘徊(不会往下跑)并且不会发射子弹。
2、正在研究怎么写计分。
3、也参考了不少大佬的代码,但也仅仅只是参考了。
加油!
'''


import pygame
from pygame.locals import *
import time

# 子弹类
class Bullet(object):
# 初始化子弹属性:xy坐标、窗口、子弹图片
def __init__(self, screen_temp, x, y):
self.x = x--18
self.y = y-15
self.screen = screen_temp
self.image = pygame.image.load("zd.png")
# 子弹显示
def display(self):
self.screen.blit(self.image, (self.x, self.y))
# 子弹移动,y -= 10
def move(self):
self.y -= 10
# 判断子弹是否越界
def judge(self):
if self.y < 0:
return True
else:
return False

# 玩家飞机类
class Aircraft_obj(object):
# 初始化玩家飞机属性:坐标、窗口、子弹列表对象
def __init__(self, screen_temp):
self.x = 190
self.y = 650
self.screen = screen_temp
self.image = pygame.image.load("FeiJi.jpeg")
self.bullet_list = []
# 显示玩家飞机图片,for循环显示发射子弹,如越界则删除子弹
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):
if self.x < 10:
pass
else:
self.x -= 10
def move_right(self):
if self.x > 480-100-10:
pass
else:
self.x += 10
def move_up(self):
if self.y < 10:
pass
else:
self.y -= 10
def move_down(self):
if self.y > 650:
pass
else:
self.y += 10
# 将子弹储存在发射列表中
def fire(self):
self.bullet_list.append(Bullet(self.screen, self.x, self.y))
# 敌机类
class EnemyPlane(object):
# 初始化敌机属性:坐标、窗口、图片、移动反向、是否爆炸、爆炸图片列表
def __init__(self, screen_temp, x, y):
self.x = x
self.y = y
self.screen = screen_temp
self.image = pygame.image.load("DiJi.jpg")
self.direction = 'right'
self.hit = False
self.bomb_lists = []
self.__crate_images()
self.image_num = 0
self.image_index = 0
# 敌机左右移动
def move(self):
if self.hit == True:
pass
else:
if self.direction == 'right':
self.x += 5
elif self.direction == 'left':
self.x -= 5
if self.x > 420-50:
self.direction = 'left'
elif self.x < 0:
self.direction = 'right'
# 添加爆炸图片列表
def __crate_images(self):
self.bomb_lists.append(pygame.image.load("baozha.png"))
self.bomb_lists.append(pygame.image.load("baozha.png"))
self.bomb_lists.append(pygame.image.load("baozha.png"))
self.bomb_lists.append(pygame.image.load("baozha.png"))
# 判断是否爆炸
def blast(self, x1, x2, y):
if((x1>=self.x and x2<=self.x+51) or x2==self.x or x1==self.x+51) and y<39:
self.hit = True
# 显示爆炸效果或正常图片,利用循环和sleep()暂停让用户看清
def display(self):
if self.hit == True:
self.screen.blit(self.bomb_lists[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:
# 暂停1秒
time.sleep(1)
exit()
else:
self.screen.blit(self.image, (self.x, self.y))
# 检查键盘输入函数
def key_control(aircraft_temp):
for event in pygame.event.get():
# 退出
if event.type == QUIT:
print("exit")
exit()
# 键盘按下事件
elif event.type == KEYDOWN:
# 按下a或←,调用玩家飞机向左移动方法
if event.key == K_a or event.key == K_LEFT:
print("left")
aircraft_temp.move_left()
# 按下d或→,,调用玩家飞机发射子弹方法
elif event.key == K_d or event.key == K_RIGHT:
print("right")
aircraft_temp.move_right()

elif event.key == K_w or event.key == K_UP:
print("up")
aircraft_temp.move_up()
# 按下d或→,,调用玩家飞机发射子弹方法
elif event.key == K_s or event.key == K_DOWN:
print("down")
aircraft_temp.move_down()

# 按下空格,调用玩家飞机发射子弹方法
elif event.key == K_SPACE:
print("space")
aircraft_temp.fire()
# 得分系统
def score():
pass

# 主函数
def main():
pygame.init()
pygame.display.set_caption("飞机大战")
screen = pygame.display.set_mode((420,700), 0, 32)
background = pygame.image.load('bj.png')

# 创建玩家飞机对象
aircraft = Aircraft_obj(screen)
enemy = EnemyPlane(screen,0,0)
enemy2 = EnemyPlane(screen,100,0)

# 游戏音效
sound = pygame.mixer.Sound("时光洪流.mp3")
sound.play()
pygame.mixer.music.load("时光洪流.mp3")
pygame.mixer.music.play(-1, 0.0)

while True:
screen.blit(background, (0, 0))
aircraft.display()
# 遍历子弹,找到坐标,判断是否与敌机相逢
for bullet in aircraft.bullet_list:
x1 = bullet.x
x2 = bullet.x+22
y1 = bullet.y
enemy.blast(x1, x2, y1)
enemy.display()
enemy2.display()
enemy.move()
enemy2.move()
pygame.display.update()
key_control(aircraft)
# 暂停0.01秒
time.sleep(0.01)
main()

python飞机大战
文章图片


【python飞机大战】



    推荐阅读