-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
212 lines (178 loc) · 8.86 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Pygame recreation in pygame with extra features
#imports
from board import boards #import tilemaps from board file
import pygame
import math
pygame.init()
game_version = 2
game_icon = pygame.image.load('assets/player_images/1.png')
#Window options
WIDTH = 900
HEIGHT = 950 # 50 extra pixels, so that there is space for amount of coins & lives.
fps = 60
pygame.display.set_icon(game_icon)
pygame.display.set_caption(f'Pacman - Version {game_version}')
font = pygame.font.Font('freesansbold.ttf', 20) # set font
screen = pygame.display.set_mode([WIDTH, HEIGHT]) # set screen size
timer = pygame.time.Clock()
PI = math.pi # sets pi as a variable, so that it can be easily called in future without having to use math.pi
colour = 'blue' #colour for all of the tilemap shapes. (excl powerups & coins.)
player_images = []
for i in range(1,5):
player_images.append(pygame.transform.scale(pygame.image.load(f'assets/player_images/{i}.png'), (45,45)))
player_x = 450 # Player starting position x
player_y = 663# Player starting position y
direction = 0 # Player starting direction (right)
counter = 0
flicker = False # flicker for powerup tiles
valid_turns = [False, False, False, False] #R,L,U,D
direction_command = 0
player_speed = 2
#function for drawing the player sprite
def drawPlayer():
if direction == 0:
screen.blit(player_images[counter // 5], (player_x, player_y)) # RIGHT
elif direction == 1:
screen.blit(pygame.transform.flip(player_images[counter // 5], True, False), (player_x, player_y)) # LEFT
elif direction == 2:
screen.blit(pygame.transform.rotate(player_images[counter // 5], 90), (player_x, player_y)) # UP
elif direction == 3:
screen.blit(pygame.transform.rotate(player_images[counter // 5], 270), (player_x, player_y)) # DOWN
#function for drawing the tilemap board
level = boards
def drawBoard():
num1 = ((HEIGHT - 50) // 32) #to leave space for the score & number of lives remaining.
num2 = (WIDTH // 30)
# the two above are using floor division, so that the output will always be an integer
for i in range(len(level)):
for j in range(len(level[i])):
if level[i][j] == 1:
pygame.draw.circle(screen, 'white', (j * num2 + (0.5*num2), i * num1 + (0.5*num1)), 4) #draw small circles (coins)
if level[i][j] == 2 and not flicker:
pygame.draw.circle(screen, 'gold', (j * num2 + (0.5*num2), i * num1 + (0.5*num1)), 10) #draw large circles (powerups)
if level[i][j] == 3:
pygame.draw.line(screen, colour, (j * num2 + (0.5*num2), i * num1), (j * num2 + (0.5*num2), i * num1 + num1), 3) # draw vertical walls
if level[i][j] == 4:
pygame.draw.line(screen, colour, (j * num2 , i * num1 + (0.5*num1)), (j * num2 + num2, i * num1 + (0.5*num1)), 3) # draw horizontal walls
if level[i][j] == 5:
pygame.draw.arc(screen, colour, [(j*num2 - (num2*0.4)) - 2, (i * num1 + (0.5*num1)), num2, num1], 0, PI/2, 3) # draw curve for corners
if level[i][j] == 6:
pygame.draw.arc(screen, colour, [(j*num2 + (num2*0.5)), (i * num1 + (0.5*num1)), num2, num1], PI/2, PI, 3) # draw curve for corners
if level[i][j] == 7:
pygame.draw.arc(screen, colour, [(j * num2 + (num2 * 0.5)), (i * num1 - (0.4 * num1)), num2, num1], PI, 3 * PI / 2, 3) # draw curve for corners
if level[i][j] == 8:
pygame.draw.arc(screen, colour, [(j * num2 - (num2 * 0.4)) - 2, (i * num1 - (0.4 * num1)), num2, num1], 3 * PI / 2, 2 * PI, 3) # draw curve for corners
if level[i][j] == 9:
pygame.draw.line(screen, 'white', (j * num2 , i * num1 + (0.5*num1)), (j * num2 + num2, i * num1 + (0.5*num1)), 3) # draw horizontal walls (ghost area gate)
#collision checker - spots direct near open spaces in all directions of the player sprite
def checkPosition(centerx, centery):
turns = [False, False, False, False] #R,L,U,D
num1 = (HEIGHT-50)//32
num2 = (WIDTH//30)
num3 = 15
#check collisions based on center x and center y of player + or - num3
if centerx // 30 < 29:
if direction == 0:
if level[centery//num1][(centerx - num3)//num2] <3:
turns[1] = True
if direction == 1:
if level[centery//num1][(centerx + num3)//num2] <3:
turns[0] = True
if direction == 2:
if level[(centery+num3)//num1][centerx//num2] <3:
turns[3] = True
if direction == 3:
if level[(centery-num3)//num1][centerx//num2] <3:
turns[2] = True
if direction == 0 or direction == 1: # check for moving left or right
if 12 <= centerx % num2 <= 18: # if centerx mod num2 between 12 and 18, rough midpoint of tile - to calculate above or below
if level[(centery + num1) //num1][centerx //num2] < 3: # if position directly below open
turns[3] = True
if level[(centery - num1) //num1][centerx //num2] < 3: # if position directly above open
turns[2] = True
if 12 <= centery % num1 <= 18: # if centerx mod num2 between 12 and 18, rough midpoint of tile - to calculate left or right
if level[centery//num1][(centerx - num3) //num2] < 3: # if position directly left open
turns[1] = True
if level[centery//num1][(centerx + num3) //num2] < 3: # if position directly right open
turns[0] = True
if direction == 2 or direction == 3: # check for moving up or down
if 12 <= centerx % num2 <= 18: # if centerx mod num2 between 12 and 18, rough midpoint of tile - to calculate above or below
if level[(centery + num3) //num1][centerx //num2] < 3: # if position directly below open
turns[3] = True
if level[(centery - num3) //num1][centerx //num2] < 3: # if position directly above open
turns[2] = True
if 12 <= centery % num1 <= 18: # if centerx mod num2 between 12 and 18, rough midpoint of tile - to calculate left or right
if level[centery//num1][(centerx - num2) //num2] < 3: # if position directly left open
turns[1] = True
if level[centery//num1][(centerx + num2) //num2] < 3: # if position directly right open
turns[0] = True
else:
turns[0] = True
turns[1] = True
return turns
def movePlayer(play_x, play_y):
#r,l,u,d
if direction == 0 and valid_turns[0]:
play_x += player_speed
elif direction == 1 and valid_turns[1]:
play_x -= player_speed
if direction == 2 and valid_turns[2]:
play_y -= player_speed
elif direction == 3 and valid_turns[3]:
play_y += player_speed
return play_x, play_y
#game loop
run = True
while run:
timer.tick(fps)
if counter < 19:
counter += 1
if counter > 3:
flicker = False
else:
counter = 0
flicker = True
screen.fill('black')
drawBoard()
drawPlayer()
center_x = player_x + 23
center_y = player_y + 24
valid_turns = checkPosition(center_x,center_y) # calls checkPosition, checks for valid turn and passes the center point for the player sprite
player_x, player_y = movePlayer(player_x,player_y)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN: #Register direction changes with arrow keys
if event.key == pygame.K_RIGHT:
direction_command = 0
if event.key == pygame.K_LEFT:
direction_command = 1
if event.key == pygame.K_UP:
direction_command = 2
if event.key == pygame.K_DOWN:
direction_command = 3
if event.type == pygame.KEYUP: #Register direction changes with arrow keys
if event.key == pygame.K_RIGHT and direction_command == 0:
direction_command = direction
if event.key == pygame.K_LEFT and direction_command == 1:
direction_command = direction
if event.key == pygame.K_UP and direction_command == 2:
direction_command = direction
if event.key == pygame.K_DOWN and direction_command == 3:
direction_command = direction
if event.key == pygame.K_ESCAPE: #Register game quit with escape
pygame.quit()
if direction_command == 0 and valid_turns[0]:
direction = 0
if direction_command == 1 and valid_turns[1]:
direction = 1
if direction_command == 2 and valid_turns[2]:
direction = 2
if direction_command == 3 and valid_turns[3]:
direction = 3
if player_x > 900:
player_x = -47
elif player_x < -50:
player_x = 897
pygame.display.flip()
pygame.quit()