import pygame
import time
import random
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,155,0)
blue = (0, 0, 200)
#img = pygame.image.load("rectangle.png")
displayW = 500
displayH = 500
display = pygame.display.set_mode((displayW, displayH))
pygame.display.update()
clock = pygame.time.Clock()
rectSize = 40
obstacleSize = 30
FPS = 30
def left(obstaclex,obstacley):
pygame.draw.rect(display, blue, [0, obstacley, rectSize, rectSize])
def right(rectx,recty):
pygame.draw.rect(display, blue, [rectx, recty, rectSize, rectSize])
def middleL(obstaclex, obstacley):
obstacleSize = -230
obstaclex = 500
pygame.draw.rect(display, red, [obstaclex,obstacley, obstacleSize, rectSize])
def middleR(obstaclex, obstacley):
obstacleSize = 210
obstaclex = 0
#display.blit(img, (obstaclex, obstacley))
pygame.draw.rect(display, red, [obstaclex, obstacley, obstacleSize, 40])
def middle(obstaclex, obstacley):
middleL(obstaclex, obstacley)
middleR(obstaclex, obstacley)
def gameLoop():
gameExit = False
gameOver = False
obstacley = 10
obstaclex = 1
rectx = 220
recty = 400
rect1 = (rectx, recty, rectSize, rectSize)
obstacleRectX = 0
obstacleRectY = 20
obstacleChangeX = 0
obstaleChageY = 0
randrect = random.randrange(0,4)
myRight = rectx + rectSize
myLeft = rectx
myTop = recty
myBottom = recty + rectSize
otherRight = obstaclex + obstacleSize
otherLeft = obstaclex
otherTop = obstacley
otherBottom = obstacley + rectSize
randObstacle = random.randrange(0,2)
while not gameExit:
pressed = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
display.fill(black)
pygame.draw.rect(display, blue, [rectx, recty, rectSize, rectSize])
if pressed[pygame.K_RIGHT]:
display.fill(black)
rectx = displayW/2 + 70 + rectSize
right(rectx,recty)
if pressed[pygame.K_LEFT]:
display.fill(black)
rectx = displayW/2 - 170
left(rectx,recty)
if pressed[pygame.K_LEFT] and pressed[pygame.K_RIGHT]:
display.fill(black)
rectx = displayW/2 + 100
right(rectx,recty)
rectx = displayW/2 - 170
left(rectx,recty)
else:
display.fill(black)
pygame.draw.rect(display, blue, [rectx, recty, rectSize, rectSize])
rectx = displayW/2 - 30
## if randObstacle == 0:
##
## middleL(obstaclex, obstacley)
## obstacley += 5
## randObstacle = random.randrange(0,2)
## elif randObstacle == 1:
##
## middleR(obstaclex, obstacley)
## obstacley += 5
## randObstacle = random.randrange(0,2)
## elif randObstacle == 2:
##
## middle(obstaclex, obstacley)
## obstacley += 5
## randObstacle = random.randrange(0,2)
middleR(obstaclex, obstacley)
obstacley += 5
collision = True
if ((myRight < otherLeft) and (myLeft > otherRight) and (myBottom < otherTop) and (myTop > otherBottom)):
# i replaced the OR's with AND's
collision = False
print collision
pygame.display.update()
clock.tick(FPS)
pygame.quit()
quit()
gameLoop()
我试图让一个矩形与朝向它的矩形相撞,但是我似乎无法让它们正确地碰撞。我想用if语句来做这件事,而不是sprite.collide()
。
这段代码不是很精致,我会修复它,但暂时帮助我理解代码。
**这是编辑后的代码**
答案 0 :(得分:0)
当你开始做这种事情的方法是制作8个变量(这只是伪代码)
myRight = x + width
myLeft = x
myTop = y
myBottom = y + height
otherRight = x + width
otherLeft = x
otherTop = y
otherBottom = y + height
最简单的方法是从碰撞开始并尝试反驳它
collision = True
if ((myRight < otherLeft) or (myLeft > otherRight) or (myBottom < otherTop) or (myTop > otherBottom)):
collision = False
你必须自己实现这个,但是当我开始使用八个变量myRight,myLeft等时我发现它很有用,因为它们可以帮助你理解所有x +宽度实际意味着什么
好的,我不会在这里发布整个代码,因为它会使这个评论很长。我删除了这个动作和很多无关紧要的东西。你需要先计算一切,然后画出来。
这是你的游戏,直到工作碰撞的裸骨: http://pastebin.com/P0HyPGY0