我无法在 pygame 中使用 blit 方法
I can't use the blit method in pygame
我正在关注 youtube 上关于使用 pygame 构建平台游戏的教程。
我收到错误:Traceback(最后一次通话):
文件 I:\Home\PYGAME\main.py 中的第 23 行
screen.blit(player_image, player_Pos)
AttributeError: 'NoneType' 对象没有属性 'blit'
我该如何解决?
这是我的代码:
import pygame
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
SCREEN_SIZE = (500,500)
screen = pygame.display.set_caption('first pygame project')
pygame.display.set_mode(SCREEN_SIZE, 0, 32)
moving_Right = False
moving_Left = False
player_Pos = (50,350)
velocity = 10
player_y_momentum = 0
running = True
player_image = pygame.image.load('I:\Home\PYGAME\player_icon.png')
while running:
screen.blit(player_image, player_Pos)
if moving_Right == True:
player_Pos[0] += velocity
if moving_Left == True:
player_Pos[0] -= velocity
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == KEYDOWN:
if event.key == K_RIGHT:
moving_Right = True
if event.key == K_LEFT:
moving_Left = True
if event.type == KEYUP:
if event.key == K_RIGHT:
moving_Right = False
if event.key == K_LEFT:
moving_Left = False
pygame.display.update()
clock.tick(60)
更改此部分:
screen = pygame.display.set_caption('first pygame project')
pygame.display.set_mode(SCREEN_SIZE, 0, 32)
为此:
screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption('first pygame project')
您所做的不允许将屏幕用作对象。
我正在关注 youtube 上关于使用 pygame 构建平台游戏的教程。 我收到错误:Traceback(最后一次通话): 文件 I:\Home\PYGAME\main.py 中的第 23 行 screen.blit(player_image, player_Pos) AttributeError: 'NoneType' 对象没有属性 'blit'
我该如何解决?
这是我的代码:
import pygame
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
SCREEN_SIZE = (500,500)
screen = pygame.display.set_caption('first pygame project')
pygame.display.set_mode(SCREEN_SIZE, 0, 32)
moving_Right = False
moving_Left = False
player_Pos = (50,350)
velocity = 10
player_y_momentum = 0
running = True
player_image = pygame.image.load('I:\Home\PYGAME\player_icon.png')
while running:
screen.blit(player_image, player_Pos)
if moving_Right == True:
player_Pos[0] += velocity
if moving_Left == True:
player_Pos[0] -= velocity
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == KEYDOWN:
if event.key == K_RIGHT:
moving_Right = True
if event.key == K_LEFT:
moving_Left = True
if event.type == KEYUP:
if event.key == K_RIGHT:
moving_Right = False
if event.key == K_LEFT:
moving_Left = False
pygame.display.update()
clock.tick(60)
更改此部分:
screen = pygame.display.set_caption('first pygame project')
pygame.display.set_mode(SCREEN_SIZE, 0, 32)
为此:
screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption('first pygame project')
您所做的不允许将屏幕用作对象。