pygame surface object not subscribable

Viewed 29

heres my code

import pygame, sys
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((800,600))

pygame.display.set_caption("hello mom and dad")

player_image = pygame.image.load('player.png')
player_location = [355,245]

move_right = False
move_left = False

player_y_momentum = 0

while True: #codes written outside of this loop wont run in the window or get executed
screen.fill((146, 244, 255))

screen.blit(player_image, [155, 245])#player model

if player_location[1] > screen[1] - player_image.get_height():
    player_y_momentum = -player_y_momentum
else:
    player_location[1] += player_y_momentum

if move_right == True:
    player_location[0] += 1
if move_left == True:
    player_location[0] -= 1


for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
    if event.type == KEYDOWN:
        if event.key == K_RIGHT:
            move_right = True
        if event.key == K_LEFT:
            move_left = True
    if event.type == KEYUP:
        if event.key == K_RIGHT:
            move_right = False
        if event.key == K_LEFT:
            move_left = False
pygame.display.update() #put stuff above this code to put it in the game

it keeps saying 'pygame.Surface' object is not subscriptable in line 22 on if player_location[1] > screen[1] - player_image.get_height():

i was following a tutorial on pygame and it was teaching me about momentum and it spammed me that error i tried googling it it always says "remeber the missing comas" it shows the screen.blit part my code was supposed to be variable called "player_location" im new to python and pygame and i dont know whats wrong

1 Answers

You're trying to access screen[1], but screen is of type pygame.Surface. To get the height of screen, use screen.get_height() instead:

if player_location[1] > screen.get_height() - player_image.get_height():

Related