我的游戏在输或赢时重新启动 - Pygame - 语义错误

问题描述 投票:0回答:1

我正在制作一个游戏,乌龟要回到自己的巢穴才能赢得比赛。如果他们赢了,游戏应该以“你赢了”屏幕结束。如果玩家掉进洞里,游戏就会失败,并以“游戏结束”屏幕结束。相反,这些结束屏幕会播放一瞬间,然后立即重播游戏。这是我当前的代码。

import pygame

# Initialize pygame
pygame.init()
screen = pygame.display.set_mode((600, 800))  # Set screen size (600 wide, 800 high)
clock = pygame.time.Clock()

# Define player speed
player_speed = 5

# Set up game over and win screen images
game_over_image = pygame.image.load('game_over.png')
end_screen_image = pygame.image.load('youwin.png')

# Load the start screen image
start_screen_image = pygame.image.load('start_screen_real.png')
start_screen_rect = start_screen_image.get_rect(center=(300, 400))  # Center start screen at (300, 400)

# Player class definition
class Player:
    def __init__(self, image_path, player_speed):
        self.image = pygame.image.load(image_path)
        self.rect = self.image.get_rect()  # Get the rect that represents the image
        self.velocity_x = 0
        self.velocity_y = 0
        self.player_speed = player_speed  # Define player speed
        self.moving = True  # Flag to indicate whether the player is still moving
    
    def update(self, keys, obstacles):
        if not self.moving:
            return True  # If the player is stopped, don't update

        # Set velocity to 0 if no keys are pressed
        if not any(keys):  # No key pressed
            self.velocity_x = 0
            self.velocity_y = 0

        # Handle key presses for movement
        if keys[pygame.K_a]:  # Left
            self.velocity_x = -self.player_speed
        if keys[pygame.K_d]:  # Right
            self.velocity_x = self.player_speed
        if keys[pygame.K_w]:  # Up
            self.velocity_y = -self.player_speed
        if keys[pygame.K_s]:  # Down
            self.velocity_y = self.player_speed

        # Handle collisions with obstacles (sand piles, holes, etc.)
        for obstacle in obstacles:
            if self.rect.colliderect(obstacle.rect):
                if obstacle.type == 'hole':  # Collision with hole triggers Game Over
                    return False
                else:
                    # Handle collision with sand piles or other obstacles (push player back slightly)
                    if self.velocity_x < 0:  # Moving left
                        self.rect.x += 10  # Push player right (10 steps)
                    if self.velocity_x > 0:  # Moving right
                        self.rect.x -= 10  # Push player left (10 steps)
                    if self.velocity_y < 0:  # Moving up
                        self.rect.y += 10  # Push player down (10 steps)
                    if self.velocity_y > 0:  # Moving down
                        self.rect.y -= 10  # Push player up (10 steps)

        return True  # Return True if no hole was hit, indicating normal movement

    def move(self):
        self.rect.x += self.velocity_x  # Move the player horizontally
        self.rect.y += self.velocity_y  # Move the player vertically

    def draw(self, screen):
        screen.blit(self.image, self.rect)  # Draw the image on the screen using its rect


# Define obstacles as rectangles (sand piles or holes)
class Obstacle:
    def __init__(self, image_path, x, y, obstacle_type='sand'):
        self.image = pygame.image.load(image_path)
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)
        self.type = obstacle_type  # Could be 'sand' or 'hole'

    def draw(self, screen):
        screen.blit(self.image, self.rect)


# Game loop
running = True
game_started = False  # Flag to track whether the game has started
game_over = False  # Flag to track game over state
game_won = False  # Flag to track if the game is won

# Initialize player and obstacles
player = Player("turtle1.png", player_speed)  # Create player instance

