pygame如何创建地图障碍

时间:2019-06-17 05:20:42

标签: python pygame

我保存图像而不用付费,但是,除了学习如何使用PyGame之外,我不打算将其用于其他任何用途。我是初学者。

执行代码时,玩家控制的矩形对象无法穿过围栏,但是,当围栏位于矩形对象的顶部或底部时,则不能向左或向右移动,并且当栅栏靠在矩形对象的左侧或右侧时,它不能向上或向下移动。我该如何解决?

对不起,如果代码不是很井井有条或不清楚,请先谢谢您!

在此处找到地图精灵:https://www.scirra.com/store/2d-game-gra...t-pack-184

# Allows the usage of the PyGame library
import pygame

# Initializes PyGame
pygame.init()

# RGB Colors
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)

# Coordinates of the rectangle (player) object
x = 507
y = 60

# Bad Coordinates (Fence)
# bad_coordinates = bad_x, bad_y = range(447, 528), 150

# Directions
up = False
down = False
left = False
right = False

# Height and width of the window
height = 320
width = 720

# Barrier
minimum_x = 342

# Initializes the window's title, width, and height
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("New Game")
# Loads the background image
farm_image = pygame.image.load("farm_sprite.png").convert()

# Keeps the while loop running
done = False

# Boolean to identify whether the player has unlocked the currently locked area
unlocked = False

# Creates a clock object to keep track of time
clock = pygame.time.Clock()

# Keeps the program running
while not done:

    # Allows the user to exit by pressing the X button without throwing an error
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # Fence Rectangles
    fence = [
        pygame.draw.rect(screen, red, pygame.Rect(455, 65, 20, 85)),
        pygame.draw.rect(screen, red, pygame.Rect(390, 65, 85, 20)),
        pygame.draw.rect(screen, red, pygame.Rect(390, 0, 20, 85)),
        pygame.draw.rect(screen, red, pygame.Rect(395, 0, 240, 20)),
        pygame.draw.rect(screen, red, pygame.Rect(615, 0, 20, 150)),
        pygame.draw.rect(screen, red, pygame.Rect(580, 130, 40, 20)),
        pygame.draw.rect(screen, red, pygame.Rect(455, 130, 80, 20))
    ]

    # Assigns a list of the possible pressed keys to a variable
    pressed = pygame.key.get_pressed()

    # If the UP key is pressed, move the player upwards
    if pressed[pygame.K_UP]:
        if y != 3 and (char.collidelist(fence)) == -1 or down is True:
            up = True
            down = False
            y -= 3

    # If the DOWN key is pressed, move the player downwards
    if pressed[pygame.K_DOWN]:
        if y != 279 and (char.collidelist(fence)) == -1 or up is True:
            down = True
            up = False
            y += 3

    # If the LEFT key is pressed, move the player to the left
    if pressed[pygame.K_LEFT]:
        if x != minimum_x and not unlocked and (char.collidelist(fence)) == -1 or right is True:
            left = True
            right = False
            x -= 3

        # If the area is unlocked, allow the player to move in the area, as well as create a new barrier
        """
        elif unlocked and (char.collidelist(fence)) == -1 or right is True:
            if x != 3:
                x -= 3
        """

    # If the RIGHT key is pressed, move the player to the right
    if pressed[pygame.K_RIGHT]:
        if x != 693 and (char.collidelist(fence)) == -1 or left is True:
            right = True
            left = False
            x += 3

    # Place the background image on the window
    screen.blit(farm_image, [0, 0])

    # Draw the player object onto the screen
    char = pygame.draw.rect(screen, red, pygame.Rect(x, y, 25, 40))

    # Update the full Surface to the screen
    pygame.display.flip()

    # Run the program at 60 frames per second
    clock.tick(60)

0 个答案:

没有答案