美文网首页
移动控制

移动控制

作者: 匿隱 | 来源:发表于2019-02-14 11:45 被阅读0次

    import pygame
    from color import Color
    from random import randint
    ···
    window_width = 400
    window_height = 600

    class Direction:
    """方向类"""
    UP = 273
    DOWN = 274
    RIGHT = 275
    LEFT = 276

    class Ball:
    def init(self, center_x, center_y, radius, bg_color=Color.random_color()):
    self.center_x = center_x
    self.center_y = center_y
    self.radius = radius
    self.bg_color = bg_color
    self.is_move = True # 是否移动
    self.move_direction = Direction.DOWN
    self.speed = 5

    def disappear(self, window):
        """
        球从指定界面消失
        :param window:
        :return:
        """
        pygame.draw.circle(window, Color.white, (self.center_x, self.center_y), self.radius)
    
    def show(self, window):
        """
        小球显示
        :param window:
        :return:
        """
        pygame.draw.circle(window, self.bg_color, (self.center_x, self.center_y), self.radius)
    
    def move(self, window):
        """
        小球移动
        :param window:
        :return:
        """
        # 让移动前的球消失
        self.disappear(window)
        if self.move_direction == Direction.DOWN:
            self.center_y += self.speed
        elif self.move_direction == Direction.UP:
            self.center_y -= self.speed
        elif self.move_direction == Direction.LEFT:
            self.center_x -= self.speed
        else:
            self.center_x += self.speed
    
        # 移动后重新显示球
        self.show(window)
    
    @classmethod
    def creat_enemy_ball(cls):
        r = randint(10, 25)
        x = randint(r, int(window_width - r))
        y = randint(r, int(window_height - r))
        enemy = cls(x, y, r, Color.random_color())
        enemy.is_move = False
        return enemy
    

    def main():
    pygame.init()
    window = pygame.display.set_mode((window_width, window_height))
    pygame.display.set_caption('事件')
    window.fill(Color.white)

    # 先显示一个的球
    ball = Ball(100, 100, 30)
    ball.show(window)
    
    pygame.display.flip()
    # 计时
    time = 0
    # 所有被吃的球
    all_enemy = []
    while True:
        time += 1
    
        # 每隔100个运行单位移动一次
        if time % 100 == 0:
            if ball.is_move:
                # 让球动起来
                ball.move(window)
                pygame.display.update()
    
        if time == 10000:
            time = 0
            enemy = Ball.creat_enemy_ball()
            all_enemy.append(enemy)
            enemy.show(window)
    
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == Direction.DOWN or event.key == Direction.UP or event.key == Direction.LEFT or event.key == Direction.RIGHT:
                    # ball.is_move = True
                    ball.move_direction = event.key
            elif event.type == pygame.KEYUP:
                if event.key == Direction.DOWN or event.key == Direction.UP or event.key == Direction.LEFT or event.key == Direction.RIGHT:
                    # ball.is_move = False
                    pass
    

    ···

    相关文章

      网友评论

          本文标题:移动控制

          本文链接:https://www.haomeiwen.com/subject/dghljqtx.html