繁体   English   中英

Pygame:在其他多个矩形上碰撞矩形

[英]Pygame: Colliding Rectangle on multiple other rectangles

我正在尝试创建一个游戏,其中一个方块来回移动直到玩家按下空格。 然后,程序段跳至下一行并停止。

目前,我在碰撞代码方面遇到问题。

shell抛出的错误是:

    if doRectsOverlap(j['rect'], floors['line']):
TypeError: list indices must be integers, not str

我坚持了解我的代码哪里出了问题。 我对python如何工作的了解非常有限。

还有一些代码我已经注释掉,它与玩家跳跃时地板移动向后移动有关。 它已被注释掉,直到我可以使碰撞起作用,但仍包括在内

下面的代码:

import pygame, sys, time
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 = 480
WINDOWHEIGHT = 800
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Jumper')

#Directions
LEFT = 4
RIGHT = 6
UP = 8
DOWN = 2
STILL = 5

#blocks location for jumping
#BLOCKLOCY = 700

#Binary for stopping movement
#STOPPER = 0

MOVESPEED = 1

# set up the colors
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)


j = {'rect':pygame.Rect(240, 700, 20, 20), 'color':GREEN, 'dir':LEFT, 'jump':STILL}


f1 = {'line':pygame.Rect(0,720,480,2), 'color':GREEN, 'dir':STILL}
f2 = {'line':pygame.Rect(0,650,480,2), 'color':GREEN, 'dir':STILL}
floors = [f1,f2]

# 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()



    # draw the black background onto the surface
    windowSurface.fill(BLACK)


        # move the block data structure
    if j['dir'] == LEFT:
        j['rect'].left -= MOVESPEED
    if j['dir'] == RIGHT:
        j['rect'].left += MOVESPEED

    if j['jump'] == UP:
        j['rect'].bottom -= MOVESPEED
        #BLOCKLOCY -= MOVESPEED

    if j['rect'].left < 0:
        j['dir'] = RIGHT
    if j['rect'].left > WINDOWWIDTH-j['rect'].width:
        j['dir'] = LEFT

    if event.type == KEYDOWN:
        if event.key == K_SPACE:
            j['jump'] = UP

    if doRectsOverlap(j['rect'], floors['line']):
        j['jump'] = STILL



#Floor controll code for moving level - not working currently
   # for f in floors:
        #if f['dir'] == DOWN:
          #  f['line'].y += MOVESPEED

      #  if event.type == KEYDOWN:
          #  if event.key == K_SPACE:
              # f['dir'] = DOWN

     #   if f['line'].top == BLOCKLOCY:
      #      f['dir'] = STILL
       #     STOPPER = 1

        #if f['line'].bottom == BLOCKLOCY:
         #   f['dir'] = STILL
          #  STOPPER = 1




        # draw the block onto the surface
        pygame.draw.rect(windowSurface, j['color'], j['rect'])

        pygame.draw.rect(windowSurface, f['color'], f['line'])


    # draw the window onto the screen
    pygame.display.update()
    mainClock.tick(40)

您正在创建floors作为list

f1 = {'line':pygame.Rect(0,720,480,2), 'color':GREEN, 'dir':STILL}
f2 = {'line':pygame.Rect(0,650,480,2), 'color':GREEN, 'dir':STILL}
floors = [f1,f2]

因此,当您致电:

if doRectsOverlap(j['rect'], floors['line']):
    j['jump'] = STILL

您收到的消息是告诉您需要一个索引作为int

for n in range(len(floors)):
    if doRectsOverlap(j['rect'], floors[n]['line']):
       j['jump'] = STILL

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM