-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.py
167 lines (128 loc) · 6.04 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import pygame, random, sys
###
class LeftPlataforma:
def __init__(self):
self.plataformaImg = None
self.pontuacao = 0
self.plataformaX = .0
self.plataformaY = .0
self.alterarPlataformaY = .0
def apresentarLeftPlataforma(self, screen, x, y):
screen.blit(self.plataformaImg, (x, y))
return screen
class RightPlataforma:
def __init__(self):
self.plataformaImg = None
self.pontuacao = 0
self.plataformaX = .0
self.plataformaY = .0
self.alterarPlataformaY = .0
def apresentarRightPlataforma(self, screen, x, y):
screen.blit(self.plataformaImg, (x, y))
return screen
class Objeto:
def __init__(self):
self.objetoImg = None
self.objetoX = .0
self.objetoY = .0
self.alterarObjetoX = .0
self.alterarObjetoY = .0
def apresentarObjeto(self, screen, x, y):
screen.blit(self.objetoImg, (x, y))
return screen
###
class Pong:
def __init__(self):
pygame.init()
self.FPS = 60
self.fpsClock = pygame.time.Clock()
self.screen = pygame.display.set_mode((800, 600))
self.fundo = pygame.image.load('Assets/Pong/PlanoDeFundo.png')
pygame.display.set_caption('Pong')
icone = pygame.image.load('Assets/Pong/Icone.png')
pygame.display.set_icon(icone)
###
def exibirLeftPlataforma(self):
self.leftPlataforma = LeftPlataforma()
self.leftPlataforma.plataformaImg = pygame.image.load('Assets/Pong/Manipulavel/LeftPlataforma.png')
self.leftPlataforma.plataformaX = 89
self.leftPlataforma.plataformaY = 237
def exibirRightPlataforma(self):
self.rightPlataforma = RightPlataforma()
self.rightPlataforma.plataformaImg = pygame.image.load('Assets/Pong/Manipulavel/RightPlataforma.png')
self.rightPlataforma.plataformaX = 692
self.rightPlataforma.plataformaY = 237
def exibirObjeto(self):
self.objeto = Objeto()
self.objeto.objetoImg = pygame.image.load('Assets/Pong/Manipulavel/Objeto.png')
self.objeto.objetoX = 390
self.objeto.objetoY = 275
###
def executar(self):
self.exibirLeftPlataforma()
self.exibirRightPlataforma()
self.exibirObjeto()
num = random.randint(0, 3)
varXY = [(-4, 2), (4, -2), (-4, -2), (4, 2)]
self.objeto.alterarObjetoX = varXY[num][0]
self.objeto.alterarObjetoY = varXY[num][1]
execPrograma = True
while execPrograma == True:
self.screen.fill((255, 255, 255))
self.screen.blit(self.fundo, (0, 0))
###
for event in pygame.event.get():
if event.type == pygame.QUIT:
execPrograma = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
execPrograma = False
if event.key == pygame.K_w:
self.leftPlataforma.alterarPlataformaY = -4
elif event.key == pygame.K_s:
self.leftPlataforma.alterarPlataformaY = 4
if event.key == pygame.K_UP:
self.rightPlataforma.alterarPlataformaY = -4
elif event.key == pygame.K_DOWN:
self.rightPlataforma.alterarPlataformaY = 4
if event.key == pygame.K_SPACE:
self.leftPlataforma.alterarPlataformaY = 0
###
self.leftPlataforma.plataformaY += self.leftPlataforma.alterarPlataformaY
self.rightPlataforma.plataformaY += self.rightPlataforma.alterarPlataformaY
self.objeto.objetoX += self.objeto.alterarObjetoX
self.objeto.objetoY += self.objeto.alterarObjetoY
### Detecção de colisão com as plataformas
if self.leftPlataforma.plataformaImg.get_rect(x = self.leftPlataforma.plataformaX, y = self.leftPlataforma.plataformaY).colliderect(self.objeto.objetoImg.get_rect(x = self.objeto.objetoX, y = self.objeto.objetoY)) == True:
self.objeto.alterarObjetoX *= -1
if self.rightPlataforma.plataformaImg.get_rect(x = self.rightPlataforma.plataformaX, y = self.rightPlataforma.plataformaY).colliderect(self.objeto.objetoImg.get_rect(x = self.objeto.objetoX, y = self.objeto.objetoY)) == True:
self.objeto.alterarObjetoX *= -1
### Detecção de colisão com os limites
if self.leftPlataforma.plataformaY <= 0:
self.leftPlataforma.plataformaY = 0
elif self.leftPlataforma.plataformaY >= 552:
self.leftPlataforma.plataformaY = 552
if self.rightPlataforma.plataformaY <= 0:
self.rightPlataforma.plataformaY = 0
elif self.rightPlataforma.plataformaY >= 552:
self.rightPlataforma.plataformaY = 552
if self.objeto.objetoY <= 0:
self.objeto.alterarObjetoY *= -1
if self.objeto.objetoY >= 589:
self.objeto.alterarObjetoY *= -1
if self.objeto.objetoX >= 800:
self.leftPlataforma.pontuacao += 1
print('O vencedor da partida foi a plataforma direita.')
pygame.quit()
sys.exit()
if self.objeto.objetoX <= 0:
self.rightPlataforma.pontuacao += 1
print('O vencedor da partida foi a plataforma esquerda.')
pygame.quit()
sys.exit()
###
self.screen = self.leftPlataforma.apresentarLeftPlataforma(self.screen, self.leftPlataforma.plataformaX, self.leftPlataforma.plataformaY)
self.screen = self.rightPlataforma.apresentarRightPlataforma(self.screen, self.rightPlataforma.plataformaX, self.rightPlataforma.plataformaY)
self.screen = self.objeto.apresentarObjeto(self.screen, self.objeto.objetoX, self.objeto.objetoY)
pygame.display.update()
self.fpsClock.tick(self.FPS)