-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsprites.py
More file actions
114 lines (99 loc) · 3.59 KB
/
sprites.py
File metadata and controls
114 lines (99 loc) · 3.59 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
import pygame as pg
from definitions import *
# player sprite
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 = pg.Surface((TILESIZE, TILESIZE))
self.image.fill(BLACK)
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.moveCount = 0
def move(self, X=0, Y=0):
if not self.wall_collision(X, Y):
Trail(self.game, self.x, self.y) # shows trail of current path
self.x += X
self.y += Y
self.moveCount += 1
# checks for walls and also map boundaries
def wall_collision(self, X=0, Y=0):
for wall in self.game.walls:
if (wall.x == self.x + X and wall.y == self.y + Y or
self.x + X < 0 or self.y + Y < 0 or (self.x + X) >
(WIDTH/TILESIZE)-1 or (self.y + Y) > (HEIGHT/TILESIZE)-1):
return True
return False
def update(self):
self.rect.x = self.x * TILESIZE
self.rect.y = self.y * TILESIZE
# computer player sprite
class Computer(Player):
def __init__(self, game, x, y, path_lst):
self.groups = game.all_sprites
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = pg.Surface((TILESIZE, TILESIZE))
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.path = path_lst
self.i = 1
self.moveCount = 0
def move(self):
if not self.wall_collision(self.path[self.i][1], self.path[self.i][0]):
Trail(self.game, self.x, self.y) # shows trail of current path
self.x = self.path[self.i][1]
self.y = self.path[self.i][0]
self.i += 1
self.moveCount += 1
# checks for walls and also map boundaries for computer movements
def wall_collision(self, X=0, Y=0):
for wall in self.game.walls:
if (wall.x == X and wall.y == Y or
X < 0 or Y < 0 or X >
(WIDTH/TILESIZE)-1 or Y > (HEIGHT/TILESIZE)-1):
return True
return False
# "wall" sprite
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 = pg.Surface((TILESIZE, TILESIZE))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.rect.x = x * TILESIZE
self.rect.y = y * TILESIZE
# trail sprite
class Trail(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites, game.trails
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = pg.Surface((TILESIZE, TILESIZE))
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.rect.x = x * TILESIZE
self.rect.y = y * TILESIZE
# finish line sprite
class End(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites, game.finish
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = pg.Surface((TILESIZE, TILESIZE))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.rect.x = x * TILESIZE
self.rect.y = y * TILESIZE