python - Pygame: Colliding Rectangle on multiple other rectangles -


i attempting create game in block moves , forth until player presses space. upon which, block jumps next line , stops.

currently having problems collision code.

the error being thrown shell is:

    if dorectsoverlap(j['rect'], floors['line']): typeerror: list indices must integers, not str 

i stuck understanding code has gone wrong. knowledge of how python works limited.

there code have commented out floor moving dowards when player jumps. has been commented out until can collisions working, still included

code below:

import pygame, sys, time pygame.locals import *   def dorectsoverlap(rect1, rect2):     a, b in [(rect1, rect2), (rect2, rect1)]:         # check if a's corners 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) , (x < rect.right) , (y > rect.top) , (y < rect.bottom):         return true     else:         return false  # set pygame pygame.init() mainclock = pygame.time.clock()  # set window windowwidth = 480 windowheight = 800 windowsurface = pygame.display.set_mode((windowwidth, windowheight), 0, 32) pygame.display.set_caption('jumper')  #directions left = 4 right = 6 = 8 down = 2 still = 5  #blocks location jumping #blocklocy = 700  #binary stopping movement #stopper = 0  movespeed = 1  # set 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 game loop while true:     # check quit event     event in pygame.event.get():         if event.type == quit:             pygame.quit()             sys.exit()        # draw black background onto surface     windowsurface.fill(black)           # move 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'] =      if dorectsoverlap(j['rect'], floors['line']):         j['jump'] = still    #floor controll code moving level - not working    # 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 block onto surface         pygame.draw.rect(windowsurface, j['color'], j['rect'])          pygame.draw.rect(windowsurface, f['color'], f['line'])       # draw window onto screen     pygame.display.update()     mainclock.tick(40) 

you creating 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] 

so when call:

if dorectsoverlap(j['rect'], floors['line']):     j['jump'] = still 

you're message telling you need index int:

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

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -