repl.it
@glench/

Python Play pong game demo

Pygame

A simple pong game using Python Play.

fork
loading
Files
  • main.py
  • nohup.out
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import play # this should always be the first line

box = play.new_box(color='black', x=350, y=0, width=30, height=120)

other_box = play.new_box(color='black', x=-350, y=0, width=30, height=120)
other_box.dy = 3

ball = play.new_box(color='dark red', x=0, y=0, width=20, height=20)
ball.dx = 2
ball.dy = -1

@play.when_key_pressed('up')
async def do(key):
    box.y += 10

@play.when_key_pressed('down')
async def do(key):
    box.y -= 10

# make the ball move
@play.repeat_forever
async def do():
    ball.x += ball.dx
    ball.y += ball.dy

# make the ball bounce off the player's paddle
@play.repeat_forever
async def do():
    if (ball.right >= box.left) and (ball.top >= box.bottom) and (ball.bottom <= box.top) and (ball.left < box.left):
        ball.dx = -2

# make the computer player follow the ball
@play.repeat_forever
async def do():
    if ball.x < 0 and abs(ball.y-other_box.y) > other_box.dy:
        if other_box.y < ball.y:
            other_box.y += other_box.dy
        elif other_box.y > ball.y:
            other_box.y -= other_box.dy

# make the ball bounce off the computer player's paddle
@play.repeat_forever
async def do():
    if (ball.left <= other_box.right) and (ball.top >= other_box.bottom) and (ball.bottom <= other_box.top) and (ball.right > other_box.right):
        other_box.dy = play.random_number(1, 4)
        ball.dx = 2

# make ball bounce off bottom and top walls
@play.repeat_forever
async def do():
    if ball.bottom <= play.screen.bottom:
        ball.dy = 1
    elif ball.top >= play.screen.top:
        ball.dy = -1

play.start_program() # this should always be the last line