python - Pygame Colliderect 无法运行

标签 python pygame

Pygame 出现问题! Colliderect 函数无法检测两个矩形何时发生碰撞!尽管没有给出错误消息,但这些矩形只是相互穿过。这是为什么?如何解决?连续几天都在为同样的问题苦苦挣扎!假设的问题点标有注释。提前致谢!

#Start it up
import pygame
pygame.init()
fpsClock = pygame.time.Clock()
#surface = pygame.display.set_mode((640,480),pygame.FULLSCREEN)
surface = pygame.display.set_mode((640,480))
pygame.display.set_caption('Game Skeleton')

#Globals and Misc.
x = 10
y = 350
l = 15
w = 35
moveX=0
moveY=0
characterRect= pygame.Rect(x,y,l,w)
ground = pygame.Rect(0,385,700,385)
ledge1= pygame.Rect(310,330,20,20)
jump=0
white = (255,255,255)
black = (0,0,0)
firebrick = (178,34,34)
blockRects = [ground,ledge1]
contact = False
playOn = True
var=0

#standingLeft = pygame.image.load("images/
#standingRight = pygame.image.load("images/
#walkingRight = pygame.image.load("images/
#walkingLeft = pygame.image.load("images/
#straightJumping = pygame.image.load("images/
#rightJumping = pygame.image.load("images/
#leftJumping = pygame.image.load("images/
#inquire = pygame.image.load("images/
#climbing = pygame.image.load("images/


#Game Loop
while playOn:

    #Take user input
    for event in pygame.event.get():

        if(event.type==pygame.KEYDOWN):

            if(event.key==pygame.K_RIGHT):
                moveX=1

            if(event.key==pygame.K_LEFT):
                moveX=-1

            if(event.key==pygame.K_UP):
                moveY=-1

            if(event.key==pygame.K_DOWN):
                moveY=1

            if(event.key==pygame.K_SPACE):
                jump=1

            if(event.key==pygame.K_ESCAPE):
                playOn = False

        if(event.type==pygame.KEYUP):

            if(event.key==pygame.K_RIGHT):
                moveX=0

            if(event.key==pygame.K_LEFT):
                moveX=0

            if(event.key==pygame.K_UP):
                moveY=0

            if(event.key==pygame.K_DOWN):
                moveY=0

    #If user input said to move
    x = x + moveX
    y = y + moveY

    #Jump Code
    if(jump>0 and contact==False):
        y=y-jump - 1
        var=var+1
        if(var>45):
            jump=0
            var=0

    if(contact==False):
        y=y+1

    if(contact!=True):
        contact==False

    #These two "ifs" don't appear to be working D:
    if (ground.colliderect(characterRect)):
        y=y-1
        contact == True

    if(ledge1.colliderect(characterRect)):
        y=y-1
        contact == True

    #Renderings    
    surface.fill(white)
    pygame.draw.rect(surface,black,(x,y,l,w))
    pygame.draw.rect(surface,firebrick,(0,385,700,385))
    pygame.draw.rect(surface,firebrick,(340,350,100,5))   
    fpsClock.tick(80)
    pygame.display.flip()

pygame.quit()

最佳答案

您正在初始化:

characterRect= pygame.Rect(x,y,l,w)

然后,您独立更新 x,y,l,w 变量,并忘记 characterRect,因此,characterRect 矩形始终位于同一位置。

您可以直接在 characterRect 上进行更新,或者在检查 colliderect 之前分配新的 y 值。

@Justin Pearce 修正也很重要,否则你的代码将无法正常工作。

另请查看PEP 8 。为了获得美观的 Python 代码,您应该删除 if 条件周围的括号。

关于python - Pygame Colliderect 无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17793968/

相关文章:

python - 使用 pygame 通过按键结束循环

python - 正确使用 threading.RLock

python - 当数据从 Google Datastore 流向 BigQuery 时多次更新行

Python/PyGame : Set font. 渲染()阿尔法

python - 在移动 pygame 时移动 Sprite

python - Pygame窗口闪烁

python - 第一发射击与第二发射击叠加,或者在第二发射击后简单消失,等等

python - 如何使用 %s 命名列表

python - 从命令行跳过 pytest 中的导入模块

python - "import tensorflow as tf"导入错误 : Could not find 'cudart64_90.dll'