English 中文(简体)
气球的参与者在移动时不断利用屏幕。
原标题:pygame player keeps drawing itself on screen when moving

I am making a game in pygame and I copied and pasted my Player class from my previous game but when the player moves it leaves a trail behind but it didn t do that in my previous game where I copied and pasted from. This is the player class:

class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
    pygame.sprite.Sprite.__init__(self)
    self.speed = 7
    self.frames = []
    self.index = 0
    for num in range(1, 4):
        img = pygame.image.load(f hunter/img/player/player{num}.png )
        img.convert()
        img = pygame.transform.scale(img, (75, 75))
        self.frames.append(img)
    self.player = self.frames[self.index]
    self.rect = self.player.get_rect()
    self.rect.center = (x, y)
def update(self):
    # movement
    dx = 0
    dy = 0

    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:
        dy -= self.speed
    if keys[pygame.K_s]:
        dy += self.speed
    if keys[pygame.K_a]:
        dx -= self.speed
    if keys[pygame.K_d]:
        dx += self.speed

    self.rect.x += dx
    self.rect.y += dy
    # collison check
    if (self.rect.left) + 24 < 0:
        self.rect.left = -24
    if (self.rect.right) - 24 > 1024:
        self.rect.right = 1024 + 24
    if (self.rect.top) + 24 < 0:
        self.rect.top = -24
    if (self.rect.bottom) - 24 > 512:
        self.rect.bottom = 512 + 24

    # shoot
        
    display.blit(self.player, self.rect)

这是主要的游戏:

player = Player(200, 200)
world = Tile(getTilemap( map1.txt ))


while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    player.update()
    world.draw()

    pygame.display.flip()
    CLOCK.tick(60)
pygame.quit()

before I press d on my keyboard before

after I press d after

问题回答

法典 这是一个共同的问题,我认为还有其他员额。 尽管如此,我将帮助你。

在提供每个框架时,Pyega没有说明以前的情况。 相反,它只是树立了形象。 在此情况下,我认为你发挥了你的作用,并 。

例如,你从黑色屏幕开始,然后吸引了参与者和马奇。 然后,你带上了最新的参与者地点和同样的马奇。 因此,由于黑色屏幕被击退,我们也可以看到以前的框架。

解决办法? 一种简单的<代码>屏幕(0,0)应当做到。 your。

Happy coding!





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签