【发布时间】:2015-04-13 03:04:13
【问题描述】:
当你运行这段代码时:
import pygame, sys, random
from pygame.locals import *
def doRectsOverlap(rect1, rect2):
for a, b in [(rect1, rect2), (rect2, rect1)]:
# Check if a's corners are inside b
if ((isPointInsideRect(a.left, a.top, b)) or
(isPointInsideRect(a.left, a.bottom, b)) or
(isPointInsideRect(a.right, a.top, b)) or
(isPointInsideRect(a.right, a.bottom, b))):
return True
return False
def isPointInsideRect(x, y, rect):
if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom):
return True
else:
return False
# set up pygame
pygame.init()
mainClock = pygame.time.Clock()
# set up the window
WINDOWWIDTH = 1024
height = 768
windowSurface = pygame.display.set_mode((WINDOWWIDTH, height), 0, 32)
pygame.display.set_caption('Collision Detection')
# set up direction variables
DOWNLEFT = 1
DOWNRIGHT = 3
UPLEFT = 7
UPRIGHT = 9
MOVESPEED = 10
# set up the colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
# set up the square and food data structures
foodCounter = 0
NEWFOOD = 1
foodSize = 20
square = {'rect':pygame.Rect(300, 100, 25, 25), 'dir':UPLEFT}
foods = []
for i in range(20):
foods.append({'rect':pygame.Rect(random.randint(0, WINDOWWIDTH - foodSize), random.randint(0, height - foodSize), foodSize, foodSize), 'dir':UPLEFT})
# run the game loop
while True:
# check for the QUIT event
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
foodCounter += 1
if foodCounter >= NEWFOOD:
# add new food
foodCounter = 0
foods.append({'rect':pygame.Rect(random.randint(0, WINDOWWIDTH - foodSize), random.randint(0, height - foodSize), foodSize, foodSize), 'dir':UPLEFT})
# draw the black background onto the surface
windowSurface.fill(BLACK)
# move the square data structure
if square['dir'] == DOWNLEFT:
square['rect'].left -= MOVESPEED
square['rect'].top += MOVESPEED
if square['dir'] == DOWNRIGHT:
square['rect'].left += MOVESPEED
square['rect'].top += MOVESPEED
if square['dir'] == UPLEFT:
square['rect'].left -= MOVESPEED
square['rect'].top -= MOVESPEED
if square['dir'] == UPRIGHT:
square['rect'].left += MOVESPEED
square['rect'].top -= MOVESPEED
# check if the square has move out of the window
if square['rect'].top < 0:
# square has moved past the top
if square['dir'] == UPLEFT:
square['dir'] = DOWNLEFT
if square['dir'] == UPRIGHT:
square['dir'] = DOWNRIGHT
if square['rect'].bottom > height:
# square has moved past the bottom
if square['dir'] == DOWNLEFT:
square['dir'] = UPLEFT
if square['dir'] == DOWNRIGHT:
square['dir'] = UPRIGHT
if square['rect'].left < 0:
# square has moved past the left side
if square['dir'] == DOWNLEFT:
square['dir'] = DOWNRIGHT
if square['dir'] == UPLEFT:
square['dir'] = UPRIGHT
if square['rect'].right > WINDOWWIDTH:
# square has moved past the right side
if square['dir'] == DOWNRIGHT:
square['dir'] = DOWNLEFT
if square['dir'] == UPRIGHT:
square['dir'] = UPLEFT
# draw the square onto the surface
pygame.draw.rect(windowSurface, WHITE, square['rect'])
# check if the square has intersected with any food squares.
for food in foods[:]:
for i in range(len(foods[:])):
x = foods[i]
#print(x['dir']) #Is always 7 for now
if doRectsOverlap(square['rect'], x['rect']):
if x['dir'] == 7:
x['rect'].left -= MOVESPEED
x['rect'].top -= MOVESPEED
# draw the food
for i in range(len(foods)):
pygame.draw.rect(windowSurface, GREEN, foods[i])
# draw the window onto the windowSurface
pygame.display.update()
mainClock.tick(60)
有一个白色方块在屏幕上弹跳,吃掉食物(绿色方块不动),这使它变大了。既然已经解决了,我该如何修改它,当白色方块接触绿色方块时,绿色方块开始移动(右下)然后跑出屏幕? (之后白色方块还在增长)
更新: 程序运行,但屏幕上没有显示任何内容。现在也显示此错误消息。
Traceback (most recent call last):
File "file.py", line 130, in <module>
pygame.draw.rect(windowSurface, GREEN, foods[i])
TypeError: Rect argument is invalid
附:没有课程。
感谢您的帮助!谢谢。
【问题讨论】:
标签: python pygame python-3.4