Currently I am making a game using pygame in which, at the moment, I am trying to display fish into the screen to appear randomly around the screen. Later, these fish will add points to score. However I get a type error for when I am trying to load some fish into the game. How can I fix this?
Right now, I am follow a good majority of the code similar to the game 'Squirrel eat Squirrel', which I believe can be played on the Raspberry Pi and have also followed some of sentdex's video on YouTube. I have been debugging it by any means that stop the problem, but I do not understand what this error means or how to fix it.
Right now I run the following code:
global screen, grasspic, bearImg, fishpic, screen_width, screen_height
import random
import pygame
import sys
import math
pygame.init()
camerax = 0
cameray = 0
screen_width = 640
screen_height = 480
fishpic = []
for i in range(1, 3):
fishpic.append(pygame.image.load('fish%s.png' % i))
for i in range(3):
allfish.append(makeNewFish(camerax, cameray))
allfish[i]['x'] = random.randint(0, screen_width)
allfish[i]['y'] = random.randint(0, screen_height)
def getRandomOffCameraPos(camerax, cameray, objWidth, objHeight):
cameraRect = pygame.Rect(camerax, cameray, screen_width, screen_height)
while True:
x = random.randint(camerax - screen_width, camerax + (2*screen_width))
y = random.randint(cameray - screen_height, cameray + (2*screen_height))
objRect = pygame.Rect(x, y, objWidth, objHeight)
if not objRect.colliderect(cameraRect):
return x, y
def makeNewFish(camerax, cameray):
fi = {}
fi['fishPicture'] = random.randint(0, len(fishpic) - 1)
fi['width'] = 150
fi['height'] = 150
fi['x'], fi['y'] = getRandomOffCameraPos(camerax, cameray, fi['width'], fi['height'])
fi['rect'] = pygame.Rect((fi['x'], fi['y'], fi['width'], fi['height']))
I hoped the output would have fish randomly appear as if the world was 'infinite', but instead I get an error that reads allfish[i]['x'] = random.randint(0, screen_width)
TypeError: 'None Type' object does not support item assignment"
Is there a easy way for this to be fixed?
I'm sorry if I did not explain this good. If it is needed, I can provide more of the code and try to answer anything I did not explain.