-1
我知道你可能認爲它有點複製,但事實並非如此。這不是使用類,完全基於pygame
而沒有import os
。我需要檢測這兩個矩形是否碰撞,並在代碼中用註釋發送信號。如何測試Rect與Rect |衝突的時間Python | Pygame
下面是代碼: 的Python:
import pygame, sys, time, random
from pygame.locals import *
pygame.init()
fpsClock = pygame.time.Clock()
playSurface = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Bob the Block goes Skiing')
redColour = pygame.Color(252, 0, 6)
blackColour = pygame.Color(0, 0, 0)
whiteColour = pygame.Color(255, 255, 255)
greyColour = pygame.Color(150, 150, 150)
bobPosition = [300,70]
direction = 'right'
changeDirection = direction
pygame.draw.rect(playSurface,redColour,Rect(bobPosition[0], bobPosition[1], 40, 40))
pygame.display.flip()
length = 100
obstPosition = [100,475]
while True:
if obstPosition[1] < 25:
x = random.randint(10,505)
obstPosition = [x,475]
length += random.randint(1,5)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
elif event.type == KEYDOWN:
if event.key == K_RIGHT or event.key == ord('d'):
changeDirection = 'right'
if event.key == K_LEFT or event.key == ord('a'):
changeDirection = 'left'
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
direction = changeDirection
if direction == 'right':
bobPosition[0] += 20
if direction == 'left':
bobPosition[0] -= 20
playSurface.fill(blackColour)
a = ['one','two','three']
# Creating the obstacle
pygame.draw.rect(playSurface,whiteColour,Rect(obstPosition[0], obstPosition[1], length, 20))
obstPosition[1] -= 10
# Creating "bob the block"
pygame.draw.rect(playSurface,redColour,Rect(bobPosition[0], bobPosition[1], 25, 25))
pygame.display.flip()
if bobPosition[0] > 620 or bobPosition[0] < 0:
gameOver()
if bobPosition[1] > 460 or bobPosition[1] < 0:
gameOver()
fpsClock.tick(19)
所以'collidetest'將TRUE;如果碰撞是真的嗎? – Okx 2014-10-26 17:12:59
collideTest等於1,如果爲真,則爲0 – OhMyLumpinGlob 2014-10-26 17:16:49