Search code examples
pythonpygameattributeerrorblit

I can't use the blit method in pygame


I'm following a tutorial on youtube for building a platformer with pygame. I'm getting the error: Traceback(most recent call last): File I:\Home\PYGAME\main.py line 23 in screen.blit(player_image, player_Pos) AttributeError: 'NoneType' Object has no attribute 'blit'

How do I solve this?

Here's my code:

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)

Solution

  • Change this part:

    screen = pygame.display.set_caption('first pygame project')
    pygame.display.set_mode(SCREEN_SIZE, 0, 32)
    

    To this:

    screen = pygame.display.set_mode(SCREEN_SIZE)
    pygame.display.set_caption('first pygame project')
    

    What you have done does not allow you to use screen as an object.