Arcade外星飞船抓地球人到火星上

"""火星上并非没有高级智慧生命,这是由于他们都生活在火星地表下面。并且火星的科技也很发达了,他们派出一艘UFL来地球抓人。
在这个程序中,你用鼠标指针牵引UFO,单击就能放出吸收通道,只要地球人碰到了,就能被吸入。然后再让它们碰到火星,这样地球人就能被挪动到火星上去了。
程序用Arcade街机模块编写,最新安装版本请在命令提示符里输入 pip install arcade。
"""

import math
import random
import arcade

# 常量定义
SCREEN_WIDTH = 768                # 定义屏幕宽度
SCREEN_HEIGHT = 1024              # 定义屏幕高度
SCREEN_TITLE = "Arcade外星飞船抓地球人到火星_作者:李兴球"
SPRITE_PIXEL_SIZE = 64
SPRITE_SCALING = 1

class MyGame(arcade.Window):
    """
    继承自窗口类的游戏类,在具体的游戏中,重写以下方法,删除不需要重写的方法。
    """

    def __init__(self, width, height, title):
        super().__init__(width, height, title)

        arcade.set_background_color(arcade.color.AMAZON)

        self.npc_list = None
        self.dummy_list = None

    def setup(self):
        """ 这个方法是在实例化Mygame后对游戏进行一些设置。 """
        self.npc_amounts = 10
        self.mouse_x = SCREEN_WIDTH//2
        self.mouse_y = SCREEN_WIDTH//2
        self.begin_catch = False
        self.catcher = arcade.Sprite("抓子.png",0.5)
        self.ufo = arcade.Sprite("ufo.png",0.3)
        self.ufo.center_x = SCREEN_WIDTH//2
        self.ufo.center_y = SCREEN_HEIGHT//2        


        # 火星对象
        self.mars = arcade.Sprite("mars.png",0.5)
        self.mars.center_x = SCREEN_WIDTH - 200
        self.mars.center_y = SCREEN_HEIGHT - 300
        
        
        self.dummy_list = arcade.SpriteList()  # 放在火星上的木偶列表
        for i in range(self.npc_amounts):      # 生成 小人
            npc = arcade.Sprite("ren.png",random.choice([0.11,0.12,0.13,0.14,0.15]))
            npc.visible  = False               # 自定义属性,首先是不可见的(不会画)
            npc.angle = random.randint(1,360)  # 角度
            radius = self.mars.width //2       # 火星的半径
            radius += 20
            x = self.mars.center_x + radius * math.cos(math.radians(npc.angle+90)) 
            y = self.mars.center_y + radius * math.sin(math.radians(npc.angle+90))
            npc.center_y = y
            npc.center_x = x            
            self.dummy_list.append(npc)             
        
        self.npc_list = arcade.SpriteList()    # npc小人列表        
        
        for i in range(self.npc_amounts):       # 生成5个小人
            npc = arcade.Sprite("ren.png",random.choice([0.21,0.22,0.23,0.24,0.25]))
            npc.catched_flag = False           # 自定义属性,标识它被抓到了
            npc.shift_x = random.randint(-25,25) # 被抓到时放在抓子里的横向偏移量
            npc.bottom = SPRITE_PIXEL_SIZE
            npc.left =  SCREEN_WIDTH//2 + random.randint(-3,3) * SPRITE_PIXEL_SIZE
            npc.change_x = random.randint(-3,3)
            self.npc_list.append(npc)         

        # 下面是加载地图        
        my_map = arcade.read_tiled_map(f"level_0.tmx", SPRITE_SCALING)
         
          
        # 从墙生成地图列表
        self.wall_list = arcade.generate_sprites(my_map, 'ground', SPRITE_SCALING)
        self.wall_list.move(SPRITE_PIXEL_SIZE ,0)   # 水平方向和垂直方向移动


    def on_draw(self):
        """ 渲染屏幕  ,帧率为60左右,即每60份之一秒会自动调用此方法     """

        arcade.start_render()
        self.mars.draw()
        self.wall_list.draw()    
       
        self.npc_list.draw()
        for dummy in self.dummy_list:
            if dummy.visible : dummy.draw()

        self.ufo.draw()
        if self.begin_catch: self.catcher.draw()

        if len(self.npc_list)==0:
            x = SCREEN_WIDTH//2 -180
            y = SCREEN_HEIGHT//2 +100
            endstring  = "已经把所有地球人都抓到火星上去了"
            arcade.draw_text(endstring,x,y, arcade.color.BLUE,24,font_name="simhei")        

    def update(self, delta_time):
        """ 所有的角色移动等游戏逻辑都在这里编写代码   """        

        self.follow_mouse()
        
        self.ufo.update()
        self.npc_list.update()        

        if self.begin_catch:                 #  单击了鼠标它的值为True时
            # 检查抓捕器和小人之间的碰撞
            hits = arcade.check_for_collision_with_list(self.catcher,self.npc_list)
            if hits:                         # 抓到了的npc都停止移动,设定被抓到标志为True
                for npc in hits:
                    npc.change_x = 0
                    npc.change_y = 0
                    npc.catched_flag = True                    
        
        for npc in self.npc_list:             # 根据不同情况决定不同的移动方式            
            if npc.catched_flag  :            #  如果被抓到了,移到抓子里去
               npc.center_x = self.catcher.center_x  + npc.shift_x
               npc.bottom = self.catcher.bottom
               碰到火星 = arcade.check_for_collision(npc,self.mars) # 被抓到的小人和火星的碰撞
               if 碰到火星 :
                   npc.visible = True
                   npc.kill()
                   # 选择一个dummy,让它显示出来
                   for d in self.dummy_list:
                       if d.visible==False:
                           d.visible=True
                           break
            else:
                if npc.left <= SPRITE_PIXEL_SIZE or npc.right >= SCREEN_WIDTH-SPRITE_PIXEL_SIZE:
                   npc.change_x = -npc.change_x
                if npc.bottom <=  SPRITE_PIXEL_SIZE:
                    npc.change_y = 0
                    
               
    def follow_mouse(self):
        """让ufo跟随鼠标移动"""
        
        dx = self.ufo.center_x -  self.mouse_x
        dy = self.ufo.center_y -  self.mouse_y
        distance = math.sqrt(dx*dx + dy*dy)
        if distance < 300:        # 跟随效果                   
            self.ufo.change_x = -dx//10.0
            self.ufo.change_y = -dy//10.0

        else:
            self.ufo.change_x = 0
            self.ufo.change_y = 0        
            
        if self.begin_catch:        # 抓子跟着鼠标指针移动
               self.catcher.top = self.ufo.center_y
               self.catcher.center_x = self.ufo.center_x               
            

    def on_mouse_motion(self, x, y, delta_x, delta_y):        
        """ 当鼠标移动时会自动地调用此方法,仅记录下鼠标指针坐标 """

        self.mouse_x = x    # 为了传递给follow_mouse
        self.mouse_y = y            
                
    def on_mouse_press(self, x, y, button, key_modifiers):
        """ 当按鼠标键时会自动地调用此方法     """
        self.begin_catch = not  self.begin_catch
        if self.begin_catch:
           self.catcher.top = y
           self.catcher.center_x = x 

    def on_mouse_release(self, x, y, button, key_modifiers):
        """ 当松开鼠标键时会自动地调用此方法    """
        if not   self.begin_catch:    # 如果放开抓手
            for npc in self.npc_list:
                if npc.catched_flag == True:   # 如果这个是被抓到的
                    npc.change_y = -5          # 让它往下降
                    npc.change_x = ((2 * random.randint(0,1) ) - 1)* 5 # choice([-5,5])
                    npc.catched_flag = False        

def main():
    """ 主要的方法"""
    game = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
    game.setup()
    arcade.run()


if __name__ == "__main__":
    main()

Arcade外星飞船抓地球人到火星第一画,观望的地球人