I want to combine these two programs into one game but I keep getting errors when I try
main.py ”
`import pygame
import sys
MAZE_WIDTH = 800
MAZE_HEIGHT = 600
class Player:
def
init
(self):
self.pos = [0, MAZE_HEIGHT // 2] # Initialize player at the bottom of the maze
self.speed = 5
def move(self, dx, dy):
if self.pos[0] + dx >= 0 and self.pos[0] + dx < MAZE_WIDTH - 1:
self.pos[0] += dx
if self.pos[1] + dy >= 0 and self.pos[1] + dy < MAZE_HEIGHT - 2:
self.pos[1] += dy
class Enemy: def __init__(self): self.pos = [MAZE_WIDTH - 1, MAZE_HEIGHT // 2] # Initialize enemy self.speed = 3
def move(self, player_pos):
dx = player_pos[0] - self.pos[0]
dy = player_pos[1] - self.pos[1]
if abs(dx) > abs(dy):
if dx > 0:
self.pos[0] += self.speed
else:
self.pos[0] -= self.speed
elif dy != 0:
if dy > 0:
self.pos[1] += self.speed
else:
self.pos[1] -= self.speed
class Maze:
def init(self):
self.player = Player()
self.enemy = Enemy()
def update_maze(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.player.move(0, -1)
moving_up = True
elif event.key == pygame.K_DOWN:
self.player.move(0, 1)
moving_down = True
elif event.key == pygame.K_LEFT:
self.player.move(-1, 0)
moving_left = True
elif event.key == pygame.K_RIGHT:
self.player.move(1, 0)
moving_right = True
if moving_up and not moving_down:
self.player.pos[1] -= self.player.speed
elif moving_down and not moving_up:
self.player.pos[1] += self.player.speed
elif moving_left and not moving_right:
self.player.pos[0] -= self.player.speed
elif moving_right and not moving_left:
self.player.pos[0] += self.player.speed
if abs(self.enemy.pos[0] - self.player.pos[0]) < 10 and abs(self.enemy.pos[1] - self.player.pos[1]) < 10:
print("Game Over")
pygame.quit()
sys.exit()
def draw_maze(self, screen):
pygame.draw.rect(screen, (255, 255, 255), (0, 0, MAZE_WIDTH, MAZE_HEIGHT))
pygame.draw.circle(screen, (255, 0, 0), tuple(self.player.pos), 5) # Draw player
pygame.draw.circle(screen, (0, 0, 255), tuple(self.enemy.pos), 5) # Draw enemy
def main(): pygame.init() screen = pygame.display.set_mode((MAZE_WIDTH, MAZE_HEIGHT)) clock = pygame.time.Clock()
maze = Maze()
player = maze.player
enemy = maze.enemy
moving_up = False
moving_down = False
moving_left = False
moving_right = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
moving_up = True
elif event.key == pygame.K_DOWN:
moving_down = True
elif event.key == pygame.K_LEFT:
moving_left = True
elif event.key == pygame.K_RIGHT:
moving_right = True
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
player.move(0, -5)
moving_up = True
elif keys[pygame.K_DOWN]:
player.move(0, 5)
moving_down = True
elif keys[pygame.K_LEFT]:
player.move(-5, 0)
moving_left = True
elif keys[pygame.K_RIGHT]:
player.move(5, 0)
moving_right = True
if not keys[pygame.K_UP] and moving_up:
moving_up = False
if not keys[pygame.K_DOWN] and moving_down:
moving_down = False
if not keys[pygame.K_LEFT] and moving_left:
moving_left = False
if not keys[pygame.K_RIGHT] and moving_right:
moving_right = False
enemy.move(player.pos)
screen.fill((0, 0, 0)) # Clear the screen
maze.draw_maze(screen)
pygame.display.flip()
clock.tick(60)
if abs(enemy.pos[0] - player.pos[0]) < 10 and abs(enemy.pos[1] - player.pos[1]) < 10:
print("Game Over")
pygame.quit()
sys.exit()
if
name
== "
main
":
main()
and the second file gen.py
from random import choice
import pygame
RES = WIDTH, HEIGHT = 1202, 902
TILE = 100
cols, rows = WIDTH // TILE, HEIGHT // TILE
class Cell:
def
init
(self, x, y):
self.x, self.y = x, y
self.walls = {'top': True, 'right': True, 'bottom': True, 'left': True}
self.visited = False
self.thickness = 4
def draw(self, sc):
x, y = self.x * TILE, self.y * TILE
if self.walls['top']:
pygame.draw.line(sc, pygame.Color('darkorange'), (x, y), (x + TILE, y), self.thickness)
if self.walls['right']:
pygame.draw.line(sc, pygame.Color('darkorange'), (x + TILE, y), (x + TILE, y + TILE), self.thickness)
if self.walls['bottom']:
pygame.draw.line(sc, pygame.Color('darkorange'), (x + TILE, y + TILE), (x , y + TILE), self.thickness)
if self.walls['left']:
pygame.draw.line(sc, pygame.Color('darkorange'), (x, y + TILE), (x, y), self.thickness)
def get_rects(self):
rects = []
x, y = self.x * TILE, self.y * TILE
if self.walls['top']:
rects.append(pygame.Rect( (x, y), (TILE, self.thickness) ))
if self.walls['right']:
rects.append(pygame.Rect( (x + TILE, y), (self.thickness, TILE) ))
if self.walls['bottom']:
rects.append(pygame.Rect( (x, y + TILE), (TILE , self.thickness) ))
if self.walls['left']:
rects.append(pygame.Rect( (x, y), (self.thickness, TILE) ))
return rects
def check_cell(self, x, y):
find_index = lambda x, y: x + y * cols
if x < 0 or x > cols - 1 or y < 0 or y > rows - 1:
return False
return self.grid_cells[find_index(x, y)]
def check_neighbors(self, grid_cells):
self.grid_cells = grid_cells
neighbors = []
top = self.check_cell(self.x, self.y - 1)
right = self.check_cell(self.x + 1, self.y)
bottom = self.check_cell(self.x, self.y + 1)
left = self.check_cell(self.x - 1, self.y)
if top and not top.visited:
neighbors.append(top)
if right and not right.visited:
neighbors.append(right)
if bottom and not bottom.visited:
neighbors.append(bottom)
if left and not left.visited:
neighbors.append(left)
return choice(neighbors) if neighbors else False
def remove_walls(current, next):
dx = current.x - next.x
if dx == 1:
current.walls['left'] = False
next.walls['right'] = False
elif dx == -1:
current.walls['right'] = False
next.walls['left'] = False
dy = current.y - next.y
if dy == 1:
current.walls['top'] = False
next.walls['bottom'] = False
elif dy == -1:
current.walls['bottom'] = False
next.walls['top'] = False
def generate_maze():
grid_cells = [Cell(col, row) for row in range(rows) for col in range(cols)]
current_cell = grid_cells[0]
array = []
break_count = 1
while break_count != len(grid_cells):
current_cell.visited = True
next_cell = current_cell.check_neighbors(grid_cells)
if next_cell:
next_cell.visited = True
break_count += 1
array.append(current_cell)
remove_walls(current_cell, next_cell)
current_cell = next_cell
elif array:
current_cell = array.pop()
return grid_cells`
Ive tried calling the maze function but I keep getting an error “AttributeError: ‘list’ object has no attribute ‘player'” and when I go to fix that I get more errors after, so I know I’m not doing something right lol.
*sorry for the format it didn’t format correctly for some reason
user25482109 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.