class Charachter():
def __init__(self, x, y, type_, d_right=pygame.K_RIGHT, d_left=pygame.K_LEFT, d_up=pygame.K_UP):
self.x = x
self.y = y
self.width = 34
self.height = 50
self.type = type_
self.direction = Direction.UP
self.KEY = {d_right: Direction.RIGHT,
d_left: Direction.LEFT, d_up: Direction.UP}
self.hitbox = pygame.Rect(self.x, self.y, self.width, self.height)
self.isJump = True
self.yvel = 0 # скорость вертикального перемещения
def run(self, direction, time, power):
dx = 80*time
# if direction not in self.KEY.keys():
# return
if direction == Direction.LEFT:
dx *= -1
self.x += dx
if direction == Direction.UP:
self.isJump = True
if (self.isJump):
if power >= -10:
if power < 0:
self.y += (power**2)/2
else:
self.y -= (power**2)/2
power -= 1
else:
self.isJump = False
power = 10
self.hitbox = pygame.Rect(
round(self.x+2*dx), round(self.y), self.width, self.height)
self.change_direction(direction)
for wall in walls:
if self.hitbox.colliderect(wall.hitbox):
self.x -= dx
return
def draw(self):
img = person_images[self.type][self.direction]
screen.blit(img, (round(self.x-img.get_width()/5), round(self.y)))
# pygame.draw.rect(screen, [0, 255, 0],(self.x, self.y, self.width, self.height), 1)
def change_direction(self, direction):
self.direction = direction
class Direction(Enum):
UP = 'UP'
LEFT = 'LEFT'
RIGHT = 'RIGHT'
MOVE_KEYS = {
pygame.K_a: 'LEFT',
pygame.K_d: 'RIGHT',
pygame.K_w: 'UP'
}
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_ok = False
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
is_ok = False
pygame.quit()
sys.exit()
return
keys = pygame.key.get_pressed()
for ch in characters: //characters-list of my characters
for k in ch.KEY:
if keys[k]:
ch.run(ch.KEY[k], seconds, 10)
break
else:
ch.change_direction(Direction.UP)
pygame.display.flip()