Python中途岛海战街机1943模拟_A Simple Version of Midway Island Naval Warfare with Pthon for Easy Learning

以下是部分代码预览:

"""
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 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()

 

如需要查看完整代码,请扫码付款:

VIP免费查看

已有0人支付

本站所有作品,教程等皆为原创,版权所有。只供个人及单位内部研究使用,对外展示或传播必需经本站同意,且注明来自本站。培训机构等用本站资源培训学生,需经本站授权。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。亦可直接向微信号scratch8付款购买。入住QQ群:225792826 和爱好者共同交流,并且能下载免费提供的Python资源(需提供真实姓名才可入群)
李兴球的博客_Python创意编程技术前沿_pygame » Python中途岛海战街机1943模拟_A Simple Version of Midway Island Naval Warfare with Pthon for Easy Learning
scratch

学本领,探索更大的世界!

李兴球博客 风火轮编程主页