"""
A Simple Version of Midway Island Naval Warfare with Pthon for Easy Learning by lixingqiu
这个射击游戏是使用Arcade模块制作的。这个版本是核心版本,演示了爆炸效果,类的继承,按键检测等。
其中滚动的背景是使用两张图片制作的,它们相隔SCREEN_HEIGHT的高度。如果图片的top到了最底下,也就是坐标为0了,那么它就瞬间移到最顶上去。
要注意Arcade的坐标系统和数学中的坐标系统一样,在本游戏中是以左下角为原点,y轴正向上。在Pygame中则是左上角为原点,y轴正向下。
"""__author__ = "lixingqiu"
__date__ = "2019/3/13"
__url__ = "http://www.lixingqiu.com/?p=209"
__qq__ = "406273900"import requests
import imageio
from PIL  import Image
from coloradd import *
from pygame.locals import * import os
import random
import arcadeSPRITE_SCALING = 1.0SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "A Simple Version of Midway Island Naval Warfare with Pthon for Easy Learning(Python中途岛海战街机模拟)"
ENEMY_COUNT = 100
MOVEMENT_SPEED  = 5class Explosion(arcade.Sprite):""" 创建可爆炸的角色 create explosion sprite""" def __init__(self, texture_list,x,y):"""texture_list是已经load了的造型列表"""super().__init__()self.center_x = xself.center_y = y# 第一帧self.current_texture = 0      # 这是造型索引号self.textures = texture_list  # 这是每帧图片列表self.set_texture(self.current_texture)def update(self):# 更新每帧的图片,到了最后一帧后就会删除自己。self.current_texture += 1if self.current_texture < len(self.textures):self.set_texture(self.current_texture)else:self.kill()class Bullet(arcade.Sprite):"""子弹类,继承自Arcade自带的角色类,它从飞机的坐标射出"""def __init__(self,image,plane):super().__init__(image)self.center_x = plane.center_xself.center_y = plane.center_yself.change_y = 20        def update(self):"""每帧更新坐标 update coordinates"""self.center_x += self.change_xself.center_y += self.change_yif self.bottom >  SCREEN_HEIGHT: self.kill()class Background(arcade.Sprite):def __init__(self,image):super().__init__(image)    self.change_y = -10       def update(self):self.center_x += self.change_xself.center_y += self.change_yif self.top <=0 : self.bottom = SCREEN_HEIGHTclass Enemy(arcade.Sprite):def __init__(self,image):super().__init__(image)self.center_x = random.randrange(SCREEN_WIDTH)self.center_y = random.randrange(SCREEN_HEIGHT,SCREEN_HEIGHT*30)def update(self):if self.top<=0:self.kill()class MyGame(arcade.Window):""" 应用程序主要类. """def __init__(self, width, height, title):super().__init__(width, height, title)# 角色列表定义self.all_sprites_list = Noneself.enemy_list = None# 定义玩家的相关变量 self.score = 0self.player_sprite = Nonedef start_new_game(self):""" 设置与初始化游戏 """self.shoot_sound = arcade.sound.load_sound("images/midway/Shot.wav")        self.background_music = arcade.sound.load_sound("images/midway/background.wav")arcade.sound.play_sound(self.background_music)        # 实例化所有角色列表      self.enemy_list = arcade.SpriteList()            # 敌人列表        self.bullet_list = arcade.SpriteList()           # 玩家子弹列表self.all_sprites_list = arcade.SpriteList()      # 所有角色列表self.enemy_explosion_list = arcade.SpriteList()  # 所有敌人的爆炸角色列表self.enemy_explosion_images = []for i in range(15):# 加载从 explosion0000.png 到 explosion0054.png 的所有图片为敌人的爆炸效果动画帧            texture_name = f"images/enemy_explosion/explosion{i:04d}.png"self.enemy_explosion_images.append(arcade.load_texture(texture_name))             # 设置玩家操作的飞机self.score = 0self.player_sprite = arcade.AnimatedTimeSprite("images/midway/Plane 1.png",SPRITE_SCALING)self.player_sprite.textures.append(arcade.load_texture("images/midway/Plane 2.png", scale=SPRITE_SCALING))self.player_sprite.textures.append(arcade.load_texture("images/midway/Plane 3.png", scale=SPRITE_SCALING))          self.player_sprite.center_x = 50self.player_sprite.center_y = 70self.player_sprite.health  = 10             # 血条,在左上角画一根即可self.all_sprites_list.append(self.player_sprite)# 敌人角色生成for i in range(ENEMY_COUNT):enemy = Enemy("images/midway/Fighters.png")self.enemy_list.append(enemy)self.all_sprites_list.append(enemy)sea_image = "images/midway/sea.png"# Setting a Fixed Background 设置不动的背景 To cover up rolling background cracksself.background = arcade.Sprite(sea_image)self.background.center_x = SCREEN_WIDTH // 2self.background.bottom = 0# 设置滚动背景,两个最先画的角色往下移,移到一定坐标就到上面去self.background1 = Background(sea_image)self.background1.center_x = SCREEN_WIDTH // 2self.background1.bottom = 0self.all_sprites_list.append(self.background1)self.background2 = Background(sea_image)self.background2.center_x = SCREEN_WIDTH // 2self.background2.bottom = SCREEN_HEIGHT  self.all_sprites_list.append(self.background2)# 1943logoself.logo_1943 = arcade.Sprite("images/midway/1943Logo2.png")self.logo_1943.center_x  = SCREEN_WIDTH // 2self.logo_1943.center_y  = SCREEN_HEIGHT // 2self.interval = 60self.interval_counter = 0def on_key_press(self, key, modifiers):"""鼠标按键事件 """if self.player_sprite.health <=0 :returnif key == arcade.key.W:self.player_sprite.change_y = MOVEMENT_SPEEDelif key == arcade.key.S:self.player_sprite.change_y = -MOVEMENT_SPEEDelif key == arcade.key.A:self.player_sprite.change_x = -MOVEMENT_SPEEDelif key == arcade.key.D:self.player_sprite.change_x = MOVEMENT_SPEEDelif key == arcade.key.J:arcade.sound.play_sound(self.shoot_sound)self.bullet = Bullet("images/midway/Shot 1.png",self.player_sprite)self.bullet_list.append(self.bullet)self.all_sprites_list.append(self.bullet)            def on_key_release(self, key, modifiers):"""鼠标松开事件 """if self.player_sprite.health <=0 :returnif key == arcade.key.W or key == arcade.key.S:self.player_sprite.change_y = 0elif key == arcade.key.A or key == arcade.key.D:self.player_sprite.change_x = 0def update(self, delta_time):""" 游戏更新逻辑"""# 更新坐标等等#  print(self.background2.bottom - self.background1.top)distance = self.background2.bottom - self.background1.topif  distance < 0 and distance > -40:   # Fissure remedy 弥补裂隙问题self.background2.bottom = self.background1.topself.enemy_list.move(0, -5)self.all_sprites_list.update() self.player_sprite.update_animation()         if self.player_sprite.health > 0:# 玩家和所有敌机的碰撞检测.hit_list = arcade.check_for_collision_with_list(self.player_sprite,self.enemy_list)# 遍历碰到的敌机列表for enemy in hit_list:enemy.kill()self.player_sprite.health -= 1if self.player_sprite.health < 0 :Explosion(self.enemy_explosion_images,self.player_sprite.center_x,self.player_sprite.center_y)self.player_sprite.kill()e = Explosion(self.enemy_explosion_images,enemy.center_x,enemy.center_y)self.enemy_explosion_list.append(e)self.all_sprites_list.append(e)# 每个敌人有没有碰到子弹for enemy in self.enemy_list:   hit_list = arcade.check_for_collision_with_list(enemy,self.bullet_list)if len(hit_list) > 0:enemy.kill()[b.kill() for b in hit_list]   # 碰到的每颗子弹都删除self.score += len(hit_list)                e = Explosion(self.enemy_explosion_images,enemy.center_x,enemy.center_y)self.enemy_explosion_list.append(e)self.all_sprites_list.append(e)def on_draw(self):"""   渲染屏幕    """# 开始渲染,此命令要在所有重画命令之前arcade.start_render()self.background.draw()    # 画不动的背景,弥补滚动背景裂纹问题self.background1.draw()   # 画滚动背景self.background2.draw()   # 画滚动背景# 画所有的角色self.enemy_list.draw()if self.player_sprite.health > 0:            self.player_sprite.draw()self.bullet_list.draw()self.enemy_explosion_list.draw()# 画得分情况score = "score: " + str(self.score) + " , Blood volume: " + str(self.player_sprite.health)arcade.draw_text(score, 10, 20, arcade.color.WHITE, 14 )if self.player_sprite.health < 1:  # 血量小于1显示游戏结束self.interval_counter +=1if self.interval_counter < self.interval //2: self.logo_1943.draw()if self.interval_counter % self.interval == 0:self.interval_counter = 0class Cover(arcade.Window):"""封面类"""def __init__(self, width, height, title):super().__init__(width, height, title)def setup(self):self.background = arcade.Sprite("images/midway/Logo1.png")self.background.left = 50self.background.top = SCREEN_HEIGHTself.demo_plane = arcade.AnimatedTimeSprite("images/midway/Plane 1.png",0.8)self.demo_plane.textures.append(arcade.load_texture("images/midway/Plane 2.png",scale=0.8))self.demo_plane.textures.append(arcade.load_texture("images/midway/Plane 3.png",scale=0.8))self.demo_plane.center_x = 300self.demo_plane.center_y = 100self.begin_music = arcade.sound.load_sound("images/midway/start.wav")# arcade.sound.play_sound(self.begin_music)def on_key_press(self, key, modifiers):"""鼠标按键事件 """if key == arcade.key.SPACE: # 按空格键关闭窗口self.close()def update(self,delta_time):self.demo_plane.update()self.demo_plane.update_animation()def on_draw(self):"""        渲染屏幕        """# 开始渲染,此命令要在所有重画命令之前arcade.start_render()self.background.draw()self.demo_plane.draw()         def main():cover = Cover(800, 600, SCREEN_TITLE)cover.setup()arcade.run()window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)window.start_new_game()arcade.run()if __name__ == "__main__":main()

