Reputation: 55
Im making a function for my flappy bird clone that takes the Pipe
class and the PipeInvert
class, and from the possibleLevels
list selects one of 2 level Y numbers where the first one gets asigned to the normal pipe and the second one to the inverted pipe. But when running in keep getting this TypeError: 'int' object is not subscriptable
in line 59
. Im using pygame.
Code so far:
import pygame
from pygame.locals import *
from random import randint
pygame.init()
clock = pygame.time.Clock()
fps = 60
screen_width = 864
screen_height = 936
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Flappy Bird')
#load images
bg = pygame.image.load(r'C:\Users\User\Desktop\code\Flappy Bird\img\bg.png')
ground_img = pygame.image.load(r'C:\Users\User\Desktop\code\Flappy Bird\img\ground.png')
pipeImage = pygame.image.load(r'C:\Users\User\Desktop\code\Flappy Bird\img\pipe.png')
pipeImageInv = pygame.image.load(r'C:\Users\User\Desktop\code\Flappy Bird\img\pipeinv.png')
#Classes
class Pipe():
def __init__(self, img, x , y, pipe_scroll, scroll_speed):
self.img = pipeImage
self.x = x
self.y = y
self.pipe_scroll = pipe_scroll
self.scroll_speed = scroll_speed
def move(self):
screen.blit(self.img, (self.pipe_scroll, y))
self.pipe_scroll -= self.scroll_speed
if self.pipe_scroll < -77:
self.pipe_scroll = 864
class PipeInverted():
def __init__(self, img, x , y, pipe_scroll, scroll_speed):
self.img = pipeImageInv
self.x = x
self.y = y
self.pipe_scroll = pipe_scroll
self.scroll_speed = scroll_speed
def moveinvert(self):
screen.blit(self.img, (self.pipe_scroll, y))
self.pipe_scroll -= self.scroll_speed
if self.pipe_scroll < -77:
self.pipe_scroll = 864
def levelSpawn():
posLevels = [[220,-50], [250, -80]]
select = randint(1,2)
newNormal = Pipe(pipeImage, 5, posLevels[select[1]])
newInvert = Pipe(pipeImage, 5, posLevels[select[2]])
newNormal.move()
newInvert.moveinvert()
#define game variables
ground_scroll = 0
pipe_scroll = 720
scroll_speed = 4
pipaKanonikh = Pipe(pipeImage, 5, 6, pipe_scroll, scroll_speed)
pipaAnapodh = PipeInverted(pipeImageInv, 5, 5, pipe_scroll, scroll_speed)
run = True
while run:
clock.tick(fps)
#draw background
screen.blit(bg, (0,0))
#draw and scroll pipe
levelSpawn()
#draw and scroll the ground
screen.blit(ground_img, (ground_scroll, 768))
ground_scroll -= scroll_speed
if abs(ground_scroll) > 0:
ground_scroll = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
pygame.quit()
Upvotes: 0
Views: 1712
Reputation: 50
You have to convert select
to a string or a list first
select = str(randint(1,2))
Upvotes: 0
Reputation: 41
On lines 59 and 60 you are trying to select the first value of select
which is of type integer
(and not a list
or another subscriptable type).
Another problem you might encounter is an IndexError
(first index of a list
is 0
in python)
Depending on what you want to do you have two ways of fixing this error:
The following would probably work:
newNormal = Pipe(pipeImage, 5, posLevels[select][0])
newInvert = PipeInverted(pipeImage, 5, posLevels[select][1])
# OR
newNormal = Pipe(pipeImage, 5, posLevels[0][select])
newInvert = PipeInverted(pipeImage, 5, posLevels[1][select])
(I changed the type of newInvert
into PipeInverted
because the class Pipe
doesn't have a moveInvert
method)
Upvotes: 1