移動控制

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個運(yùn)行單位移動一次
    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

···

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 8月22日-----字符串相關(guān) 2-3 個性化消息: 將用戶的姓名存到一個變量中,并向該用戶顯示一條消息。顯示的消...
    future_d180閱讀 1,042評論 0 1
  • 一、快捷鍵 ctr+b 執(zhí)行ctr+/ 單行注釋ctr+c ...
    o_8319閱讀 6,044評論 2 16
  • 看了那么就的簡書,終于有勇氣自己想自己寫點(diǎn)什么 女兒小名石頭源于我的姓氏,可以說女兒的到來確實改變了我的人生,3年...
    可0000閱讀 269評論 0 0
  • 2016年5月16日凌晨,時鐘繞了一圈恢復(fù)到了原點(diǎn),而生命的鐘擺卻戛然而止。 仍記得小時候...
    小月兔吖閱讀 363評論 3 5
  • 寫在前面:昨晚在微信公眾號發(fā)了這篇文章,很多人說看了很有感觸。卷毛維安給我留言道:“向前有路,未來可期(大四狗之間...
    眼鏡姑娘呀閱讀 3,144評論 160 98

友情鏈接更多精彩內(nèi)容