2
votes

I am struggling to move the sprite correctly. Instead of smooth move I can see blur move and I do not know how to solve it. Is there any chance you can point what I do incorrectly ?

My target with it to drop the pizza so it hits the bottom and bounce back and bounc back if it hits the top and again the bottom -> bounce -> top -> bounce etc. etc.

import pygame

gravity = 0.5

class PizzaSprite:
    def __init__(self, image, spritepos):
        self.image = image
        self.spos = spritepos
        (x, y) = spritepos

        self.posx = x
        self.posy = y
        self.xvel = 1
        self.yvel = 1

        print "x ", x
        print "y ", y

    def draw(self, target_surface):          
        target_surface.blit(self.image, self.spos)

    def update(self):
        self.posy -= self.yvel
        self.spos = (self.posx, self.posy)
        return



def main():
    pygame.init()

    screen_width = 800
    screen_height = 600

    x = screen_width
    y = screen_height

    screen = pygame.display.set_mode((screen_width, screen_height))
    wall_image = pygame.image.load("wall.png")
    sky_image = pygame.image.load("sky.png")

    pizza_image = pygame.image.load("pizza.png")

    screen.blit(wall_image,(0,200))
    screen.blit(sky_image,(0,0))

    all_sprites = []
    pizza1 = PizzaSprite(pizza_image, (x/2, y/2))
    all_sprites.append(pizza1)

    while True:
        ev = pygame.event.poll()
        if ev.type == pygame.QUIT:
            break

        for sprite in all_sprites:
            sprite.update()

        for sprite in all_sprites:
            sprite.draw(screen)

        pygame.display.flip()
    pygame.quit()

main()
1
I understand what I do wrong. I just add a new image without removing the previous one. - Dariusz Krynicki
Ok, I understand what is the problem. The problem is that I blit wall_image and sky_image outside of "while True" loop. - Dariusz Krynicki
has anybody got any idea how to optimize it ? - Dariusz Krynicki

1 Answers

1
votes

in the beginning of your main game while loop add white = (255,255,255) screen.fill(white) let me give you a small analogy of what is happening now,

you have paper and a lot of pizza stickers with the intent to make a flip book. To make the illusion of movement on each piece of paper you place a sticker a little bit lower. The screen.fill command essentially clears the screen with the rgb color value you give it. When you dont fill the screen essentially what you are doing is trying to make that flipbook on one piece of paper. You just keep placing more and more stickers a little bit lower making a blur when what you want is one on each page.

and place

pygame.init()

screen_width = 800
screen_height = 600

x = screen_width
y = screen_height

screen = pygame.display.set_mode((screen_width, screen_height))
wall_image = pygame.image.load("wall.png")
sky_image = pygame.image.load("sky.png")

all outside of your main game loop assuming you wont be making changes to these variables ever in your program it is tedious and inefficient to redefine screen,,x,y,and your two images over and over again if they dont change.

so to sum it all up:

  1. use the screen.fill(white) command to reset the color of your screen

  2. You only need to import pngs and define variables once if they are never going to change and don't need them in your main loop

hope this helps clear things up.