How can I combine these two segments of code so I have a game where the enemy chases the player through the maze that’s generated

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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()

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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()

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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()
</code>
<code>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() </code>
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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`
</code>
<code>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` </code>
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

New contributor

user25482109 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật