-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
163 lines (123 loc) · 5.69 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
# Setup Python ----------------------------------------------- #
import pygame
import sys
import os
import world as wo
import menu as me
# Setup pygame/window ---------------------------------------- #
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (100, 32) # windows position
pygame.init()
pygame.display.set_caption('Pac Man')
SCREEN_WIDTH = 798
SCREEN_HEIGHT = 882
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32)
mainClock = pygame.time.Clock()
# Sprites ------------------------------------------------------- #
# load the sprites
sprites = {"pac_man": {"moving": {}}, "ghosts": {}}
sprites_path = "Assets/Sprites"
# pac man
sprites["pac_man"]["death"] = [pygame.image.load(f"{sprites_path}/pac_man/death/deathPac{nb}.png") for nb in range(11)]
sprites["pac_man"]["moving"]["right"] = [pygame.image.load(f"{sprites_path}/pac_man/moving/moving{nb}.png") for nb in
range(3)]
sprites["pac_man"]["moving"]["left"] = [pygame.transform.rotate(sprite, 180) for sprite in
sprites["pac_man"]["moving"]["right"]]
sprites["pac_man"]["moving"]["up"] = [pygame.transform.rotate(sprite, 90) for sprite in
sprites["pac_man"]["moving"]["right"]]
sprites["pac_man"]["moving"]["down"] = [pygame.transform.rotate(sprite, -90) for sprite in
sprites["pac_man"]["moving"]["right"]]
# ghosts
for ghost_name in ("blinky-red", "clyde-orange", "inky-turquoise", "pinky-pink"):
sprites["ghosts"][ghost_name] = {}
for direction in ("down", "right", "left", "up"):
dir_prefix = direction[0]
sprites["ghosts"][ghost_name][direction] = [
pygame.image.load(f"{sprites_path}/ghosts/{ghost_name}/{dir_prefix}{nb}.png") for nb in range(2)]
# blue version
sprites["ghosts"][ghost_name]["chased"] = [pygame.image.load(f"{sprites_path}/ghosts/chased/chased{nb}.png") for nb
in range(2)]
sprites["ghosts"][ghost_name]["chased_white"] = [pygame.image.load(f"{sprites_path}/ghosts/chased/chased{nb}.png")
for nb in range(2, 4)]
# Sounds -----------------------------------------------------#
sounds = {"theme_music_remix": pygame.mixer.Sound(f"Assets/Sounds/theme_music_remix.wav"),
"beginning": pygame.mixer.Sound(f"Assets/Sounds/beginning.wav"),
"death": pygame.mixer.Sound(f"Assets/Sounds/death.wav"),
"chomp": pygame.mixer.Sound(f"Assets/Sounds/chomp.wav"),
"ghost_moving": pygame.mixer.Sound(f"Assets/Sounds/ghost_moving.wav")
}
sounds["theme_music_remix"].set_volume(0.17)
sounds["theme_music_remix"].play(-1)
sounds["beginning"].set_volume(0.18)
# change the volume of the music
sounds["chomp"].set_volume(0.18)
sounds["ghost_moving"].set_volume(0.18)
# Fonts ------------------------------------------------------- #
main_font = pygame.font.SysFont("coopbl", 22)
# Variables ------------------------------------------------------- #
start_level = 1 # the user will start at the level X
game_state = "menu"
# score for leaderboard
with open("score.txt", 'r') as file: # open and read the score file
all_score = [score.split("_,_") for score in file.read().splitlines()] # make a list with all scores
for score in all_score:
score[0] = int(score[0]) # make the score number an integer
# Constants -------------------------------------------------------#
BG_COLOR = (11, 11, 11)
# Classes --------------------------------------------------------- #
# Creation ---------------------------------------------------------#
menu = me.Menu(SCREEN)
# Functions ------------------------------------------------------- #
def write_score(score, name):
"""write the score of the player in the score text file"""
with open("score.txt", 'a') as score_file:
text = f"{score}_,_{name}"
score_file.write(text + "\n")
def redraw():
SCREEN.fill(BG_COLOR)
""" draw and make the game functional"""
global game_state, world
# when the user is in the menu
if game_state == "menu":
if menu.do(key, all_score):
game_state = "game"
world = wo.World(SCREEN, sprites, sounds, start_level, BG_COLOR, menu.user_name)
menu.reset()
sounds["theme_music_remix"].stop()
elif game_state == "game":
if world.do() == "end":
sounds["theme_music_remix"].play(-1)
game_state = "menu"
menu.menu_state = "leaderboard"
write_score(world.player.score, world.user_name)
all_score.append([world.player.score, world.user_name])
print(all_score)
fps_label = main_font.render(f"FPS: {int(mainClock.get_fps())}", 1, (255, 200, 20))
SCREEN.blit(fps_label, (5, 5))
def buttons():
global key
key = None
for event in pygame.event.get():
if event.type == pygame.QUIT: # if the user want to close the game
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
key = event.unicode
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == pygame.K_BACKSPACE:
key = "delete_key"
def update():
pygame.display.update()
mainClock.tick(90)
def main():
# Loop ------------------------------------------------------- #
while True:
# Buttons ------------------------------------------------ #
buttons()
# draw --------------------------------------------- #
redraw()
# Update ------------------------------------------------- #
update()
if __name__ == '__main__':
main()