更新 2p

main
Vincents Xun 2024-04-26 07:16:58 +00:00
parent 676c64789d
commit fff106ad2d
1 changed files with 31 additions and 28 deletions

59
2p
View File

@ -158,8 +158,8 @@ def draw_maze(maze):
def draw_menu(): def draw_menu():
font = pygame.font.Font(None, 36) font = pygame.font.Font(None, 36)
title_text = font.render("Labirints", True, RED) title_text = font.render("Labirints", True, RED)
start_text = font.render("Press 1 to Control Player", True, BLACK) start_text = font.render("Press 1 to Control Player 1", True, BLACK)
start_text2 = font.render("Press 2 to Control Villain", True, BLACK) start_text2 = font.render("Press 2 to Control Player 2", True, BLACK)
quit_text = font.render("Press Q to Quit", True, BLACK) quit_text = font.render("Press Q to Quit", True, BLACK)
screen.blit(title_text, (width // 2 - title_text.get_width() // 2, height // 2 - 100)) screen.blit(title_text, (width // 2 - title_text.get_width() // 2, height // 2 - 100))
@ -169,15 +169,16 @@ def draw_menu():
pygame.display.flip() pygame.display.flip()
player = Player(32, 32) player1 = Player(32, 32)
player2 = Player(736, 736)
cheese = Cheese(416, 416) # Cheese position cheese = Cheese(416, 416) # Cheese position
villain = Villain(704, 704, player) # Villain's starting position villain = Villain(704, 704, player1) # Villain's starting position
villain2 = Villain(32, 32, player) # Second villain's starting position villain2 = Villain(32, 32, player2) # Second villain's starting position
clock = pygame.time.Clock() clock = pygame.time.Clock()
running = True running = True
show_menu = True show_menu = True
control_player = True # 初始设置玩家控制主角 control_player = True # 初始设置玩家1控制主角
while running: while running:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
@ -201,14 +202,14 @@ while running:
dx, dy = 0, 0 dx, dy = 0, 0
if keys[pygame.K_LEFT]: if keys[pygame.K_LEFT]:
dx = -player.speed dx = -player1.speed
elif keys[pygame.K_RIGHT]: elif keys[pygame.K_RIGHT]:
dx = player.speed dx = player1.speed
if keys[pygame.K_UP]: if keys[pygame.K_UP]:
dy = -player.speed dy = -player1.speed
elif keys[pygame.K_DOWN]: elif keys[pygame.K_DOWN]:
dy = player.speed dy = player1.speed
if dx != 0 and dy != 0: if dx != 0 and dy != 0:
if keys[pygame.K_LEFT]: if keys[pygame.K_LEFT]:
@ -221,26 +222,27 @@ while running:
dx = 0 dx = 0
walls, roads = draw_maze(maze) walls, roads = draw_maze(maze)
player.move(dx, dy, walls) player1.move(dx, dy, walls)
player.update_image(dx, dy) player1.update_image(dx, dy)
villain.move_towards_player(walls) villain.move_towards_player(walls)
if player.rect.colliderect(villain.rect): if player1.rect.colliderect(villain.rect):
print("Game Over! You were caught by the villain.") print("Game Over! Player 1 was caught by the villain.")
running = False running = False
if player.rect.colliderect(cheese.rect): if player1.rect.colliderect(cheese.rect):
player.cheese_count += 1 player1.cheese_count += 1
print("You collected a cheese! Total cheese count:", player.cheese_count) print("Player 1 collected a cheese! Total cheese count:", player1.cheese_count)
cheese.rect.topleft = random.choice(roads).topleft # Move cheese to a random position on a road cheese.rect.topleft = random.choice(roads).topleft # Move cheese to a random position on a road
if player.cheese_count >= 10: if player1.cheese_count >= 10:
print("Congratulations! You collected 10 cheeses!") print("Congratulations! Player 1 collected 10 cheeses!")
running = False running = False
screen.fill(BLUE) screen.fill(BLUE)
draw_maze(maze) draw_maze(maze)
player.draw(screen) player1.draw(screen)
player2.draw(screen)
villain.draw(screen) villain.draw(screen)
pygame.display.flip() pygame.display.flip()
else: else:
@ -248,14 +250,14 @@ while running:
dx, dy = 0, 0 dx, dy = 0, 0
if keys[pygame.K_a]: if keys[pygame.K_a]:
dx = -villain2.speed dx = -player2.speed
elif keys[pygame.K_d]: elif keys[pygame.K_d]:
dx = villain2.speed dx = player2.speed
if keys[pygame.K_w]: if keys[pygame.K_w]:
dy = -villain2.speed dy = -player2.speed
elif keys[pygame.K_s]: elif keys[pygame.K_s]:
dy = villain2.speed dy = player2.speed
if dx != 0 and dy != 0: if dx != 0 and dy != 0:
if keys[pygame.K_a]: if keys[pygame.K_a]:
@ -268,15 +270,16 @@ while running:
dx = 0 dx = 0
walls, roads = draw_maze(maze) walls, roads = draw_maze(maze)
villain2.move(dx, dy, walls) player2.move(dx, dy, walls)
if villain2.rect.colliderect(player.rect): if player2.rect.colliderect(villain2.rect):
print("Game Over! The second villain caught you.") print("Game Over! Player 2 was caught by the second villain.")
running = False running = False
screen.fill(BLUE) screen.fill(BLUE)
draw_maze(maze) draw_maze(maze)
player.draw(screen) player1.draw(screen)
player2.draw(screen)
villain.draw(screen) villain.draw(screen)
villain2.draw(screen) villain2.draw(screen)
pygame.display.flip() pygame.display.flip()