-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.py
73 lines (56 loc) · 2.6 KB
/
user.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
import pygame
import os
import math
from gameWall import walls
ROTATION_VEL = 3
class User(pygame.sprite.Sprite):
def __init__(self, x, y, sprites):
super().__init__()
self.x = x
self.y = y
self.HEIGHT = 25
self.WIDTH = 25
self.IMAGE_WIDTH = 40
self.IMAGE_HEIGHT = 40
self.rot = 0
self.health = 100
self.sprites = sprites
self.currentimage = 0
self.originalImage = pygame.transform.scale(
self.sprites[self.currentimage%len(self.sprites)], (self.WIDTH, self.HEIGHT))
self.image = pygame.transform.scale(
self.originalImage, (self.IMAGE_WIDTH, self.IMAGE_HEIGHT))
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
def shoot(self):
pass
def to_radian(self, rot):
return math.pi * 2 * rot / 360
def move(self, keys_pressed, WIDTH, HEIGHT, VEL, prev):
initialx, initialy = self.x, self.y
if keys_pressed[pygame.K_a]:
self.rot = (self.rot + ROTATION_VEL) % 360
if keys_pressed[pygame.K_d]:
self.rot = (self.rot - ROTATION_VEL) % 360
if keys_pressed[pygame.K_w]:
self.currentimage += 1
self.x += (VEL * math.cos(self.to_radian(self.rot))) if 0 < (self.x +
VEL * math.cos(self.to_radian(self.rot))) < WIDTH else 0
self.y -= (VEL * math.sin(self.to_radian(self.rot))) if 0 < (self.y -
VEL * math.sin(self.to_radian(self.rot))) < HEIGHT else 0
if keys_pressed[pygame.K_s]:
self.currentimage -= 1
self.x -= (VEL * math.cos(self.to_radian(self.rot))) if 0 < (self.x -
VEL * math.cos(self.to_radian(self.rot))) < WIDTH else 0
self.y += (VEL * math.sin(self.to_radian(self.rot))) if 0 < (self.y +
VEL * math.sin(self.to_radian(self.rot))) < HEIGHT else 0
self.rect.center = (self.x, self.y)
for wall in walls:
if self.rect.colliderect(wall):
self.x, self.y = initialx, initialy
def update(self, keys_pressed, WIDTH, HEIGHT, VEL):
prev = self.rot
self.originalImage = self.sprites[self.currentimage%len(self.sprites)]
self.image = pygame.transform.rotate(
self.originalImage, self.rot)
self.move(keys_pressed, WIDTH, HEIGHT, VEL, prev)