-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
74 lines (58 loc) · 2.13 KB
/
player.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
"""Codigo do jogador."""
import pygame
PLAYER_SPEED = 6
PLAYER_PULO = 7
GRAVIDADE = 2
MAX_Y = 24
TEMPO_PULO = 8 # Qtd maxima de frames que o pulo dura
CONSTANTE_PULO_MONSTRO = 3
# Imagens:
olhando_direita = pygame.image.load("assets/PNG/player1.png")
olhando_esquerda = pygame.image.load("assets/PNG/player2.png")
pulando_direita = pygame.image.load("assets/PNG/playerjump1.png")
pulando_esquerda = pygame.image.load("assets/PNG/playerjump2.png")
class Player(pygame.sprite.Sprite):
def __init__(self, posicao):
super().__init__()
self.image = pygame.Surface((32, 64)) # Sprite do jogador
self.image = olhando_direita
self.rect = self.image.get_rect(topleft=posicao)
# Usar vetor pq é melhor que tupla/lista
# Velocidade do jogador
self.speed = PLAYER_SPEED
self.direcao = pygame.math.Vector2(0, 0)
self.constante_gravidade = GRAVIDADE
self.speed_pulo = PLAYER_PULO
# Contador de pulo
self.tempo_pulo = 0
def key_input(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_d]:
self.direcao.x = 1
self.image = olhando_direita
elif keys[pygame.K_a]:
self.direcao.x = -1
self.image = olhando_esquerda
else:
self.direcao.x = 0
if keys[pygame.K_SPACE]:
self.pulo()
def gravidade(self):
self.direcao.y += self.constante_gravidade
self.rect.y += self.direcao.y
def pulo(self, do_monstro=False):
if self.tempo_pulo < TEMPO_PULO:
self.direcao.y -= self.speed_pulo
self.tempo_pulo += 1
if do_monstro:
self.direcao.y -= self.speed_pulo * CONSTANTE_PULO_MONSTRO
self.image = pulando_direita if self.direcao.x == 1 else pulando_esquerda
def max_speed_y(self):
if self.direcao.y > MAX_Y:
self.direcao.y = MAX_Y
elif self.direcao.y < -MAX_Y:
self.direcao.y = -MAX_Y
def update(self):
self.key_input()
self.max_speed_y()
# incrementa o pulo a velocidade (pulo = -16, é negativo, pygame estranho...)