python-3.x Hitbox不会呆在原地,它们会移动

iyzzxitl  于 2023-08-08  发布在  Python
关注(0)|答案(1)|浏览(93)

我在做一个关于收集树木和杀死僵尸的游戏。
当我在Map上行走时,树的hitbox不会停留在原地,它们只是随着屏幕移动。
我在试着解决这个问题,但我不知道怎么做。
我是一个Python新手,如果有人能帮助我,我会很感激。
这里是代码。

#imports
import pygame
import random

#window settings
window_size = (600, 600)
window = pygame.display.set_mode(window_size)

#background settings
background = pygame.image.load("Images/Background8.2.png")
background_x = -450
background_y = -450

#camera settings
camera_x = 0
camera_y = 0

#player settings
class Player:
    def __init__(self):
        self.image = pygame.image.load("Images/Player2.1.png")
        self.x_cord = 250
        self.y_cord = 250
        self.width = 35
        self.height = 60
        self.velocity = 3
        self.hitbox = pygame.Rect(self.x_cord, self.y_cord, self.width, self.height)
    def tick(self, keys):
        global camera_x
        global camera_y
        if keys[pygame.K_d]:
            self.x_cord += self.velocity
            self.image = pygame.image.load("Images/Player2.1.png")
            camera_x += self.velocity
        if keys[pygame.K_a]:
            self.x_cord -= self.velocity
            self.image = pygame.image.load("Images/Player2.2.png")
            camera_x -= self.velocity
        if keys[pygame.K_w]:
            self.y_cord -= self.velocity
            camera_y -= self.velocity
        if keys[pygame.K_s]:
            self.y_cord += self.velocity
            camera_y += self.velocity
        if self.x_cord >= 2800:
            self.x_cord -= self.velocity
            camera_x -= self.velocity
        if self.x_cord <= -200:
            self.x_cord += self.velocity
            camera_x += self.velocity
        if self.y_cord <= -200:
            self.y_cord += self.velocity
            camera_y += self.velocity
        if self.y_cord >= 2800:
            self.y_cord -= self.velocity
            camera_y -= self.velocity
        self.hitbox = pygame.Rect(self.x_cord - camera_x + 30, self.y_cord - camera_y + 20, self.width, self.height)

    def draw(self):
        window.blit(self.image, (self.x_cord - camera_x, self.y_cord - camera_y))
        pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)

class Tree:
    def __init__(self):
        self.image = pygame.image.load("Images/Tree1.png")
        self.x_cord = random.randint(0, 100)
        self.y_cord = random.randint(0, 100)
        self.width = 50
        self.height = 100
        self.hitbox = pygame.Rect(self.x_cord + 75, self.y_cord + 125, self.width, self.height)

    def tick(self):
        self.hitbox = pygame.Rect(self.x_cord, self.y_cord, self.width, self.height)
    def draw(self):
        window.blit(self.image, (self.x_cord - camera_x, self.y_cord - camera_y))
        pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)

#main loop
def game():
    player = Player()
    run_game = True
    trees = []
    for _ in range(1):
        trees.append(Tree())
    while run_game:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit(0)
        
        keys = pygame.key.get_pressed()
        for tree in trees:
            if player.hitbox.colliderect(tree.hitbox):
                quit(0)

        window.blit(background, (background_x -camera_x, background_y -camera_y))
        for tree in trees:
            tree.draw()
        player.draw()
        player.tick(keys)
        pygame.display.update()

if __name__ == "__main__":
    game()

字符串
屏幕截图


的数据


trnvg8h3

trnvg8h31#

树图像的位置取决于摄像机,因此hitbox的位置也必须取决于摄像机:

class Tree:
    def __init__(self):
        self.image = pygame.image.load("Images/Tree1.png")
        self.x_cord = random.randint(0, 100)
        self.y_cord = random.randint(0, 100)
        self.width = 50
        self.height = 100
        self.hitbox = pygame.Rect(self.x_cord + 75, self.y_cord + 125, self.width, self.height)

    def tick(self):
        self.hitbox.x = self.x_cord + 75 - camera_x
        self.hitbox.y = self.y_cord + 125 - camera_y

    def draw(self):
        self.tick()
        window.blit(self.image, (self.x_cord - camera_x, self.y_cord - camera_y))
        pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)

字符串

相关问题