发布时间:2024-03-02 18:01
本文实例为大家分享了python实现飞机大战的具体代码,供大家参考,具体内容如下
游戏的实现本质是多个图片的快速切换,类似动画一样,达到动态的效果。
比如子弹的发射,实际上是一个子弹的照片根据列表中存储的位置多次粘贴到界面上。
还有飞机的移动是首先收集到移动信息将坐标变化,然后下一次覆盖页面的时候进行粘贴。
import pygame import time from pygame.locals import * hero_x = 150 hero_y = 600 # 子弹夹 mybullet = [] # 导弹夹 bomb_list = [] enemy_x = 0 enemy_y = 0 flag = 0 enemy_life = \"live\" hero_life = \"live\" # 飞机爆炸 a = pygame.image.load(\"./feiji/enemy1_down1.png\") b = pygame.image.load(\"./feiji/enemy1_down2.png\") c = pygame.image.load(\"./feiji/enemy1_down3.png\") d = pygame.image.load(\"./feiji/enemy1_down4.png\") e = pygame.image.load(\"./feiji/enemy1_hit.png\") a1 = pygame.image.load(\"./feiji/hero_blowup_n1.png\") b1 = pygame.image.load(\"./feiji/hero_blowup_n2.png\") c1 = pygame.image.load(\"./feiji/hero_blowup_n3.png\") d1 = pygame.image.load(\"./feiji/hero_blowup_n4.png\") hero_explode = [a1, b1, c1, d1] explode = [a, b, c, d, e] num = 0 hnum = 0 count = 0 def enemy_plant(screen, enemy, bomb): global enemy_x global enemy_y global flag global num global count global bomb_list global enemy_life if enemy_life == \"live\": if flag == 1 and enemy_x >= 0: enemy_x -= 20 else: flag = 0 if flag == 0 and enemy_x <= 320: enemy_x += 20 else: flag = 1 screen.blit(enemy, (enemy_x, enemy_y)) elif enemy_life == \"death\": screen.blit(explode[num], (enemy_x, enemy_y)) bomb_list.clear() num += 1 if num == 4: enemy_life = \"live\" num = 0 if count % 10 == 0: bomb_list.append({\"x\": enemy_x + 30, \"y\": enemy_y + 20}) count += 1 for b in bomb_list: screen.blit(bomb, (b[\"x\"], b[\"y\"])) b[\"y\"] += 10 def hero_plant(screen, hero, bullet): global hero_x global hero_y global enemy_life global hero_explode global enemy_x global enemy_y global hnum global bomb_list global hero_life for b in bomb_list: # 注意区间的取值 if b[\"x\"] <= hero_x + 30 and b[\"x\"] >= hero_x and b[\"y\"] >= hero_y and b[\"y\"] <= hero_y + 30: hero_life = \"death\" break if hero_life == \"death\": mybullet.clear() screen.blit(hero_explode[hnum], (hero_x, hero_y)) hnum += 1 if hnum == 4: hnum = 0 hero_life = \"live\" if hero_life == \"live\": screen.blit(hero, (hero_x, hero_y)) # 事件捕获,将捕获的事件放在列表中 # 快速运行然后接受命令造成连续性的画面,有的时候可能为空。 for event in pygame.event.get(): # 关闭游戏 if event.type == pygame.QUIT: # 退出游戏 exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT: hero_x += 10 elif event.key == pygame.K_LEFT: hero_x -= 10 elif event.key == pygame.K_DOWN: hero_y += 10 elif event.key == pygame.K_UP: hero_y -= 10 elif event.key == pygame.K_SPACE: print(\"发射子弹\") mybullet.append({\"x\": hero_x + 30, \"y\": hero_y - 20}) for i in mybullet: # 注意出界的子弹所以要大于0 if i[\"x\"] <= enemy_x + 20 and i[\"x\"] >= enemy_x and i[\"y\"] <= 20 and i[\"y\"] >= 0: enemy_life = \"death\" screen.blit(bullet, (i[\"x\"], i[\"y\"])) # 散弹模式 # screen.blit(bullet, (i[\"x\"]-20, i[\"y\"])) # screen.blit(bullet, (i[\"x\"]+20, i[\"y\"])) # 这样就可以自动控制上升和时间间隔了 i[\"y\"] -= 10 def main(): \'\'\'流程控制\'\'\' # 1 创建一个游戏窗口 # display方法:展示相关的都会用到这个方法 # 参数1:元组(长,高)像素 # 参数2:有无特殊功能 # 参数3:像素深度 screen = pygame.display.set_mode((400, 800), 0, 32) # 加载背景图片 background = pygame.image.load(\"./feiji/background.png\") # 加载飞机图片 hero = pygame.image.load(\"./feiji/hero1.png\") # 加载子弹照片 bullet = pygame.image.load(\"./feiji/plane.png\") # 加载导弹 bomb = pygame.image.load(\"./feiji/bomb-1.gif\") # 加载敌人飞机照片 enemy = pygame.image.load(\"./feiji/enemy1.png\") # 图片添加到屏幕 # blit剪切,粘贴 # screen 类似指针的使用带动目的地址的数据改动 while True: screen.blit(background, (0, 0)) # 显示英雄飞机 hero_plant(screen, hero, bullet) # 显示敌人飞机 enemy_plant(screen, enemy, bomb) # 数据更新加载出来 pygame.display.update() # 图片多显示一会 time.sleep(0.1) main()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。