不能让我的圈子在Python中移动

时间:2015-02-15 22:05:35

标签: python pygame

我试图通过在按下向上箭头键时更改其Y轴来移动一个简单的pygame绘制圆圈,但它不起作用,继承我的代码:

import pygame, sys
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode([500, 500])
circle = pygame.draw.circle(screen, [255,255,255],[100,100], 10, 0)
pygame.display.update()


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit(0)
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                circle.y = circle.y + 1
                pygame.display.flip()

希望你能帮忙!谢谢!

3 个答案:

答案 0 :(得分:2)

您忘了在新位置画圈。不要忘记删除旧的。

答案 1 :(得分:0)

工作版:

import pygame, sys
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode([500, 500])
circle = pygame.draw.circle(screen, [255,255,255],[100,100], 10, 0)
pygame.display.update()


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit(0)
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                eraser = pygame.draw.circle(screen, [0,0,0],[100,circle.y], 20, 0) #revision
                circle = pygame.draw.circle(screen, [255,255,255],[100,circle.y+1], 10, 0)
                pygame.display.flip()

修订版只是绘制了旧区域,然后在新位置绘制了一个新圆圈。

答案 2 :(得分:0)

你去:

import pygame,sys
from pygame.locals import *
from pygame.constants import K_UP, K_DOWN

pygame.init()
screen = pygame.display.set_mode([500,500])
white = (255,255,255)
black = (0,0,0)

class Circle(object):
    def __init__(self,posy):
        self.y = posy   
        def update(self):
        key=pygame.key.get_pressed()
        if key[pygame.K_UP]:
            self.y -= 1
        if key[pygame.K_DOWN]:
            self.y += 1

    def draw(self):
        circle = pygame.draw.circle(screen,white,(100,self.y),10,0)

def main():
    newcircle = Circle(100)
    while True:
        screen.fill(black)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        newcircle.update()
        newcircle.draw()
        pygame.display.update()
    pygame.quit()
main()