-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
182 lines (147 loc) · 6.54 KB
/
main.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import pygame
from math import sqrt
from constants import *
from player import Player
from ghosts.ghost import Ghost
from ghosts.vec2int import Vec2Int
"""
from food import Food
from wall import Wall
"""
vec = pygame.math.Vector2
# PAC_MAN = pygame.image.load(os.path.join('Assets', 'pacman.png')) TODO
# BLUE_GHOST = pygame.image.load(os.path.join('Assets', 'blueghost.png')) TODO
# RED_GHOST = pygame.image.load(os.path.join('Assets', 'redghost.png')) TODO
# ORANGE_GHOST = pygame.image.load(os.path.join('Assets', 'orangeghost.png')) TODO
# PINK_GHOST = pygame.image.load(os.path.join('Assets', 'pinkghost.png')) TODO
class Game(object):
def init(self):
pygame.init()
self.font = pygame.font.SysFont("Comic Sans MS", 30)
self.screen = pygame.display.set_mode(SCREENSIZE, 0, 32)
self.background = None
self.state = 'start'
self.running = True
self.grid = PACMAN_GRID
self.game_objects = []
self.player = Player(self.grid, self.init_pacman())
self.ghost = Ghost(self.grid, Vec2Int(TILELENGTH, TILELENGTH))
def run(self):
self.clock = pygame.time.Clock()
self.setBackground()
while self.running:
self.clock.tick(FPS)
if self.state == 'start':
self.check_events()
self.render_start()
elif self.state == 'playing':
self.update(1)
self.render_play()
self.check_events()
elif self.state == 'paused':
self.check_events()
pygame.display.update()
pygame.quit()
def setBackground(self):
self.background = pygame.surface.Surface(SCREENSIZE).convert()
self.background.fill(BLACK)
def init_pacman(self):
for x in range(COLS):
for y in range(ROWS):
if self.grid[y][x] == 2:
return vec(y, x)
"""
borde väl fungera som initiering? får circular import så vette tusan
def init_grid(self):
for x in range(COLS):
for y in range(ROWS):
if self.grid[y][x] == WALL:
self.game_objects.append(Wall(self.grid, vec(y,x)))
if self.grid[y][x] == PACMAN:
self.game_objects.append(self.player)
if self.grid[y][x] == CANDY:
self.game_objects.append(Food(self.grid, vec(y,x)))
if self.grid[y][x] == SUPER_CANDY:
self.game_objects.append(Food(self.grid, vec(y,x)))
if self.grid[y][x] == GHOST_BASIC:
self.game_objects.append(Ghost(self.grid, vec(y,x)))
if self.grid[y][x] == GHOST_RED:
self.game_objects.append(Ghost(self.grid, vec(y,x)))
if self.grid[y][x] == GHOST_CYAN:
self.game_objects.append(Ghost(self.grid, vec(y,x)))
if self.grid[y][x] == GHOST_PINK:
self.game_objects.append(Ghost(self.grid, vec(y,x)))
if self.grid[y][x] == GHOST_ORANGE:
self.game_objects.append(Ghost(self.grid, vec(y,x)))
"""
def update(self, delta_time: float):
self.player.update()
self.grid = self.player.grid
self.ghost.update(delta_time, Vec2Int(self.player.grid_position.x, self.player.grid_position.y))
self.check_gameover()
def check_gameover(self):
#if self.player.grid_position.x - 0.5 == self.ghost.get_grid_pos().x and self.player.grid_position.y - 0.5 == self.ghost.get_grid_pos().y:
# exit()
if self.coll_circ_rect(self.player.radius, self.player.pxl_pos, pygame.Rect(self.ghost.pos.x, self.ghost.pos.y, self.ghost.side_size, self.ghost.side_size)):
self.state = 'paused'
def check_events(self):
for event in pygame.event.get():
# Exit event
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
exit()
# Pause event
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
if self.state == "playing":
self.state = 'paused'
else:
self.state = 'playing'
def draw_grid(self):
for x in range(COLS):
for y in range(ROWS):
pygame.draw.rect(self.screen, GREY, (x*TILELENGTH, y*TILELENGTH,
TILELENGTH, TILELENGTH), 1)
if self.grid[y][x] == 1:
pygame.draw.rect(self.screen, BLUE, (x*TILELENGTH, y*TILELENGTH,
TILELENGTH, TILELENGTH))
# Draw Candy
if self.grid[y][x] == 3:
pygame.draw.circle(self.screen, WHITE, (x*TILELENGTH + TILELENGTH/2, y*TILELENGTH + TILELENGTH/2), TILELENGTH / 6)
# Draw super candy
if self.grid[y][x] == 4:
pygame.draw.circle(self.screen, YELLOW, (x*TILELENGTH + TILELENGTH/2, y*TILELENGTH + TILELENGTH/2), TILELENGTH / 3)
def render_play(self):
self.screen.blit(self.background, (0, 0))
self.draw_grid()
self.player.render(self.screen)
self.ghost.draw(self.screen)
text = self.font.render("SCORE: " + str(self.player.score), False, WHITE)
self.screen.blit(text, (SCREENWIDTH/18, 10.65*SCREENHEIGHT/12))
def render_start(self):
text = self.font.render("PRESS SPACE BAR", False, CERISE)
self.screen.blit(text, (SCREENWIDTH/6, SCREENHEIGHT/2))
def coll_circ_rect(self, radius, pos: vec, rect: pygame.Rect):
testX = pos.x
testY = pos.y
# Check if circle is closest to LEFT side of rectangle
if pos.x < rect.x:
testX = rect.x
# Check if circle is closest to RIGHT side of rectangle
elif pos.x > rect.x + rect.width:
testX = rect.x + rect.width
# Check if circle is closest to TOP side of rectangle
if pos.y < rect.y:
testY = rect.y
# Check if circle is closest to BOTTOM side of rectangle
elif pos.y > rect.y + rect.height:
testY = rect.y + rect.height
# Calculate distance from closest sides to circle
distX = pos.x - testX
distY = pos.y - testY
dist = sqrt((distX * distX) + (distY * distY))
if dist < radius:
return True
return False
if __name__ == "__main__":
game = Game()
game.init()
game.run()