4

I'm just making a little game with Pygame. Objects should move across the screen. When I try to do this, a "track" is always dragged along (see picture). How can I move the apple without drawing the "course" of the movement?

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)

enter image description here

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
siclaro
  • 137
  • 13
  • Does this answer your question? [How to clear up screen in pygame?](https://stackoverflow.com/questions/21257865/how-to-clear-up-screen-in-pygame) – Uli Sotschok Nov 28 '19 at 16:46
  • @UliSotschok No, that doesn't answer the question, because this is [Pygame Zero](https://pygame-zero.readthedocs.io/en/stable/) and not [Pygame](https://www.pygame.org/news) only – Rabbid76 Nov 28 '19 at 17:02

3 Answers3

4

This is not pure pygame, it is Pygame Zero. You've to call screen.clear() to clear the display in every frame:

def draw():
    screen.clear()
    apple.draw()
    screen.draw.text("Punkte: " + str(score), (700, 5), color = "white")
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
4

Everytime you update, use pygame.display.flip(), this resets the screen. I would also consider using a while loop, that would handle user input, draw the sprite, and then wipe the screen, and the just end the loop when the game is over.

Cyborg
  • 41
  • 2
  • from the code he sent, it just says import pygame, which i have used with the command previously stated – Cyborg Nov 28 '19 at 17:05
1

What's happening is that the apple instead of being moved down is actually being redrawn many times over at the new coordinates. It seems that you are using an inbuilt class so idk what methods it has since I normally create my own class. What would fix it is if you had your apple object created before the main loop. Then in the main loop call a method to move the apple by how many pixels you want then update the position by using screen.blit()

For example, You could create a class for your apples, the class would take 4 parameters: which pygame window, x coordinate, y coordinate, and a path to the apple image.

class Apple():
    def __init__(self, place, x, y, path,):
        self.place = place
        self.x = x
        self.y = y
        self.path = path 


    def load(self):
        screen.blit(self.path, (self.x, self.y))


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

You would then create the apple object:

path = "path_to_the_image_of_the_apple"
apple_x = random.randint(0, 800)
apple_y = random.randint(0, 800)

apple = Apple(screen, apple_x, apple_y, path)

In the main loop then call a method to first move the apple, apple.move() then update the position apple.load()

Main loop:

#main game loop
while True:
    #clear display
    screen.fill(0)

    #move call the function to move the apple
    apple.move()


    #updating the player
    apple.load()

    #update display
    pygame.display.flip() 

Note that in screen.blit(self.path, (self.x, self.y)) screen is just my variable in my code. replace it with whatever yours is.

Eugene Levinson
  • 172
  • 2
  • 13