-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.py
More file actions
139 lines (118 loc) · 4.7 KB
/
Copy pathclasses.py
File metadata and controls
139 lines (118 loc) · 4.7 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
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
import pygame as pg
from constants import *
wall_group = pg.sprite.Group()
coin_group = pg.sprite.Group()
ghost_group = pg.sprite.Group()
class GameSprite(pg.sprite.Sprite):
def __init__(self, x, y, width, height, filename):
super().__init__()
image = pg.image.load(filename)
self.image = pg.transform.scale(image, (width, height))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Player(GameSprite):
def __init__(self, x, y, filename, width, height, step):
super().__init__(x, y, width, height, filename)
self.step = step
self.points = 0
def update(self, surface):
x, y = 0, 0
keys = pg.key.get_pressed()
if keys[RIGHT]:
x += self.step
elif keys[LEFT]:
x -= self.step
if keys[UP]:
y -= self.step
elif keys[DOWN]:
y += self.step
for w in wall_group:
if w.rect.colliderect(self.rect.x + x, self.rect.y, self.rect.width, self.rect.height):
if x > 0:
x = w.rect.left - self.rect.right
elif x < 0:
x = w.rect.right - self.rect.left
if w.rect.colliderect(self.rect.x, self.rect.y + y, self.rect.width, self.rect.height):
if y > 0:
y = w.rect.top - self.rect.bottom
elif y < 0:
y = w.rect.bottom - self.rect.top
self.rect.x += x
self.rect.y += y
if pg.sprite.spritecollide(self, coin_group, True):
self.points += 1
surface.blit(self.image, self.rect)
if pg.sprite.spritecollide(self, ghost_group, False):
print(pg.sprite.spritecollide(self, ghost_group, False))
return 'finish'
return 'game'
class Ghost(GameSprite):
def __init__(self, x, y, filename, width, height, step):
super().__init__(x, y, width, height, filename)
self.step = step
def update(self, surface, goal):
x, y = 0, 0
if self.rect.x < goal.rect.x:
x += self.step
elif self.rect.x > goal.rect.x:
x -= self.step
if self.rect.y < goal.rect.y:
y += self.step
elif self.rect.y > goal.rect.y:
y -= self.step
for w in wall_group:
if w.rect.colliderect(self.rect.x + x, self.rect.y, self.rect.width, self.rect.height):
if x > 0:
x = w.rect.left - self.rect.right
elif x < 0:
x = w.rect.right - self.rect.left
elif w.rect.colliderect(self.rect.x, self.rect.y + y, self.rect.width, self.rect.height):
if y > 0:
y = w.rect.top - self.rect.bottom
elif y < 0:
y = w.rect.bottom - self.rect.top
self.rect.x += x
self.rect.y += y
surface.blit(self.image, self.rect)
class World:
def __init__(self, filename):
self.map = filename
self.list_map = []
def load_map(self):
images = [('1', 'images/wall.png'), ('2', 'images/point.png')]
with open(self.map, 'r', encoding='utf-8') as file:
row = 0
for line in file:
column = 0
for tile in line:
if tile == '1' or tile == '2':
cell = GameSprite(column * TILE_SIZE, row * TILE_SIZE, TILE_SIZE, TILE_SIZE, images[int(tile) - 1][1])
if tile == '1':
wall_group.add(cell)
elif tile == '2':
coin_group.add(cell)
column += 1
row += 1
class Button:
def __init__(self, x, y, width, height, text, cur_mode, click_mode):
self.rect = pg.Rect(x, y, width, height)
self.image = pg.font.SysFont('Verdana', int(height // 2), ORANGE).render(text, False, ORANGE)
self.rect_image = self.image.get_rect()
self.cur_mode = cur_mode
self.click_mode = click_mode
def draw(self, surface):
pg.draw.rect(surface, WHITE, self.rect, border_radius=int(self.rect.width * 0.25))
surface.blit(self.image, (self.rect.x + (self.rect.width - self.rect_image.width) / 2,
self.rect.y + (self.rect.height - self.rect_image.height) / 2))
def check_click(self):
click = pg.mouse.get_pressed()
if click[0]:
x, y = pg.mouse.get_pos()
print(x, y)
if self.rect.collidepoint(x, y):
return self.click_mode
return self.cur_mode
def update(self, surface):
self.draw(surface)
return self.check_click()