Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

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.

1 Answer

0 votes
by (25.1k points)

The issue is being caused because in your function makeNewFish() you are not returning anything. I think you should return the variable fi. If you don't return anything by default in python None is return, which is what you are trying to add in your allfish list and that is causing the error. Add return fi at the end of function makeNewFish()

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 10, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Dec 19, 2020 in Python by ashely (50.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...