#setup the constants WIDTH = 500 HEIGHT = 300 PADDLESPEED = 5 def reset_game(): #position the ball ball.x = WIDTH / 2 ball.y = HEIGHT / 2 #set initial direction of ball ballDirectionX = 1 ballDirectionY = 1 #position the paddles pad1.x = 10 pad1.y = HEIGHT / 2 pad2.x = WIDTH - 10 pad2.y = HEIGHT / 2 #create a rectangle of the playing area screenRect = Rect(10,0,WIDTH - 10,HEIGHT) #create ball ball = Actor('ball') #create paddles pad1 = Actor('paddle') pad2 = Actor('paddle') #reset the game reset_game() #setup the goals goals = [0, 0] def draw(): screen.clear() ball.draw() pad1.draw() pad2.draw() def update(): #move the paddles if keyboard.q: pad1.y = pad1.y - PADDLESPEED if keyboard.a: pad1.y = pad1.y + PADDLESPEED if keyboard.k: pad2.y = pad2.y - PADDLESPEED if keyboard.m: pad2.y = pad2.y + PADDLESPEED #move the ball ball.x = ball.x + ballDirectionX ball.y = ball.y + ballDirectionY #has the ball left the screen? if not screenRect.contains(ball): #did it hit the top or bottom? if ball.top < 0 or ball.bottom > HEIGHT: ballDirectionY = ballDirectionY -1 #it must have hit the side else: if ball.left < 10: print("Player 2 goal") goals[1] = goals[1] + 1 reset_game() sleep(2) print("Score {} : {}".format(goals[0], goals[1])) elif ball.right > WIDTH - 10: print("player 1 goal") goals[0] = goals[0] + 1 reset_game() sleep(2) print("Score {} : {}".format(goals[0], goals[1])) #has the ball hit a paddle if pad1.colliderect(ball) or pad2.colliderect(ball): ballDirectionX = ballDirectionX * -1
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question