-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprites.py
142 lines (123 loc) · 4.29 KB
/
sprites.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
import pygame
import random
from pygame.locals import (
K_a,K_s,K_w,K_d,
K_h,K_j,K_k,K_l,
K_LEFT,K_DOWN,K_UP,K_RIGHT)
from physics import Physics
from settings import Settings
controls = {
0:{K_LEFT:'left',K_DOWN:'down',K_UP:'up',K_RIGHT:'right'},
1:{K_a:'left',K_s:'down',K_w:'up',K_d:'right'},
3:{K_h:'left',K_j:'down',K_k:'up',K_l:'right'},
}
vec = pygame.math.Vector2
import json
class Angel(pygame.sprite.Sprite):
def __init__(self,_id,pos = [0,0],color = 0):
# essentials
super(Angel,self).__init__()
# import settings
self.settings = Settings()
# set id
self.id = _id
# surface and rect
self.image = pygame.Surface((200,200))
self.image = pygame.transform.scale(self.image,(30,30))
self.color = color
self.image.fill(self.settings.colors[color])
self.rect = self.image.get_rect()
self.rect.x,self.rect.y = pos
self.image.set_colorkey('#000000')
# movement
self.controls = controls[0]
self.physics = Physics(self.rect,1)
self.last_state = None
self.jumping = False
self.air_timer = 0
self.colliding = {'top':False,'bottom':False,'left':False,'right':False}
self.wall = False
# additional features
self.lives = 3
self.stamina = 10.0
self.capJump = 12
def makeDevil(self):
self.image.fill(self.settings.colors[self.color])
pygame.draw.circle(self.image,'#000000',(15,15),9)
pygame.draw.circle(self.image,self.settings.colors[self.color],(15,15),5)
def makeAngel(self):
self.image.fill(self.settings.colors[self.color])
def move_x(self):
# apply physics to player.physics.rect
self.physics.friction('grass')
self.physics.motion_x()
self.rect.move_ip(self.physics.vel.x,0)
def move_y(self):
# apply physics to player.physics.rect
self.physics.gravity('earth')
self.physics.motion_y()
self.rect.move_ip(0,self.physics.vel.y)
def update(self):
self.wall = False
if self.colliding['top']:
self.physics.vel.y *= 0.2
if self.colliding['left']:
self.physics.vel.x *= -0.5
if self.moving:
self.wall = True
self.air_timer = True
if self.colliding['right']:
self.physics.vel.x *= -0.5
if self.moving:
self.wall = True
self.air_timer = True
if self.colliding['bottom']:
self.physics.vel.y *= -0.5
self.air_timer = True
else:
pass
acc = 0
if self.jumping and self.air_timer:
if self.wall:
if self.colliding['right']:
self.physics.vel.x = -10
self.physics.acc.x += 0.3
elif self.colliding['left']:
self.physics.vel.x = +10
self.physics.acc.x -= 0.3
self.wall = False
self.air_timer = False
self.jump()
if self.wall:
self.physics.vel.y += 0.0001
if self.physics.vel.y > 0.6:
self.physics.vel.y = 0.6
self.physics.vel.x = 0
self.physics.fr = 0.1
else:
self.physics.fr = 0
# apply those changes to player's position
# NOTE: collisions will be implelemnted(restrictions) here
def start_move(self,e):
if e.key in self.controls.keys():
movement = self.controls[e.key]
self.moving = True
if movement == 'left':
self.physics.acc.x = -self.settings.base_acc
self.last_state = 'left'
if movement == 'right':
self.last_state = 'right'
self.physics.acc.x = +self.settings.base_acc
def stop_move(self,e):
try:
if self.controls[e.key] == self.last_state:
self.moving = False
self.physics.acc = vec(0,0)
if self.controls[e.key] == 'down':
self.physics.acc.y = 0
except:
pass
def jump(self):
self.physics.vel.y = -self.capJump
def dash(self):
self.physics.acc.y = 3