-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
75 lines (62 loc) · 1.79 KB
/
game.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
71
72
73
74
75
import pygame
from time import sleep, time
from pygame.locals import *
from paddle import *
from ball import *
from background import *
from scoreboard import *
from constants import *
# Main loop of game where gameplay happens
def game(screen, ball, scoreboard, paddles):
# Initialization of paddles
paddle_one = paddles[0]
paddle_two = paddles[1]
running = True
# Resets ball, paddles, and scoreboard from possible previous games
ball.reset(1)
ball.count = 5
paddle_one.reset()
paddle_two.reset()
scoreboard.reset()
# Main Game loop
while(running):
t0 = time()
# Stop running on exit
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#end
# Clear screen before each frame
drawBackground(screen)
# Update Objects
paddle_one.update()
paddle_two.update()
ball.paddleCollide(paddles)
ball.update()
# Scoring
scoreboard.count(ball)
if ball.count == 1:
paddle_one.reset()
paddle_two.reset()
#end
# Render objects
paddle_one.render(screen)
paddle_two.render(screen)
ball.render(screen)
scoreboard.render(screen)
# Display frame
display_surface = pygame.display.get_surface()
display_surface.blit(pygame.transform.flip(display_surface, False, True), dest=(0, 0))
pygame.display.flip()
t1 = time()
dt = t1 - t0
# Timer for consistant frames per second
if dt < 0.002:
sleep(0.002 - dt)
# Stop running when there is a winner
if scoreboard.winner() != 0:
running = False
#end
#end
return scoreboard.score()
#end game