你如何在python中移动一个圆圈?

时间:2017-12-10 16:21:24

标签: python pygame

我正在尝试制作一个移动的圆圈,但我不知道如何让它工作。我使用pygame作为包。我认为如果你更新圈子它会起作用,但我不知道该怎么做。

import os, sys, math, pygame, pygame.mixer
from pygame.locals import *

black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
white = (255,255,255)
run_me = True

screen_size = screen_width, screen_height = 600, 400
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('ha ha ha ha ha')

clock = pygame.time.Clock()
fps_limit = 60

#circle 
    colorcircle = (red)
    posx = 300
    posy = 200
    circle = pygame.draw.circle(screen, colorcircle, (posx, posy), 50)

while run_me:
    clock.tick(fps_limit) 


    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run_me = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                posx = posx - 10
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                posx = posx + 10
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                posy = posy + 10
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN:
                posy = posy - 10

    pygame.display.flip()

pygame.quit()

我希望圈子根据输入移动到某处。

然而,圆圈什么也没做!

1 个答案:

答案 0 :(得分:3)

要移动圆圈,您必须每帧重绘一次。 为此,请将以下内容添加到while循环的行中(就在pygame.display.flip()之前)

# fill the screen with black (otherwise, the circle will leave a trail)
screen.fill(black)
# redraw the circle
pygame.draw.circle(screen, colorcircle, (posx, posy), 50)