Search code examples
pythondebuggingpygamelivewires

Debugging simple Ping-Pong game?


I've been programming a simple ping-pong game using Python's Pygame as run through Livewires. I've posted the question and a similar sample game code that runs perfectly as not everyone here will be versed in livewires and/or pygame.

Here's the code that has bugs on it. What happens is the window opens, and the "Ball" object works well and falls of the screen (as it should, it's incomplete), and the "Slab" object is gets stuck to wherever the mouse initially is. Here it is:

from livewires import games, color
games.init (screen_width = 640, screen_height = 480, fps = 50)

class Ball (games.Sprite):
    def iMove (self):
        self.dx = -self.dx
        self.dy = -self.dy

class Slab (games.Sprite):
    def mouse_moves (self):
        self.x = games.mouse.x
        self.y = games.mouse.y
        self.iCollide()

    def iCollide (self):
        for Ball in overlapping_sprites:
            Ball.iMove()

def main():
    #Backgrounds
    pingpongbackground = games.load_image ("pingpongbackground.jpg", transparent = False)
    games.screen.background = pingpongbackground
    #Ball: Initializing object and setting speed.
    ballimage = games.load_image ("pingpongball.jpg")
    theball = Ball (image = ballimage,
                    x = 320,
                    y = 240,
                    dx = 2,
                    dy = 2)
    games.screen.add(theball)
    #Paddle: Initializing ping pong object and setting initial poisition to the initial mouse position
    slabimage = games.load_image ("pingpongpaddle.jpg")
    theslab = Slab (image = slabimage,
                    x = games.mouse.x,
                    y = games.mouse.y)
    games.screen.add(theslab)
    games.mouse.is_visible = False
    games.screen.event_grab = True
    games.screen.mainloop()

main ()

And here's a piece of similar, functioning code:

# Slippery Pizza Program
# Demonstrates testing for sprite collisions

from livewires import games
import random

games.init(screen_width = 640, screen_height = 480, fps = 50)


class Pan(games.Sprite):
    """" A pan controlled by the mouse. """
    def update(self):
        """ Move to mouse position. """
        self.x = games.mouse.x
        self.y = games.mouse.y
        self.check_collide()

    def check_collide(self):
        """ Check for collision with pizza. """
        for pizza in self.overlapping_sprites:
            pizza.handle_collide()


class Pizza(games.Sprite):
    """" A slippery pizza. """
    def handle_collide(self):
        """ Move to a random screen location. """
        self.x = random.randrange(games.screen.width)
        self.y = random.randrange(games.screen.height)


def main():
    wall_image = games.load_image("wall.jpg", transparent = False)
    games.screen.background = wall_image

    pizza_image = games.load_image("pizza.bmp")
    pizza_x = random.randrange(games.screen.width)
    pizza_y = random.randrange(games.screen.height)
    the_pizza = Pizza(image = pizza_image, x = pizza_x, y = pizza_y)
    games.screen.add(the_pizza)

    pan_image = games.load_image("pan.bmp")
    the_pan = Pan(image = pan_image,
                  x = games.mouse.x,
                  y = games.mouse.y)
    games.screen.add(the_pan)

    games.mouse.is_visible = False

    games.screen.event_grab = True

    games.screen.mainloop()

# kick it off!
main()

Any insight at all would be greatly appreciated!


Solution

  • I don't know your framework, but to keep the slab from "getting stuck" you need to update its location when the mouse is moved.

    Here you initialize it:

     theslab = Slab (image = slabimage,
                        x = games.mouse.x,
                        y = games.mouse.y)
    

    Then here you add it to the game:

    games.screen.add(theslab)
    

    Presumably, the game would then call this function whenever the mouse moves:

     def mouse_moves (self):
        self.x = games.mouse.x
        self.y = games.mouse.y
        self.iCollide()
    

    But that is either not happening, or the screen is not getting updated.

    So you should find out, by doing this:

     def mouse_moves (self):
        print "mouse_moves: ", str(games.mouse.x), str(games.mouse.y) 
        self.x = games.mouse.x
        self.y = games.mouse.y
        self.iCollide()
    

    If you see the output of the print statement happening when the mouse moves, you're probably not updating the screen, you'll need to check the framework docs. But I don't think that's the case. I think you are not updating the game when the mouse moves. I would imagine the framework has some sort of onMouseMove type event that you need to hook into, to allow you to update the game state (i.e. call mouse_moves()) when mouse movement occurs. Then, the next time the screen is updated, you should check for changes (invalidate the objects, mark them as dirty) and if they are dirty, update their part of the screen then mark them clean again.