-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.py
70 lines (53 loc) · 1.57 KB
/
pong.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
66
67
68
69
70
import random
import pygame
import pygame.locals as l
from pygame.locals import *
TITLE = "Mind Pong!"
class Player(object):
def take_input(self, event):
dy = random.randint(-2, 5)
return dy
def update(self, game):
pass
class Game(object):
pass
def main():
# Initialise screen
pygame.init()
screen = pygame.display.set_mode((600, 400))
W, H = screen.get_size()
print W, H
pygame.display.set_caption(TITLE)
# Fill background
background = pygame.Surface((W, H))
background = background.convert()
background.fill((250, 250, 250))
# Blit everything to the screen
screen.blit(background, (0, 0))
pygame.display.flip()
clock = pygame.time.Clock()
y = 20
bat = Rect((10, y), (10, 100))
# Event loop
pygame.event.set_allowed([ACTIVEEVENT, NOEVENT, SYSWMEVENT, KEYDOWN, QUIT, JOYBUTTONDOWN])
player = Player()
while True:
for event in pygame.event.get():
#print [a for a in dir(l) if getattr(l, a)==event.type], event
if event.type==QUIT:
return
if event.type==KEYDOWN and event.key in (K_q, K_ESCAPE):
return
time_passed = clock.tick(30) # in ms
dy = player.take_input(None)
y += dy
print dy
y = min(y, H - (10+100))
y = max(y, 10)
bat = Rect((10, y), (10, 100))
print bat
screen.blit(background, (0, 0))
pygame.draw.rect(screen, (0, 0, 0), bat)
pygame.display.flip()
if __name__ == '__main__':
main()