@zachsanford/

Python Play Pong

Pygame

A simple demo game using Python Play: https://github.com/replit/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
57
58
59
60
61
62
63
64
65
# A simple game showing how to use Python Play

import play

play.set_background_color('black')

half_court = play.new_box(color='white', x=0, y=0, width=5, height=play.screen.height)

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

other_box = play.new_box(color='white', 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):
  if box.top < play.screen.top:
   box.y += 10

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

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

@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
    box.color = play.random_color()
    half_court.color = play.random_color()

@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:
      if other_box.top < play.screen.top:
       other_box.y += other_box.dy
    elif other_box.y > ball.y:
      if other_box.bottom > play.screen.bottom:
        other_box.y -= other_box.dy

@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
    other_box.color = play.random_color()
    half_court.color = play.random_color()

@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()