以下是部分代码预览:
"""
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__ = "https://www.lixingqiu.com/?p=209"
__qq__ = "406273900"
import os
import random
import arcade
SPRITE_SCALING = 1.0
SCREEN_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 = 5
class Explosion(arcade.Sprite):
""" 创建可爆炸的角色 create explosion sprite"""
def __init__(self, texture_list,x,y):
"""texture_list是已经load了的造型列表"""
super().__init__()
self.center_x = x
self.center_y = y
# 第一帧
self.current_texture = 0 # 这是造型索引号
self.textures = texture_list # 这是每帧图片列表
self.set_texture(self.current_texture)
def update(self):
# 更新每帧的图片,到了最后一帧后就会删除自己。
self.current_texture += 1
if 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_x
self.center_y = plane.center_y
self.change_y = 20
def update(self):
"""每帧更新坐标 update coordinates"""
self.center_x += self.change_x
self.center_y += self.change_y
if 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_x
self.center_y += self.change_y
if self.top <=0 : self.bottom = SCREEN_HEIGHT
class 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 = None
self.enemy_list = None
# 定义玩家的相关变量
self.score = 0
self.player_sprite = None
def start_new_game(self):
""" 设置与初始化游戏 """
sea_image = "images/midway/sea.png"
# Setting a Fixed Background 设置不动的背景 To cover up rolling background cracks
self.background = arcade.Sprite(sea_image)
self.background.center_x = SCREEN_WIDTH // 2
self.background.bottom = 0
# 设置滚动背景,两个最先画的角色往下移,移到一定坐标就到上面去
self.background1 = Background(sea_image)
self.background1.center_x = SCREEN_WIDTH // 2
self.background1.bottom = 0
self.all_sprites_list.append(self.background1)
self.background2 = Background(sea_image)
self.background2.center_x = SCREEN_WIDTH // 2
self.background2.bottom = SCREEN_HEIGHT
self.all_sprites_list.append(self.background2)
# 1943logo
self.logo_1943 = arcade.Sprite("images/midway/1943Logo2.png")
self.logo_1943.center_x = SCREEN_WIDTH // 2
self.logo_1943.center_y = SCREEN_HEIGHT // 2
self.interval = 60
self.interval_counter = 0
def on_key_release(self, key, modifiers):
"""鼠标松开事件 """
if self.player_sprite.health <=0 :return
if key == arcade.key.W or key == arcade.key.S:
self.player_sprite.change_y = 0
elif key == arcade.key.A or key == arcade.key.D:
self.player_sprite.change_x = 0
class Cover(arcade.Window):
"""封面类"""
def __init__(self, width, height, title):
super().__init__(width, height, title)
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()
如需要查看完整代码,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)


