pygame 表面对象不可订阅
pygame surface object not subscribable
这是我的代码
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
如果 player_location[1] > screen[1] - player_image.get_height():
,它一直在第 22 行说 'pygame.Surface' 对象不可订阅
我正在学习 pygame 上的教程,它教我有关动量的知识,它向我发送了错误消息,我尝试用谷歌搜索它,它总是说“记住丢失的逗号”,它显示了 screen.blit我的代码部分应该是名为“player_location”的变量,我是 python 和 pygame 的新手,我不知道怎么了
您正在尝试访问 screen[1]
,但 screen
的类型为 pygame.Surface
。要获得 screen
的高度,请改用 screen.get_height()
:
if player_location[1] > screen.get_height() - player_image.get_height():
这是我的代码
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
如果 player_location[1] > screen[1] - player_image.get_height():
,它一直在第 22 行说 'pygame.Surface' 对象不可订阅我正在学习 pygame 上的教程,它教我有关动量的知识,它向我发送了错误消息,我尝试用谷歌搜索它,它总是说“记住丢失的逗号”,它显示了 screen.blit我的代码部分应该是名为“player_location”的变量,我是 python 和 pygame 的新手,我不知道怎么了
您正在尝试访问 screen[1]
,但 screen
的类型为 pygame.Surface
。要获得 screen
的高度,请改用 screen.get_height()
:
if player_location[1] > screen.get_height() - player_image.get_height():