python中途岛海战相关推荐

  1. python中途岛海战boss发射子弹类设计程序

    """中途岛海战大boss发射的子弹类设计,游戏中最后的boss会不定时的发射一序列大颗粒子弹,这些子弹都会向下散射.""" __autho ...

  2. 1943中途岛海战2020年8月22日海龟画图版(原雷电模拟升级版)

    主程序源代码: """1943中途岛海战2020年8月22日.py这是重新编程的用纯粹的Python海龟模块制作的一个飞机大战游戏.老版本就叫雷电模拟.在游戏中用鼠标操作 ...

  3. Github配置(git+vscode+python+jupyter)

    ①下载git 打开 git bash 工具的用户名和密码存储 $ git config --global user.name "Your Name" $ git config -- ...

  4. 【实验楼】python简明教程

    ①终端输入python进入 欣赏完自己的杰作后,按 Ctrl + D 输入一个 EOF 字符来退出解释器,你也可以键入 exit() 来退出解释器. ②vim键盘快捷功能分布 ③这里需要注意如果程序中 ...

  5. 【Kaggle Learn】Python 5-8

    五. Booleans and Conditionals Using booleans for branching logic x = True print(x) print(type(x))''' ...

  6. 【Kaggle Learn】Python 1-4

    [Kaggle Learn]Python https://www.kaggle.com/learn/python 一. Hello, Python A quick introduction to Py ...

  7. 使用python愉快地做高数线代题目~

    今天接触到了python,发现真是极易上手啊!对比c语言是什么鬼东西= = 诶,等下,看完教学文章发现TA在下面写了这句话 如果做了前面的内容你可能已被吸引了,觉得c语言真的是废材! 不...不是的. ...

  8. python 位运算与等号_Python 运算符

    和大多数语言一样,Python也有很多运算符,并且运算符跟其他语言的运算符大同小异接下来一一介绍: 算术运算符: 运算符描述实例 +加 - 两个对象相加a+b的输出结果是30 -减 - 得到复数或者一 ...

  9. python减小内存占用_如何将Python内存占用缩小20倍?

    当程序执行过程中RAM中有大量对象处于活动状态时,可能会出现内存问题,特别是在对可用内存总量有限制的情况下. 下面概述了一些减小对象大小的方法,这些方法可以显著减少纯Python程序所需的RAM数量. ...

最新文章

  1. Elasticsearch创建雇员目录
  2. async / await对异步的处理
  3. dw自动滚动图片_3分钟搞定图片懒加载
  4. python 网络运维框架scape_“python scape 教程“求PhotoScape X Pro for Mac软件
  5. python语言训练教程_PYTHON零基础快乐学习之旅(K12实战训练)
  6. Laravel+passport 实现API认证
  7. 最硬核Visual AssistX 安装破解(2019最新 通用)内含破解原理
  8. cordova 安装ssl证书_超详细cordova环境配置(windows)及实例
  9. ubuntu install pip
  10. .NET面试宝典130道经典面试真题及答案
  11. GoLang之浅析unsafe.Pointer与uintptr
  12. 游戏服务器开发环境搭建
  13. 海外游戏广告应该怎么做
  14. java switch finally_java switch语句详解
  15. 堆内存和栈内存的区别(通俗版)
  16. 【白 女票】Fly.io 免费服务器
  17. MAC系统的绝佳看图工具iSmartPhoto_我是亲民_新浪博客
  18. 伊斯坦布尔的流浪 (一)
  19. uniapp安卓消息推送
  20. 计算机组装岗位练兵方案,电工组岗位练兵方案.doc

热门文章

  1. 整合基于MQL的EA交易和数据库 (SQL SERVER, .NET 和 C#)
  2. 【机器学习】线性回归——最小二乘法(理论+图解+公式推导)
  3. 门店管理系统怎么挑选?请收下这份避坑指南!
  4. 使用barrier将window和ubuntu共用键鼠的方法
  5. php中dump是什么,php中var_dump是什么意思?
  6. 这些很小的错误不应该在有了!
  7. Socket接收数据耗时
  8. javascript 知识库
  9. 关于卫星地球站(或地面站)的功率限值,你了解多少?
  10. QT·页面跳转,怎么切换到另一个界面(纯代码)