-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsprites.py
More file actions
103 lines (92 loc) · 3.76 KB
/
sprites.py
File metadata and controls
103 lines (92 loc) · 3.76 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
import pygame as pg
from settings import *
from tilemap import collide_hit_rect
vec = pg.math.Vector2
class Player(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = game.player_img
self.rect = self.image.get_rect()
self.hit_rect = PLAYER_HIT_RECT
self.hit_rect.center = self.rect.center
self.vel = vec(x=0, y=0)
self.pos = vec(x=x, y=y) * TILESIZE
self.rot = 0
def get_keys(self):
self.rot_speed = 0
self.vel = vec(x=0, y=0)
keys = pg.key.get_pressed()
if keys[pg.K_LEFT] or keys[pg.K_a]:
self.rot_speed = PLAYER_ROT_SPEED
if keys[pg.K_RIGHT] or keys[pg.K_d]:
self.rot_speed = -PLAYER_ROT_SPEED
if keys[pg.K_UP] or keys[pg.K_w]:
self.vel = vec(x=PLAYER_SPEED, y=0).rotate(-self.rot)
if keys[pg.K_DOWN] or keys[pg.K_s]:
self.vel = vec(x=-PLAYER_SPEED / 2, y=0).rotate(-self.rot)
def collide_with_walls(self, dir):
if dir == 'x':
hits = pg.sprite.spritecollide(self, self.game.walls, False, collide_hit_rect)
if hits:
if self.vel.x > 0:
self.pos.x = hits[0].rect.left - self.hit_rect.width / 2
if self.vel.x < 0:
self.pos.x = hits[0].rect.right + self.hit_rect.width / 2
self.vel.x = 0
self.hit_rect.centerx = self.pos.x
if dir == 'y':
hits = pg.sprite.spritecollide(self, self.game.walls, False, collide_hit_rect)
if hits:
if self.vel.y > 0:
self.pos.y = hits[0].rect.top - self.hit_rect.height / 2
if self.vel.y < 0:
self.pos.y = hits[0].rect.bottom + self.hit_rect.height / 2
self.vel.y = 0
self.hit_rect.centery = self.pos.y
def update(self):
self.get_keys()
self.rot = (self.rot + self.rot_speed * self.game.dt) % 360
self.image = pg.transform.rotate(self.game.player_img, self.rot)
self.rect = self.image.get_rect()
self.rect.center = self.pos
self.pos += self.vel * self.game.dt
self.hit_rect.centerx = self.pos.x
self.collide_with_walls('x')
self.hit_rect.centery = self.pos.y
self.collide_with_walls('y')
self.rect.center = self.hit_rect.center
class Mob(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites, game.mobs
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = game.mob_img
self.rect = self.image.get_rect()
self.pos = vec(x=x, y=y) * TILESIZE
self.vel = vec(x=0, y=0)
self.acc = vec(x=0, y=0)
self.rect.center = self.pos
self.rot = 0
def update(self):
self.rot = (self.game.player.pos - self.pos).angle_to(vec(x=1, y=0))
self.image = pg.transform.rotate(self.game.mob_img, self.rot)
self.rect = self.image.get_rect()
self.rect.center = self.pos
self.acc = vec(x=MOB_SPEED, y=0).rotate(-self.rot)
self.acc += self.vel * -1
self.vel += self.acc * self.game.dt
self.pos += self.vel * self.game.dt * 0.5 * self.acc * self.game.dt ** 2
self.rect.center = self.pos
class Wall(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites, game.walls
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = game.wall_img
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.rect.x = x * TILESIZE
self.rect.y = y * TILESIZE