パイゲームの問題!Colliderect 関数は、2 つの四角形が衝突したことを検出しません。エラーメッセージは表示されませんが、四角形は互いに通り抜けます。これはなぜですか、どうすれば修正できますか? この同じ問題に何日も苦労してきました! 想定される問題箇所にはコメントが付けられています。前もって感謝します!
#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()