Skip to content

Commit

Permalink
Fix: Updated python game logic (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangsonww committed May 5, 2024
1 parent c7fc618 commit 1acd5bf
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ However, creating it is a pain in the brain, so I hope you'll enjoy it! Star the
## Gameplay

<p align="center">
<img src="../utils/MazeUI.png" alt="Maze Game - Gameplay" width="100%" height="400">
<img src="../utils/MazeUI.png" alt="Maze Game - Gameplay" width="100%">
</p>

## Technologies Used
Expand Down
Empty file added .gitignore
Empty file.
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/The-Maze-Game.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 103 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<body>
<header>
<h1 style="margin-top: 250px">The Maze Game</h1>
<h1 style="margin-top: 450px">The Maze Game</h1>
<p>A simple yet fun game to play when you're bored.</p>
<strong><p id="lifetimeScore">Lifetime Score: 0</p></strong>
</header>
Expand Down
24 changes: 5 additions & 19 deletions src/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ body, html {

header {
margin: 20px 0;
text-align: center;
color: #fff;
}

.game-container {
Expand All @@ -44,6 +46,9 @@ header {
canvas {
max-width: 100%;
height: auto;
background-color: #fff;
display: block;
margin-bottom: 10px;
}

@media (max-width: 800px) {
Expand All @@ -53,19 +58,6 @@ canvas {
}
}

canvas {
background-color: #fff;
display: block;
margin-bottom: 10px;

}

.game-container {
display: flex;
flex-direction: column;
align-items: center;
}

#regenerateMaze {
font-family: "Poppins", sans-serif;
font-size: 16px;
Expand Down Expand Up @@ -138,12 +130,6 @@ canvas {
background-color: #ececec;
}

header {
text-align: center;
margin-bottom: 20px;
color: #fff;
}

header h1 {
margin: 0;
padding: 10px;
Expand Down
98 changes: 65 additions & 33 deletions src/python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
pygame.display.set_caption('Maze Game')
clock = pygame.time.Clock()


class Player:
def __init__(self, x, y):
self.rect = pygame.Rect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE)
Expand All @@ -28,6 +29,7 @@ def move(self, dx, dy):
self.rect.x += dx * CELL_SIZE
self.rect.y += dy * CELL_SIZE


def draw_maze(maze, player):
for y in range(len(maze)):
for x in range(len(maze[y])):
Expand All @@ -38,66 +40,95 @@ def draw_maze(maze, player):
pygame.draw.rect(screen, PATH_COLOR, (x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE))
pygame.draw.rect(screen, PLAYER_COLOR, player.rect)


def is_path(maze, x, y):
if 0 <= x < len(maze[0]) and 0 <= y < len(maze) and maze[y][x] == 'P':
return True
return False


def find_neighbors(x, y, width, height, maze):
"""Identify and return valid neighboring cells for maze generation."""
neighbors = []
if x > 1 and maze[y][x - 2] == 'W':
neighbors.append((x - 2, y))
if x < width - 2 and maze[y][x + 2] == 'W':
neighbors.append((x + 2, y))
if y > 1 and maze[y - 2][x] == 'W':
neighbors.append((x, y - 2))
if y < height - 2 and maze[y + 2][x] == 'W':
neighbors.append((x, y + 2))
return neighbors


def connect_cells(maze, cell, next_cell):
"""Connect the current cell to the chosen next cell in the maze."""
x, y = cell
nx, ny = next_cell
if nx == x:
maze[min(ny, y) + 1][x] = 'P'
else:
maze[y][min(nx, x) + 1] = 'P'


def generate_maze(width, height):
maze = [['W' for _ in range(width)] for _ in range(height)]
stack = [(1, 1)]

while stack:
cell = stack[-1]
x, y = cell
maze[y][x] = 'P' # Mark cell as path
neighbors = []

if x > 1 and maze[y][x - 2] == 'W':
neighbors.append((x - 2, y))
if x < width - 2 and maze[y][x + 2] == 'W':
neighbors.append((x + 2, y))
if y > 1 and maze[y - 2][x] == 'W':
neighbors.append((x, y - 2))
if y < height - 2 and maze[y + 2][x] == 'W':
neighbors.append((x, y + 2))
maze[y][x] = 'P'
neighbors = find_neighbors(x, y, width, height, maze)

if neighbors:
next_cell = random.choice(neighbors)
nx, ny = next_cell
if nx == x:
maze[min(ny, y) + 1][x] = 'P'
else:
maze[y][min(nx, x) + 1] = 'P'
connect_cells(maze, cell, next_cell)
stack.append(next_cell)
else:
stack.pop()

return maze


def process_events(player, maze):
"""Handle all Pygame events and return a boolean status for the game loop."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
if event.type == pygame.KEYDOWN:
if process_keydown(event.key, player, maze):
return False
return True


def process_keydown(key, player, maze):
"""Process keydown events and move the player if the path is valid."""
directions = {
pygame.K_LEFT: (-1, 0),
pygame.K_RIGHT: (1, 0),
pygame.K_UP: (0, -1),
pygame.K_DOWN: (0, 1),
}
if key in directions:
dx, dy = directions[key]
new_x = player.rect.x // CELL_SIZE + dx
new_y = player.rect.y // CELL_SIZE + dy
if is_path(maze, new_x, new_y):
player.move(dx, dy)


def main():
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()

maze = generate_maze(SCREEN_WIDTH // CELL_SIZE, SCREEN_HEIGHT // CELL_SIZE)
player = Player(1, 1)

running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if is_path(maze, player.rect.x // CELL_SIZE - 1, player.rect.y // CELL_SIZE):
player.move(-1, 0)
elif event.key == pygame.K_RIGHT:
if is_path(maze, player.rect.x // CELL_SIZE + 1, player.rect.y // CELL_SIZE):
player.move(1, 0)
elif event.key == pygame.K_UP:
if is_path(maze, player.rect.x // CELL_SIZE, player.rect.y // CELL_SIZE - 1):
player.move(0, -1)
elif event.key == pygame.K_DOWN:
if is_path(maze, player.rect.x // CELL_SIZE, player.rect.y // CELL_SIZE + 1):
player.move(0, 1)
running = process_events(player, maze)

screen.fill(BACKGROUND_COLOR)
draw_maze(maze, player)
Expand All @@ -107,5 +138,6 @@ def main():
pygame.quit()
sys.exit()


if __name__ == "__main__":
main()
Loading

0 comments on commit 1acd5bf

Please sign in to comment.