Repository navigation
Expand file tree
/
Copy pathtut32.py
More file actions
148 lines (118 loc) · 4.84 KB
/
Copy pathtut32.py
File metadata and controls
148 lines (118 loc) · 4.84 KB
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
import pygame
class Fondo(pygame.sprite.Sprite):
def __init__(self):
self.imagen=pygame.image.load("fondomovil.gif").convert_alpha()
self.rect=self.imagen.get_rect()
def update(self,pantalla,vx,vy):
self.rect.move_ip(-vx,-vy)
pantalla.blit(self.imagen,self.rect)
#clase del jugador
class Player(pygame.sprite.Sprite):
def __init__(self):
#creo 4 imagenes
self.imagen1=pygame.image.load("animacion1.png").convert_alpha()
self.imagen2=pygame.image.load("animacion2.png").convert_alpha()
self.imagen3=pygame.image.load("animacion1l.png").convert_alpha()
self.imagen4=pygame.image.load("animacion2l.png").convert_alpha()
# creo la lista de las imaganes
#el primer indice es la orientacion y el segundo la imagen
# self.imagenes[self.orientacion][self.imagen_actual]
self.imagenes=[[self.imagen1,self.imagen2],[self.imagen3,self.imagen4]]
self.imagen_actual=0
self.imagen=self.imagenes[self.imagen_actual][0]
self.rect=self.imagen.get_rect()
self.rect.top,self.rect.left=(400,400)
#variable par ver si se esta moviendo
self.estamoviendo=False
# 0 si va ala derecha 1 si va la izquierda
self.orientacion=0
def mover(self,vx,vy):
self.rect.move_ip(vx,vy)
#funcion principal de actualizacion
def update(self,superficie,vx,vy,t):
# si no se mueve self.estamoviendo=FALSE
if (vx,vy)==(0,0): self.estamoviendo=False
else:self.estamoviendo=True # si se mueve que este en TRUE
# con estas 2 lineas cambio la orientacion
if vx>0: self.orientacion=0
elif vx<0: self.orientacion=1
# si el t==1 (auxiliar) y se esta moviendo entonces cambiar la imagen
if t==1 and self.estamoviendo:
self.nextimage()
# mover el rectangulo
vx=0
vy=0
self.mover(vx, vy)
#self.imagen va ser la imagen que este en la orientacion y en el numero de imagen_actual
self.imagen=self.imagenes[self.orientacion][self.imagen_actual]
#finalmente pintar en la pantalla
superficie.blit(self.imagen,self.rect)
#funcion que se encarga de cambiar de imagen
def nextimage(self):
self.imagen_actual+=1
if self.imagen_actual>(len(self.imagenes)-1):# si se fue de rango que lo ponga en 0
self.imagen_actual=0
def main():
import pygame
pygame.init()
pantalla=pygame.display.set_mode((800,600))
salir=False
reloj1= pygame.time.Clock()
#creo un player
player1=Player()
fondo1=Fondo()
vx,vy=0,0
velocidad=7
#auxiliares para el movimiento
leftsigueapretada,rightsigueapretada,upsigueapretada,downsigueapretada=False,False,False,False
t=0
while salir!=True:#LOOP PRINCIPAL
for event in pygame.event.get():
#control de eventos
if event.type == pygame.QUIT:
salir=True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
leftsigueapretada=True
vx=-velocidad
if event.key == pygame.K_RIGHT:
rightsigueapretada=True
vx=velocidad
if event.key== pygame.K_UP:
upsigueapretada=True
vy=-velocidad
if event.key == pygame.K_DOWN:
downsigueapretada=True
vy=velocidad
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
leftsigueapretada=False
if rightsigueapretada:vx=velocidad
else:vx=0
if event.key == pygame.K_RIGHT:
rightsigueapretada=False
if leftsigueapretada:vx=-velocidad
else:vx=0
if event.key== pygame.K_UP:
upsigueapretada=False
if downsigueapretada:vy=velocidad
else:vy=-0
if event.key == pygame.K_DOWN:
downsigueapretada=False
if upsigueapretada:vy=-velocidad
else:vy=0
reloj1.tick(25)# 25 fps
#auxiliar de la animacion
t+=1
if t>1:
t=0
# pintar el fondo
pantalla.fill((255,255,255))
#pinto fondo
fondo1.update(pantalla,vx,vy)
# actualizar jugador
player1.update(pantalla,vx,vy,t)
#actualizar pantalla
pygame.display.update()
pygame.quit()
main()