我正在尝试创建一个可以在每帧中计算对撞机的函数,但是它没有按照原意进行操作
import pygame, sys
pygame.init()
white,red,green,lightblue = (255,255,255),(255,0,0),(0,255,0),(89, 135, 255)
black = (0,0,0)
window = pygame.display.set_mode((1280,720))
pygame.display.set_caption("Testing")
playerx = 0
playery = 0
font = pygame.font.Font(None, 50)
entityx = 0
entityy = 200
jump = 0
counter = 0
yx = 0
ya = 0
#Functions
def collider(x,y,w,h):
global playerx, playery
if x-w <= playerx <= x+(w-5) and y-(h-5) <= y <= y+(h-5):
playerx = x-w
if x-(w-5) <= playerx <= x+w and y-(h-5) <= playery <= y+(h-5):
playerx = x+w
if x-(w-5) <= playerx <= x+(w-5) and y-h <= playery <= y+(h-5):
playery = y-h
if x-(w-5) <= playerx <= x+(w-5) and y-(h-5) <= playery <= y+h:
playery = y+h
return playery, playerx
x是x轴,y是y轴,w是宽度,h是高度。
pygame.draw.rect(window, black, (entityx,entityy, 100,30))
collider(entityx, entityy, 100, 30)
imgur.com/a/kix1mxz
Playerx是播放器在x轴上的位置,playery是播放器在y轴上的位置
当我将collider(entityx,entityy,100、30)替换为
if entityx-100 <= playerx <= entityx+95 and entityy-25 <= playery <= entityy+25:
playerx = entityx-100
if entityx-95 <= playerx <= entityx+100 and entityy-25 <= playery <= entityy+25:
playerx = entityx+100
if entityx-495 <= playerx <= entityx+95 and entityy-30 <= playery <= entityy+25:
playery = entityy-30
if entityx-95 <= playerx <= entityx+95 and entityy-25 <= playery <= entityy+30:
playery = entityy+30
它起作用了,我真的很困惑为什么函数会破坏它