首页 > 解决方案 > pygame - 移动图形(演员)

问题描述

我只是在用 Pygame 做一个小游戏。对象应该在屏幕上移动。当我尝试这样做时,总是拖着一条“轨道”(见图)。如何在不绘制运动“路线”的情况下移动苹果?

from random import randint
import pygame

WIDTH   = 800
HEIGHT  = 800

apple = Actor("apple")
apple.pos = randint(0, 800), randint(800, 1600)

score = 0

def draw():
    apple.draw()
    screen.draw.text("Punkte: " + str(score), (700, 5), color = "white")

def update():
    if apple.y > 0:
        apple.y = apple.y - 4
    else: 
        apple.x = randint(0, 800)
        apple.y = randint(800, 1600)

在此处输入图像描述

标签: pythongraphicspygamepgzero

解决方案


这不是纯粹的 pygame,它是Pygame Zero。您必须调用screen.clear()以清除每一帧中的显示:

def draw():
    screen.clear()
    apple.draw()
    screen.draw.text("Punkte: " + str(score), (700, 5), color = "white")

推荐阅读