经过我为期两天完善,现在我正式宣布Space War1.1已经完成了
主要是因为1.0的游戏结束后会进入全屏静止的状态。有点突兀,所以进行了一点小改进,现在不管是游戏胜利还是游戏失败,活下的我机或敌机还是可以在屏幕里移动的,其实就改了一下主循环,上面的初始化,类和函数都是和1.0一样的。
下面把改动后的1.1的主循环给出,大家可以比对1.0的版本。飞机大战系列 Space War 1.0(开源)
#主循环
while True:
clock.tick(60) # 每秒不超过60帧的速度运行
for event in pygame.event.get():
if event.type == QUIT:
screen.fill((0, 204, 255)) # 用纯色填充窗口
text_pos = u"GOOD BYE"
show_text(screen, (700,500), text_pos, (255,0,0), True, 100)
pygame.display.update() #更新屏幕
time.sleep(1)
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c: # 按下 "C" 键关闭游戏
screen.fill((0, 204, 255)) # 用纯色填充窗口
text_pos = u"GOOD BYE"
show_text(screen, (700,500), text_pos, (255,0,0), True, 100)
pygame.display.update() #更新屏幕
time.sleep(1)
pygame.quit()
sys.exit()
if event.key == pygame.K_p:
paused = not paused # 按下 "P" 键时切换暂停状态
if (event.key == pygame.K_o and plane1.health == 0 or
event.key == pygame.K_o and len(enemy_out)==enemy_number): # 重新开始游戏
Enemy_list.clear() # 清空敌机列表
frame_count = 0 # 重置帧数计数器
enemy_out = [] # 重置剩余敌机数量
plane1 = Plane(screen,plane1.image1,800,600,50,300)
paused = False # 重置游戏结束标志
if not paused: # 只有在非暂停状态下才更新游戏逻辑和绘制
screen.fill((0, 204, 255)) # 用纯色填充窗口
#重新开始
if plane1.health == 0: #如果飞机生命值为 0
#显示文字
text_pos = u"You Out"
show_text(screen, (750,500), text_pos, (255,0,0), True, 100)
text_pos = u"You can press the 'O' to restart the game"
show_text(screen, (550,600), text_pos, (0,0,0), True, 50)
#pygame.display.update() #更新屏幕
#paused = not paused
if len(enemy_out)==enemy_number and plane1.health > 0: # 如果敌机全部消灭且生命值仍大于零
#显示文字
text_pos = u"Congratulation! You Win!"
show_text(screen, (450,500), text_pos, (255,0,0), True, 100)
text_pos = u"Your Score Is: %d"%(plane1.score)
show_text(screen, (600,600), text_pos, (255,0,0), True, 100)
text_pos = u"You can press the 'O' to renew your record"
show_text(screen, (550,700), text_pos, (0,0,0), True, 50)
#pygame.display.update() #更新屏幕
# 随机生成敌机
frame_count += 1 # 帧数递增
if (len(enemy_out)+len(Enemy_list)<enemy_number
and len(Enemy_list)<max_enemy_count
and frame_count % add_enemy_interval==0):
# 随机生成敌机的初始位置
x = random.choice([0, 1780]) # 在屏幕左右边缘随机选择
y = random.randint(0, 1180) # 在屏幕上随机选择纵向位置
# 加载敌机图像
enemy_image = pygame.image.load("enemy1.png")
# 创建敌机对象
enemy = Enemy(screen, enemy_image, x, y, 10)
# 将敌机对象添加到敌机列表
Enemy_list.append(enemy)
# 用键盘控制角色移动
keys = pygame.key.get_pressed() # 获取键盘状态
if plane1.health > 0:
plane1.move(keys) # 更新飞机对象
#用鼠标控制角色转动
mouse_x,mouse_y = pygame.mouse.get_pos()
if plane1.health > 0:
plane1.zhuandong()
#检测鼠标点击,发射子弹
mouse_left, mouse_middle, mouse_right = pygame.mouse.get_pressed()
if plane1.health > 0:
if mouse_left:
plane1.fire()
plane1.show_bullet(Enemy_list, enemy_out) #显示子弹
plane1.collision(Enemy_list, enemy_out)
#显示文字
text_pos = u"plane1 score: %d "%(plane1.score)
show_text(screen, (20,0), text_pos, (255,0,0), True, 40)
#显示飞机坐标
text_pos = u"center:(%d,%d)"%(plane1.rect.centerx, plane1.rect.centery)
show_text(screen, (20,25), text_pos, (0,0,0), True, 40)
#显示飞机角度
text_pos = u"current_angle:(%f)"%(plane1.current_angle)
show_text(screen, (20,50), text_pos, (0,0,0), True, 40)
# 绘制飞机的血条
text_pos = u"current_health:(%d)"%(plane1.health)
show_text(screen, (20,75), text_pos, (0,255,0), True, 40)
plane1.health_bar.draw((255,0,0), (0,255,0), plane1.health)
#显示弹夹容量
text_pos = u"bullet_clip: (%d)"%(plane1.bullets_in_clip)
show_text(screen, (20,100), text_pos, (255,255,0), True, 40)
plane1.update_reload() # 弹夹填充
plane1.bullet_clip_bar.draw((0,0,0), (255,255,0), plane1.bullets_in_clip)
# 显示剩余敌机数量
text_pos = u"remanent enemy number:(%d)"%(enemy_number-len(enemy_out))
show_text(screen, (1400,20), text_pos, (255,0,0), True, 40)
if plane1.health > 0 :
screen.blit(plane1.image, plane1.rect.topleft) # 绘制图像
# 更新敌机
for enemy in Enemy_list:
enemy.update()
pygame.display.update() #更新屏幕