Single-player pong game
Posted
by Jam
on Stack Overflow
See other posts from Stack Overflow
or by Jam
Published on 2010-04-20T01:02:25Z
Indexed on
2010/04/20
1:03 UTC
Read the original article
Hit count: 480
I am just starting out learning pygame and livewires, and I'm trying to make a single-player pong game, where you just hit the ball, and it bounces around until it passes your paddle (located on the left side of the screen and controlled by the mouse), which makes you lose. However, I keep getting the error: "Cannot have more than on Screen object", which I can find no references to online really, and I can't make sense of it. I want to eventually make the game more complicated, but I need to make it work first. Help please! Here's the code so far:
from livewires import games
games.init(screen_width=640, screen_height=480, fps=50)
class Paddle(games.Sprite):
image=games.load_image("paddle.bmp")
def __init__(self):
super(Paddle, self).__init__(image=Paddle.image,
y=games.mouse.y,
left=0)
self.score=games.Text(value=0, size=25, top=5, right=games.screen.width-10)
games.screen.add(self.score)
def update(self):
self.y=games.mouse.y
self.check_collide()
def check_collide(self):
for ball in self.overlapping_sprites:
self.score.value+=1
self.score.right=games.screen.width-10
ball.handle_collide()
class Ball(games.Sprite):
image=games.load_image("ball.bmp")
speed=1
def __init__(self, x, y=90):
super(Ball, self).__init__(image=Ball.image,
x=x, y=y,
dx=Ball.speed, dy=Ball.speed)
def update(self):
if self.left<0:
self.end_game()
self.destroy()
def handle_collide(self):
if self.right>games.screen.width:
self.dx=-self.dx
if self.bottom>games.screen.height or self.top<0:
self.dy=-self.dy
def ball_destroy(self):
self.destroy()
def main(): background_image=games.load_image("background.bmp", transparent=False) games.screen.background=background_image
the_ball=Ball()
games.screen.add(the_ball)
the_paddle=Paddle()
games.screen.add(the_paddle)
games.mouse.is_visible=False
games.screen.event_grab=True
games.screen.mainloop()
main()
© Stack Overflow or respective owner