-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.py
96 lines (79 loc) · 3.07 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os, sys
import pygame
from pygame.locals import *
from elements.physics import Circulo
from elements.level import level_getter, Saida
if not pygame.font: print('Warning, fonts disabled')
if not pygame.mixer: print('Warning, sound disabled')
class SquareLandGame:
width = 800
height = 600
def __init__(self):
self.width = SquareLandGame.width
self.height = SquareLandGame.height
pygame.init()
self.screen = pygame.display.set_mode((self.width, self.height))
self.elementos_fisica = []
self.plataformas = []
self.saidas = []
def create_level_by_name(self, level_name):
level = level_getter(level_name)
if level:
self.create_level(level)
else:
sys.exit()
def create_level(self, level):
self.elementos_fisica.clear()
self.personagem = Circulo(posicao=level.spawn_point())
self.personagem.falling = True
self.elementos_fisica.append(self.personagem)
self.plataformas.clear()
self.saidas.clear()
for plataforma in level.plataformas():
self.plataformas.append(plataforma)
self.elementos_fisica.append(plataforma)
for saida in level.saidas():
obj_saida = Saida([saida[0], saida[1]], saida[2])
self.saidas.append(obj_saida)
self.elementos_fisica.append(obj_saida)
def controller(self):
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if (event.key == K_LEFT):
self.personagem.controle(-1)
elif (event.key == K_RIGHT):
self.personagem.controle(1)
elif (event.key == K_UP):
self.personagem.pular()
if event.type == KEYUP:
if (event.key == K_LEFT):
self.personagem.controle(0)
elif (event.key == K_RIGHT):
self.personagem.controle(0)
def run_game(self):
tique_taque = pygame.time.Clock()
acc = 0
while True:
self.screen.fill((0,0,0))
delta_seconds = (tique_taque.get_time()) / 1000
acc += delta_seconds
tique_taque.tick(600)
self.controller()
for plataforma in self.plataformas:
if self.personagem.colisao(plataforma):
self.personagem.falling = False
self.personagem.teste_stick()
for elemento_fisico in self.elementos_fisica:
elemento_fisico.cair(delta_seconds)
elemento_fisico.movimento(delta_seconds)
elemento_fisico.render(self.screen)
for saida in self.saidas:
if saida.fim_de_fase(self.personagem):
self.create_level_by_name(saida.destino)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.update()