/
Main.py
136 lines (104 loc) · 3.98 KB
/
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Import and set up modules
import pygame
pygame.init ()
from math import sin, cos, radians, acos, asin
from random import randint
from copy import deepcopy
from pygame.locals import *
from Angle import Angle
from Ball import Ball
from Circle import Circle
from Colour import Colour
from Constants import *
from Player import Player
from Sound import Sound
from Wall import Wall
# Create window
window = pygame.display.set_mode (WINDOWSIZE, 0, 32)
pygame.display.set_caption ("Pong")
def update_all (balls, walls, players, clock):
Ball.time += clock.tick ()
if Ball.time > Ball.ADD_SPEED:
balls.append (Ball (Ball.SIZE, Ball.SPEED, balls [:] ) )
Ball.time = 0
for i, b in enumerate (balls):
# The ball is out of the window
if b.rect.right < 0 or b.rect.left > WINDOWSIZE [0]:
if b.rect.right < 0:
players [1].score += 1
if b.rect.left > WINDOWSIZE [0]:
players [0].score += 1
# Delete all balls and reset with 1 ball
for i in range (len (balls)) [::-1]:
del balls [i]
balls.append (Ball (Ball.SIZE, Ball.SPEED, [] ) )
for p in players:
p.reset ()
if Sound.isOn:
Sound.point.play ()
drawScreen (walls, players, balls)
pygame.time.wait (1000)
break
balls_old = balls [:]
for b in balls:
b.update (balls_old [:], walls, players)
def drawScreen (walls, players, balls):
window.fill ((255, 255, 255))
pygame.draw.line (window, Colour.BLUE, (WINDOWSIZE [0] / 2, 0), (WINDOWSIZE [0] / 2, WINDOWSIZE [1]) )
for w in walls:
pygame.draw.rect (window, Colour.BLACK, w.rect)
for p in players:
pygame.draw.rect (window, Colour.YELLOW, p.rect)
for b in balls:
pygame.draw.circle (window, Colour.BLUE, b.rect.center, b.rect.width / 2)
scores_surfaces = [players [0].getScoreSurface (), players [1].getScoreSurface ()]
scores_rects = [scores_surfaces [0].get_rect (), scores_surfaces [1].get_rect ()]
scores_rects [0].topright = (WINDOWSIZE [0] / 2 - 50, Wall.SIZE)
scores_rects [1].topleft = scores_rects [1].topleft = (WINDOWSIZE [0] / 2 + 50, Wall.SIZE)
for i in range (2):
window.blit (scores_surfaces [i], scores_rects [i])
pygame.display.update ()
# Create game objects
clock = pygame.time.Clock ()
walls = Wall.createList ()
players = [Player (25, Player.SIZE, 2, 270), Player (775, Player.SIZE, 2, 90)]
balls = [Ball (Ball.SIZE, Ball.SPEED, [])]
if Sound.isOn:
pygame.mixer.music.set_volume (0.6)
pygame.mixer.music.play ()
keepGoing = True
while keepGoing:
for event in pygame.event.get ():
if event.type == QUIT:
keepGoing = False
# Set players' directions
elif event.type == KEYDOWN:
if event.key == ord ("w"):
players [0].moveUp = True
players [0].moveDown = False
elif event.key == ord ("s"):
players [0].moveDown = True
players [0].moveUp = False
elif event.key == K_UP:
players [1].moveUp = True
players [1].moveDown = False
elif event.key == K_DOWN:
players [1].moveDown = True
players [1].moveUp = False
elif event.type == KEYUP:
if event.key == ord ("w"):
players [0].moveUp = False
elif event.key == ord ("s"):
players [0].moveDown = False
elif event.key == K_UP:
players [1].moveUp = False
elif event.key == K_DOWN:
players [1].moveDown = False
# Update game objects
for p in players:
p.update (walls)
update_all (balls, walls, players, clock)
# Draw screen:
drawScreen (walls, players, balls)
pygame.mixer.music.stop ()
pygame.quit ()