为什么我的 colliderect() 不能正常工作?

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

碰撞没有反应,就好像平台向右移动了一样。 我是第一次使用它,所以我想要一个额外解释的答案......

    def collision(self, j):
        self.player_rect = pg.Rect(self.playerx, self.playery, player_w, player_h)
        for i in range(len(self.platfsx)):
            if self.player_rect.colliderect(pg.Rect(self.platfsx[i], self.platfsy[i], platf_w, platf_h)) and self.jump == False and self.y_change >= 0:
                j = True
                print("Hsllod")
        return j

完整代码:

from random import *
import pygame as pg
import time
from math import *

pg.init()

'''Можна змінювати'''
WIDTH, HEIGHT = 500, 900
DIS = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption("NoName")

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)

FPS = 60

platf_h = 20    # Висота платформи
platf_w = 100   # Широта платформи
platfs_h = 120  # Відстань між платформами

player_h = 30
player_w = 25

g = .4 # Гравітація
jump_h = 16 # Висота стрибка


class Map(object):
    def __init__(self):
        self.platfsx = [randint(20, WIDTH - platf_w - 20) for i in range(19)]
        self.platfsy = [HEIGHT - 30 - i*platfs_h for i in range(19)]
        self.platfsx[0] = int(WIDTH/2 - platf_w/2)
    
    def create(self):
        if len(self.platfsx) <= 20:
            self.platfsx.append(randint(20, WIDTH - platf_w - 20))
        else:
            del self.platfsx[0]
        print(self.platfsx)
        
    def platf_draw(self):
        for i in range(len(self.platfsx)):
            pg.draw.rect(DIS, WHITE, (self.platfsx[i], self.platfsy[i], platf_w, platf_h))
            
    def background(self, color):
        DIS.fill(color)
    
    
class Player(Map):
    def __init__(self):
        super().__init__()
        self.playerx = int(WIDTH/2 - player_w/2)
        self.y_change = 0
        self.jump = False
        self.playery = HEIGHT - player_h - 30
    
    def create(self):
        pg.draw.rect(DIS, RED, [self.playerx, self.playery, player_w, player_h])
    
    def collision(self, j):
        self.player_rect = pg.Rect(self.playerx, self.playery, player_w, player_h)
        for i in range(len(self.platfsx)):
            if self.player_rect.colliderect(pg.Rect(self.platfsx[i], self.platfsy[i], platf_w, platf_h)) and self.jump == False and self.y_change >= 0:
                j = True
                print("Hsllod")
        return j
    
    def move(self):
        if self.jump:
            self.y_change = -jump_h
            self.jump = False
        self.playery += self.y_change
        self.y_change += g
        pg.time.delay(10)


def main():
    clock = pg.time.Clock()
    clock.tick(FPS)
    map = Map()
    player = Player()
    run = True
    while run:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                run = False
        keys_pressed = pg.key.get_pressed()
    
        map.background(BLACK)
        map.platf_draw()
        player.create()
        player.jump = player.collision(player.jump)
        player.move()
        pg.display.update()

    
    pg.quit()


if __name__ == "__main__":
    main()

谢谢

python pygame collision-detection collision
1个回答
-1
投票

看来碰撞检测功能是在Player类中作为collision方法实现的。该方法使用 Pygame 中 Rect 类的 colliderect() 方法检查玩家的矩形边界框(由 player_rect 变量定义)是否与任何平台边界框发生碰撞。

如果检测到碰撞且玩家当前未跳跃(self.jump == False)并且玩家的垂直速度为非负数(self.y_change >= 0),则 j 变量设置为 True,表示玩家在平台上,可以再次跳跃。

在Player类的move方法中,根据当前的垂直速度(self.y_change)和重力(g)更新玩家的垂直位置(self.playery)。这个方法在游戏循环中被反复调用,以随着时间的推移更新玩家的位置。

一个问题,预期的行为是什么以及代码当前的行为方式。但我确信碰撞检测的实施或玩家位置更新的方式存在问题。您可能想尝试打印出一些变量或使用调试器来帮助隔离问题。

© www.soinside.com 2019 - 2024. All rights reserved.