-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatcaxi.py
251 lines (183 loc) · 7.34 KB
/
batcaxi.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import pygame
from pygame.locals import *
from sys import exit
import os
import random
pygame.init()
pygame.mixer.init()
diretorio_principal = os.path.dirname(__file__)
diretorio_imagens = os.path.join(diretorio_principal, 'imagens')
diretorio_sons = os.path.join(diretorio_principal, 'sons')
fundo1 = os.path.join(diretorio_imagens, 'imagem_fundo.png')
fundo2 = os.path.join(diretorio_imagens, 'imagem_game_over.png')
fundo3 = os.path.join(diretorio_imagens, 'imagem_fundo_erro.png')
img_fundo = pygame.image.load(fundo1)
img_fundo2 = pygame.image.load(fundo2)
img_fundo3 = pygame.image.load(fundo3)
largura = 640
altura = 480
tela = pygame.display.set_mode((largura, altura))
sprite_sheet = pygame.image.load(os.path.join(diretorio_imagens, 'imagens.png')).convert_alpha()
pygame.display.set_caption('Joguinho do Summaê')
# som da colisão
som_colisao = pygame.mixer.Sound(os.path.join(diretorio_sons, 'death_sound.wav'))
# aumenta o volume
som_colisao.set_volume(1)
# som da pontuação
som_pontuacao = pygame.mixer.Sound(os.path.join(diretorio_sons, 'score_sound.wav'))
# aumenta o volume
som_pontuacao.set_volume(1)
colidiu = False
pontos = 0
velocidade = 10
def exibe_texto(msg, tamanho, cor):
fonte = pygame.font.SysFont('comicsansms', tamanho, True, False)
mensagem = f'{msg}'
texto_formatado = fonte.render(mensagem, True, cor)
return texto_formatado
class Batman(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.som_pulo = pygame.mixer.Sound(os.path.join(diretorio_sons, 'jump_sound.wav'))
#aumentar o volume
self.som_pulo.set_volume(1)
self.imagens_batman = []
for i in range(3):
img = sprite_sheet.subsurface((i*32,0),(32,32))
img = pygame.transform.scale(img, (32*3, 32*3))
self.imagens_batman.append(img)
self.index_lista = 0
self.image = self.imagens_batman[0]
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
self.rect.center = (100,altura-70)
self.pulo = False
self.posicao_inicial_y = altura-70-48
def pular(self):
self.pulo = True
self.som_pulo.play()
def update(self):
if self.pulo == True:
if self.rect.y <= 200:
self.pulo = False
self.rect.y -= 20
else:
if self.rect.y < altura-70-48:
self.rect.y += 20
else: self.rect.y = altura-70-48
if self.index_lista > 2: self.index_lista = 0
self.index_lista += 0.25
self.image = self.imagens_batman[int(self.index_lista)]
class Nuvens(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = sprite_sheet.subsurface((5*32, 0),(32,32))
self.image = pygame.transform.scale(self.image, (32*3, 32*3))
self.rect = self.image.get_rect()
#self.rect.center = (100,100)
# sortear posições aleatórias para as nuvens
self.rect.y = random.randrange(50, 200, 50)
self.rect.x = largura - random.randrange(30,300,90)
# para a nuvem se movimentar horizontalmente
def update(self):
# se ultrapassar a borda esquerda da tela a posição x será igual a largura da tela
if self.rect.topright[0] < 0:
self.rect.x = largura
self.rect.y = random.randrange(50,200,50)
self.rect.x -= 10
class Chao(pygame.sprite.Sprite):
def __init__(self, posicao_x):
pygame.sprite.Sprite.__init__(self)
self.image = sprite_sheet.subsurface((6*32,0),(32,32))
self.image = pygame.transform.scale(self.image, (32*6,32*2))
self.rect = self.image.get_rect()
self.rect.y = altura - 56
self.rect.x = posicao_x * 60
def update(self):
if self.rect.topright[0] < 0:
self.rect.x = largura
self.rect.x -=10
class Abacaxi(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = sprite_sheet.subsurface((4*32,0),(32,32))
self.image = pygame.transform.scale(self.image, (32*4,32*2))
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
self.rect.center = (largura, altura-70)
self.rect.x = largura
def update(self):
if self.rect.topright[0] < 0:
self.rect.x = largura
self.rect.x -= velocidade
todas_as_sprites = pygame.sprite.Group()
batman = Batman()
todas_as_sprites.add(batman)
# criar várias nuvens
for i in range(4):
nuvem = Nuvens()
todas_as_sprites.add(nuvem)
# repetir o bloco do chão várias vezes
for i in range(largura//32):
chao = Chao(i)
todas_as_sprites.add(chao)
abacaxi = Abacaxi()
todas_as_sprites.add(abacaxi)
grupo_obstaculos = pygame.sprite.Group()
grupo_obstaculos.add(abacaxi)
relogio = pygame.time.Clock()
clicou_errado = False
while True:
relogio.tick(30)
# coloca imagem no fundo
tela.blit(img_fundo,(0,0))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
if event.type == KEYDOWN:
if colidiu == False:
if event.key == K_SPACE:
if batman.rect.y != batman.posicao_inicial_y: pass
else: batman.pular()
else:
if event.key == K_c:
pontos = 0
colidiu = False
velocidade = 10
abacaxi = Abacaxi()
grupo_obstaculos.add(abacaxi)
todas_as_sprites.add(abacaxi)
todas_as_sprites.update()
abacaxi.rect.center = (largura, altura-70)
else: clicou_errado = True
colisoes = pygame.sprite.spritecollide(batman, grupo_obstaculos, True, pygame.sprite.collide_mask)
todas_as_sprites.draw(tela)
# se não ocorrer colissões atualiza as sprites
if colisoes and colidiu == False:
som_colisao.play()
colidiu = True
if colidiu == True:
# exibir mensagem de Game Over
if clicou_errado == False:
tela.blit(img_fundo2,(0,0))
questao = '∫{}xcos(x²) dx'.format(pontos)
game_over = exibe_texto(questao, 16, (0,0,0))
go2 = exibe_texto("Para continuar jogando, substitua x por 0 no resultado da integral", 15, (0,0,0) )
tela.blit(game_over, (largura/2.6, altura/1.4))
tela.blit(go2, (largura/7.5, altura/1.3))
if pontos % 100 == 0: pontos += 1
else:
tela.blit(img_fundo3, (0,0))
pygame.display.update()
pygame.time.delay(150)
clicou_errado = False
else:
pontos += 1
todas_as_sprites.update()
texto_pontos = exibe_texto(pontos, 40, (25,25,112))
tela.blit(texto_pontos, (520,30))
if pontos % 100 == 0:
som_pontuacao.play()
if velocidade >= 23: velocidade += 1
pygame.display.flip()