-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
172 lines (113 loc) · 4.95 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
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
import pygame,random # importando o modulo, ativo em config > modulos
from pygame.locals import *
SCREEN_LARGURA = 400 # largura tela
SCREEN_ALTURA = 800 # altura tela,,
SPEED = 10 # velocidade 10
GRAVITY = 1
GAME_SPEED = 20
GROUND_LARGURA = 2 * SCREEN_LARGURA
GROUND_ALTURA = 100
LARGURA_PIPE = 80
ALTURA_PIPE = 500
ESPACO_PIPE = 200
class Bird(pygame.sprite.Sprite): # Criando a classe passaro. (POO)
def __init__(self):
pygame.sprite.Sprite.__init__(self) # Primeira coisa a se fazer ao criar uma classe no Python.
self.images = [pygame.image.load('redbird-upflap.png').convert_alpha(),
pygame.image.load('redbird-midflap.png').convert_alpha(),
pygame.image.load('redbird-downflap.png').convert_alpha()]
self.speed = SPEED
self.current_image = 0
self.image = pygame.image.load('redbird-upflap.png').convert_alpha()
self.mask = pygame.mask.from_surface(self.image)
self.rect = self.image.get_rect()
self.rect[1] = SCREEN_ALTURA / 2
self.rect[0] = SCREEN_LARGURA / 2
def update(self):
self.current_image = (self.current_image + 1) % 3
self.image = self.images[self.current_image]
# Update height
self.rect[1] += self.speed
self.speed += GRAVITY
def bump(self):
self.rect[1] -= 5
self.speed = -SPEED
class Pipe(pygame.sprite.Sprite):
def __init__(self, inverted, xpos, ysize):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('pipe-red.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (LARGURA_PIPE, ALTURA_PIPE))
self.rect = self.image.get_rect()
self.rect[0] = xpos
if inverted:
self.image = pygame.transform.flip(self.image, False, True)
self.rect[1] = - (self.rect[3] - ysize)
else:
self.rect[1] = SCREEN_ALTURA - ysize
self.mask = pygame.mask.from_surface(self.image)
def update(self):
self.rect[0] -= GAME_SPEED
class Ground(pygame.sprite.Sprite):
def __init__(self, xpos):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('base.png').convert_alpha()
self.mask = pygame.mask.from_surface(self.image)
self.image = pygame.transform.scale(self.image, (GROUND_LARGURA, GROUND_ALTURA))
self.rect = self.image.get_rect()
self.rect[0] = xpos
self.rect[1] = SCREEN_ALTURA - GROUND_ALTURA
def update(self):
self.rect[0] -= GAME_SPEED
def is_off_screen(sprite):
return sprite.rect[0] < -(sprite.rect[2])
def get_canos_random(xpos):
size = random.randint(100, 300)
pipe = Pipe(False, xpos, size)
pipe_inverted = Pipe(True, xpos, SCREEN_ALTURA - size - ESPACO_PIPE)
return (pipe, pipe_inverted)
clock = pygame.time.Clock()
pygame.init() # Inicializa todos os modulos do pygame
screen = pygame.display.set_mode((SCREEN_LARGURA, SCREEN_ALTURA)) # Inicializa uma janela ou um uma tela para exibicao
BACKGROUND = pygame.image.load('background-day.png') # Carregando o backgroud
BACKGROUND = pygame.transform.scale(BACKGROUND, (SCREEN_LARGURA, SCREEN_ALTURA)) # estamos escalando o background, passando a imagem e as as dimensoes.
bird_group = pygame.sprite.Group() # Realizado para melhor gerenciamento de objeto
bird = Bird() # Vamos criar um objeto
bird_group.add(bird) # Vamos add o bird no grupo
ground_group = pygame.sprite.Group()
for n in range(2):
ground = Ground(GROUND_LARGURA * n)
ground_group.add(ground)
pipe_group = pygame.sprite.Group()
for x in range(2):
pipes = get_canos_random(SCREEN_LARGURA * x + 800)
pipe_group.add(pipes[0])
pipe_group.add(pipes[1])
while True: # Looping infinito para execucao do jogo
clock.tick(30)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
pygame.display.update()
if event.type == KEYDOWN:
if event.key == K_SPACE:
bird.bump()
screen.blit(BACKGROUND, (0, 0)) #atualizacao de frames a cada nova alteracao.
if is_off_screen(ground_group.sprites()[0]):
ground_group.remove(ground_group.sprites()[0])
new_ground = Ground(GROUND_LARGURA - 20)
ground_group.add(new_ground)
if is_off_screen(pipe_group.sprites()[0]):
pipe_group.remove(pipe_group.sprites()[0])
pipe_group.remove(pipe_group.sprites()[0])
pipes = get_canos_random(SCREEN_LARGURA * 2 )
pipe_group.add(pipes[0],pipes[1])
bird_group.update()
ground_group.update()
pipe_group.update()
bird_group.draw(screen)
ground_group.draw(screen)
pipe_group.draw(screen)
if (pygame.sprite.groupcollide(bird_group, ground_group, False, False, pygame.sprite.collide_mask) or
pygame.sprite.groupcollide(bird_group, pipe_group, False, False, pygame.sprite.collide_mask)):
break # Gameover
pygame.display.update()