如何修复我的地图上有一个巨大的碰撞盒?

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

我正在尝试制作一款游戏,我想要在我的岛屿上设置碰撞箱。我不明白为什么碰撞盒覆盖了我的 pygame 窗口的整个右侧!我正在学习 pygame 和 python,但我不知道该怎么做。我一开始就使用 ChatGPT 来帮助我。

这是我的代码。我尝试过调整代码大小,但只会让敌人(海军陆战队)和文本命中框让我立即死亡。如果有点模糊,我很抱歉,我是堆栈溢出的新手。

import pygame
import os

pygame.init()
pygame.font.init()

pygame_icon = pygame.image.load('C:/Users/space/OneDrive/Desktop/doc for     python/Assets/images_upscaled-removebg-preview.png')
pygame.display.set_icon(pygame_icon)
WIDTH, HEIGHT = (840, 840)
MAP = pygame.transform.scale(pygame.image.load(os.path.join('Assets', 'map.png')), (WIDTH,  HEIGHT))
pygame.display.set_caption("One Piece")
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
HEALTH_FONT = pygame.font.SysFont('comicsans', 40)
SHIP_WIDTH, SHIP_HEIGHT = 45, 60
MARINEHIT = pygame.USEREVENT + 1
FPS = 60
VEL = 5
BORDER = pygame.Rect(WIDTH//2, 0, 10, HEIGHT)
SHIP = pygame.image.load(os.path.join('Assets', 'spaceship_yellow.png'))
SHIP = pygame.transform.rotate(pygame.transform.scale(SHIP, (SHIP_WIDTH, SHIP_HEIGHT)), 90)

BLUE = (29, 0, 51)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
WHITE = (255, 255, 255)
ORANGE = (255, 165, 0)
GREEN = (0, 255, 0)
PURPLE = (255, 0, 255)

ship = pygame.Rect(100, 100, SHIP_WIDTH, SHIP_HEIGHT)
ship_health = 5
marines = [pygame.Rect(300, 200, 30, 30), pygame.Rect(500, 400, 30, 30), pygame.Rect(700, 600,  30, 30), pygame.Rect(200, 800, 30, 30), pygame.Rect(800, 100, 30, 30)]
HITBOX = (40, 30, 50, 50)

def draw_window():
    WIN.blit(MAP, (0, 0))
    red_health_text = HEALTH_FONT.render("Health: " + str(ship_health), 1, WHITE)
    WIN.blit(red_health_text, (WIDTH - red_health_text.get_width() - 10, 10))
    WIN.blit(SHIP, (ship.x, ship.y))

    # Draw Black Hitbox
    pygame.draw.rect(WIN, BLACK, HITBOX)

# Draw Marines
for marine in marines:
    pygame.draw.rect(WIN, RED, marine)

def movement(keys_pressed, ship):
    keys_pressed = pygame.key.get_pressed()
    if keys_pressed[pygame.K_a] and ship.x - VEL > 0:  # left
        ship.x -= VEL
    if keys_pressed[pygame.K_d] and ship.x + VEL + ship.width < BORDER.x:  # right
        ship.x += VEL
    if keys_pressed[pygame.K_w] and ship.y - VEL > 0:  # up
        ship.y -= VEL
    if keys_pressed[pygame.K_s] and ship.y + VEL + ship.height < HEIGHT:  # down
        ship.y += VEL


def check_collisions(ship, marines):
    for marine in marines:
        if ship.colliderect(marine):
            return True
    return False

def draw_window():
    WIN.blit(MAP, (0, 0))
    red_health_text = HEALTH_FONT.render("Health: " + str(ship_health), 1, WHITE)
    WIN.blit(red_health_text, (WIDTH - red_health_text.get_width() - 10, 10))
    WIN.blit(SHIP, (ship.x, ship.y))

    # Draw Black Hitbox
    pygame.draw.rect(WIN, BLACK, HITBOX)

    # Draw Marines
    for marine in marines:
        pygame.draw.rect(WIN, RED, marine)

def draw_window():
    WIN.blit(MAP, (0, 0))
    red_health_text = HEALTH_FONT.render("Health: " + str(ship_health), 1, WHITE)
    WIN.blit(red_health_text, (WIDTH - red_health_text.get_width() - 10, 10))
    WIN.blit(SHIP, (ship.x, ship.y))

    # Draw Black Hitbox
    pygame.draw.rect(WIN, BLACK,HITBOX )

    # Draw Marines
    for marine in marines:
    pygame.draw.rect(WIN, RED, marine)

    pygame.display.flip()

# Main game loop
def main():
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        keys = pygame.key.get_pressed()
        movement(keys, ship)

        # Check collision with the black hitbox
        if ship.colliderect(HITBOX):
        # Adjust the ship position to stay outside the black hitbox
            ship.x = min(max(ship.x, HITBOX.left - ship.width), HITBOX.right)
            ship.y = min(max(ship.y, HITBOX.top - ship.height), HITBOX.bottom)

        # Check collision with Marines
        if check_collisions(ship, marines):
            ship_health -= 1
            pygame.time.set_timer(MARINEHIT, 1000)  # Set a timer to control the frequency of damage

        draw_window()

    pygame.quit()


if __name__ == "__main__":
    pygame.init()
    pygame.font.init()
python python-3.x pygame 2d game-development
1个回答
0
投票

问题不是

HITBOX
而是
BORDER

if ... and ship.x + VEL + ship.width < BORDER.x:

您需要

WIDTH
而不是
BORDER.x

if ... and ship.x + VEL + ship.width < WIDTH:

顺便说一句:

pygame.Rect()
有许多有用的属性。

您可以使用

ship.right
代替
ship.x + ship.width

if ... and ship.right + VEL < WIDTH:

您可以使用

ship.bottom
代替
ship.y + ship.height

if ... and ship.bottom + VEL < HEIGHT:
© www.soinside.com 2019 - 2024. All rights reserved.