# Create obstacles (sand piles and holes) at predefined reasonable positions
obstacles = [
    Obstacle('sand_pile.png', 100, 200),  # First sand pile
    Obstacle('sand_pile.png', 500, 300),  # Second sand pile
    Obstacle('sand_pile.png', 400, 500),  # Third sand pile
    Obstacle('sand_pile.png', 200, 600),  # Fourth sand pile
    Obstacle('hole.png', 300, 250, 'hole'),  # First hole
    Obstacle('hole.png', 100, 500, 'hole'),  # Second hole
    Obstacle('hole.png', 400, 450, 'hole'),  # Third hole
    Obstacle('hole.png', 500, 650, 'hole'),  # Fourth hole
]

# Set up the goal (nest)
goal_image = pygame.image.load("egg_nest.png")
goal_rect = goal_image.get_rect()
goal_rect.center = (275, 150)  # Nest placed at (275, 150)

# Load the background image
background = pygame.image.load("pixel_art.png")

while running:
    # Blit the background image at (0, 0) to fill the screen
    screen.blit(background, (0, 0))

    # Event handling
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # If the game hasn't started, show the start screen
    if not game_started:
        # Display the start screen centered at (300, 400)
        screen.blit(start_screen_image, start_screen_rect)  # Centered at (300, 400)
        
        # Wait for player to press F to start the game
        keys = pygame.key.get_pressed()
        if keys[pygame.K_f]:  # Press F to begin the game
            game_started = True  # Set the flag to start the game

            # Position the player at spawn point
            player.rect.center = (275, 650)

            # Set up the game environment (goal, obstacles, etc.)
            goal_rect.center = (275, 150)  # Nest placed at (275, 150)

    else:
        # Game has started, allow player movement and game logic
        keys = pygame.key.get_pressed()
        if not player.update(keys, obstacles):  # If the player collides with a hole
            game_over = True  # Set game over flag
            screen.blit(game_over_image, (150, 200))  # Show "Game Over" screen at (300, 400)
        else:
            player.move()  # Move the player based on keys pressed
            player.draw(screen)  # Draw the player on the screen

            # Draw the goal (nest) on the screen
            screen.blit(goal_image, goal_rect)

            # Draw all obstacles (sand piles and holes)
            for obstacle in obstacles:
                obstacle.draw(screen)

            # Check for collision with goal (nest)
            if player.rect.colliderect(goal_rect):
                if not game_won:  # Prevent game from restarting immediately
                    game_won = True  # Mark the game as won
                    screen.blit(end_screen_image, (150, 200))  # Show "You Win!" screen at (300, 400)

    # If game is over or won, stop further actions
    if game_over or game_won:
        game_started = False  # Stop player movement and interaction after game ends

    # Update the screen
    pygame.display.flip()

    # Set FPS
    clock.tick(60)

pygame.quit()

要分解它,这是有效的:

  • 运动
  • 碰撞
  • 精灵(就图像而言)
  • 位置
  • 开始屏幕 这是行不通的:
  • 游戏结束画面

你赢了屏幕

现在,游戏开始时会出现一个开始屏幕。当游戏开始时,该屏幕将消失。也有输的方式,也有赢的方式。通过达到最终目标,您就赢了。掉进洞里你就输了。如果你输了,就会出现一个游戏结束屏幕;如果你赢了,就会出现一个“你赢了”屏幕。当玩家获胜/失败时,这些屏幕只会播放一瞬间,并且游戏会冻结在开始屏幕上。我不想冻结在开始屏幕上,而是冻结在游戏结束或你赢得屏幕上。卡皮切?

python pygame
1个回答
0
投票

您面临的问题可能是因为当游戏在

game_started = False
组中结束时您设置了
while running:
标志,并且当该标志设置为 false 时显示开始屏幕。

这意味着当游戏结束并且

game_started
标志设置为
False
时,您将立即再次显示开始屏幕。

一种可能的解决方案是延迟

game_started = False
分配,直到玩家单击胜利或游戏结束屏幕上的按钮。

# If game is over or won, stop further actions
if game_over or game_won:
    game_started = False
# If the game hasn't started, show the start screen
if not game_started:
    # Display the start screen
© www.soinside.com 2019 - 2024. All rights reserved.