简单实现飞机大战
主要功能:
1.显示开始界面
(1)点击“开始游戏”,运行游戏
(2)点击“结束游戏”,关闭游戏
2.运行游戏
(1)显示己方飞机
(2)遍历事件,实现按键移动飞机
(3)按空格键显示己方子弹
(4)显示敌方飞机
(5)使敌方飞机左右移动,用随机数使敌方飞机随机发射子弹
3.游戏结束
如果敌方子弹打到我方飞机则显示爆炸图片,并终止程序
4.运行展示
5.代码展示
import time
import random
import pygame
from pygame.locals import *
class Plane(object):
def __init__(self, screen):
self.screen = screen
self.image = pygame.image.load(r'D:\pics\红飞机.png')
self.x = 250
self.y = 597
self.bullet_list = []
self.hit = False
self.bomb_p = pygame.image.load(r'D:\pics\爆炸.png')
def display(self): # enemy
if self.hit:
#time.sleep(1)
self.screen.blit(self.bomb_p, (self.x, self.y))
time.sleep(1)
return True
else:
self.screen.blit(self.image, (self.x, self.y))
if self.x < -51:
self.x = -51
elif self.x > 589:
self.x = 589
elif self.y < 0:
self.y = 0
elif self.y > 649:
self.y = 649
needDelList = []
for i in self.bullet_list:
if i.judge():
needDelList.append(i)
for i in needDelList:
self.bullet_list.remove(i)
for bullet in self.bullet_list:
bullet.display()
bullet.move() # enemy
return False
def hang_move(self, move_x):
self.x += move_x
def lie_move(self, move_y):
self.y += move_y
def bomb(self):
self.hit = True
def sheBullet(self):
newBullet = Bullet(self.x, self.y, screen)
self.bullet_list.append(newBullet)
class Bullet(object):
def __init__(self, x, y, screen):
self.screen = screen
self.image = pygame.image.load(r'D:\pics\恒星子弹.png')
self.x = x + 39
self.y = y - 10
def display(self):
self.screen.blit(self.image, (self.x, self.y))
def move(self): # enemy
self.y -= 4
# if (enmey.y <= self.y and self.y <= enmey.y +150) and (enmey.x <= self.x and self.x <= enmey.x +114):
def judge(self):
if self.y < 0:
return True
else:
return False
class Enemy(object):
def __init__(self, screen):
self.screen = screen
self.x = random.randint(0, 550)
self.y = 0
self.image = pygame.image.load(r'D:\pics\敌机.png')
self.enemy_bullet = []
self.direction = "right"
# self.hit = False
def display(self, hero):
# if self.hit:
# pass
# else:
self.screen.blit(self.image, (self.x, self.y))
needDelList = []
for i in self.enemy_bullet:
if i.judge():
needDelList.append(i)
for i in needDelList:
self.enemy_bullet.remove(i)
for bullet in self.enemy_bullet:
bullet.display()
bullet.move(hero)
def move(self):
d1 = random.randint(0, 3)
p1 = random.randint(150, 200)
p2 = random.randint(-60, -40)
if self.direction == "right":
self.x += d1
elif self.direction == "left":
self.x -= d1
if self.x > 700 - p1:
self.direction = "left"
elif self.x < p2:
self.direction = "right"
# def bomb(self):
# self.hit = True
def sheBullet(self):
num = random.randint(0, 800)
newBullet = Enemy_bullet(self.x, self.y, self.screen)
if num < 10:
self.enemy_bullet.append(newBullet)
class Enemy_bullet(object):
def __init__(self, x, y, screen):
self.x = x + 60
self.y = y + 100
self.screen = screen
self.image = pygame.image.load(r'D:\pics\敌机子弹.png')
def display(self):
self.screen.blit(self.image, (self.x, self.y))
def move(self, hero):
self.y += 2
if (hero.y <= self.y and self.y <= hero.y + 103) and (hero.x <= self.x and self.x <= hero.x + 103):
hero.bomb()
def judge(self):
if self.y > 700:
return True
else:
return False
if __name__ == '__main__':
#clock = pygame.time.Clock()
move_x, move_y = 0, 0
screen = pygame.display.set_mode((640, 700), 0, 32)
background = pygame.image.load(r'D:\pics\飞机大战背景.jpg')
pygame.display.set_caption("飞机大战")
interface_background = pygame.image.load(r'D:\pics\初始界面.png')
start_image = pygame.image.load(r"D:\pics\开始游戏.png")
end_image = pygame.image.load(r'D:\pics\结束游戏.png')
restart_image = pygame.image.load(r'D:\pics\重新开始.png')
hero = Plane(screen)
enmey = Enemy(screen)
pygame.init()
n_start = True
while True:
while n_start:
#clock.tick(30)
screen.blit(interface_background, (0, 0))
screen.blit(start_image, (170, 350-91))
screen.blit(end_image, (170, 350+91))
buttons = pygame.mouse.get_pressed()
x, y = pygame.mouse.get_pos()
if 170 <= x <= 470 and 350-91<= y <= 350:
if buttons[0]:
n_start = False
elif 170 <= x <= 470 and 350+91 <= y <= 350+91+91:
if buttons[0]:
exit()
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
exit()
screen.blit(background, (0, 0))
n = hero.display() # enemy
enmey.move()
enmey.sheBullet()
enmey.display(hero)
for event in pygame.event.get():
if event.type == QUIT:
exit()
if event.type == KEYDOWN:
if event.key == K_a or event.key == K_LEFT:
move_x = -20
hero.hang_move(move_x)
elif event.key == K_d or event.key == K_RIGHT:
move_x = 20
hero.hang_move(move_x)
elif event.key == K_w or event.key == K_UP:
move_y = -20
hero.lie_move(move_y)
elif event.key == K_s or event.key == K_DOWN:
move_y = 20
hero.lie_move(move_y)
elif event.key == K_SPACE:
hero.sheBullet()
time.sleep(0.01)
pygame.display.update()
if n:
#time.sleep(1)
exit()
6.图片