用Pygame实现的飞机大战小游戏,三条命,二十发初始子弹,非精灵版
import pygame
import random
import sys
import time
pygame.init()
# # bg : (480, 670)
# # bullet : (20, 40)
# # plane : (90, 74)
# # virus : (80, 81)
CREAT_VIRUS_EVENT = pygame.USEREVENT # # 用户自定义事件
bg = pygame.image.load("./images/bg.png")
bullet = pygame.image.load("./images/bullet.png")
plane = pygame.image.load("./images/plane.png")
virus = pygame.image.load("./images/virus.png")
screen = pygame.display.set_mode((480, 670))
pygame.display.set_caption("打飞机")
posplan = (195, 596)
screen.blit(bg, (0, 0))
screen.blit(plane, posplan)
pygame.display.update()
clock = pygame.time.Clock()
state = 'ready'
# # 飞机的初始位置
x = 195
y = 596
# # 病毒的初始y坐标
vy = 0
lstb = []
lstv = []
pygame.time.set_timer(CREAT_VIRUS_EVENT, 1000 )
count = 3
countscore = 0
bullet_num = 20
while True:
clock.tick(60)
screen.blit(bg, (0, 0))
screen.blit(plane, (x, y))
if state == 'ready':
flag = True
x = 195
y = 596
screen.blit(bg, (0, 0))
screen.blit(plane, (x, y))
pygame.display.update()
# # 子弹向前飞
for i in lstb:
i[1] -= 5
for i in lstb:
screen.blit(bullet, i)
if i[1] <= 0: # # lim
lstb.remove(i)
# # 病毒往下坠,病毒出界则游戏状态变为ready
for i in lstv:
i[1] += 1
for i in lstv:
screen.blit(virus, i)
if i[1] == bg.get_height():
print("病毒触底,英雄失败!")
flag = False
state = 'ready'
for j in lstv:
for i in lstb:
if j[0] <= i[0] <= j[0] + virus.get_width() and j[1] < i[1] < j[1] + virus.get_height():
print("子弹打到打到了")
countscore += 1
bullet_num += 2
print("得分+1,子弹+2")
lstb.remove(i)
lstv.remove(j)
# # 除了从天而降都可以碰撞
for i in lstv:
if i[0] <= x <= i[0]+virus.get_width() and i[1] <= y <= i[1]+virus.get_height():
print("飞机碰到病毒,失败了!")
flag = False
state = 'ready'
break
if x + plane.get_width() >= i[0] and i[1] <= y <= i[1]+virus.get_height():
print("飞机碰到病毒,失败了!")
flag = False
state = 'ready'
break
for event in pygame.event.get():
stepx = 0
stepy = 0
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
state = 'running'
if state == 'running':
# # 随机生成病毒
if event.type == CREAT_VIRUS_EVENT:
print("The Virus is coming")
lstv.append([random.randint(0, bg.get_width() - virus.get_width()), 0])
# # 控制子弹飞行
xb = x + plane.get_width()/2.6
yb = y - bullet.get_height()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bullet_num -= 1
if bullet_num <= 0:
print("子弹不够了")
else:
lstb.append([xb, yb])
print("当前剩余子弹数%d" % bullet_num)
# # 控制飞机移动
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if x > 0:
x -= 25
elif event.key == pygame.K_RIGHT:
if x + plane.get_width() < bg.get_width():
x += 25
elif event.key == pygame.K_UP:
if y >= 0:
y -= 25
elif event.key == pygame.K_DOWN:
if y + plane.get_height() < bg.get_height():
y += 25
if flag == False:
lstv.clear() # # 出现问题以后把病毒列表清空
lstb.clear()
count -= 1
if count == 0:
print("三条命玩完了小菜鸟!你的总得分是:%d" % countscore)
pygame.quit()
sys.exit()
print("你还剩%d条命,认真点搞!" % count)
print("当前得分:%d,加油哦! " % countscore)
print("游戏变为就绪态,请点击鼠标开始游戏")
pygame.display.update()
飞机可以从左右上碰,下碰未设置(想玩碰碰机的请移步他处)
三条命,二十发初始子弹,打中目标加两发
慢慢玩还没碰见Bug,要是碰上了自己调一下
作者也是为了玩hhhhhhh