0

I am trying to edit this project: https://inventwithpython.com/pygame/chapter5.html.

I wanted to change the rectangles to images, and I read that using the blit function is the way to do that. Here's what I have so far:

import random, sys, time, pygame
from pygame.locals import *

FPS = 30
WINDOWWIDTH = 640
WINDOWHEIGHT = 480
FLASHSPEED = 500 # in milliseconds
FLASHDELAY = 200 # in milliseconds
BUTTONSIZE = 200
BUTTONGAPSIZE = 20
TIMEOUT = 10 # seconds before game over if no button is pushed.

#                R    G    B
WHITE        = (255, 255, 255)
BLACK        = (  0,   0,   0)
BRIGHTRED    = (255,   0,   0)
RED          = (155,   0,   0)
BRIGHTGREEN  = (  0, 255,   0)
GREEN        = (  0, 155,   0)
BRIGHTBLUE   = (  0,   0, 255)
BLUE         = (  0,   0, 155)
BRIGHTYELLOW = (255, 255,   0)
YELLOW       = (155, 155,   0)
DARKGRAY     = ( 40,  40,  40)
bgColor = BLACK

XMARGIN = int((WINDOWWIDTH - (2 * BUTTONSIZE) - BUTTONGAPSIZE) / 2)
YMARGIN = int((WINDOWHEIGHT - (2 * BUTTONSIZE) - BUTTONGAPSIZE) / 2)

#load images
pikachu=pygame.image.load('pikachu.png')
squirtle=pygame.image.load('squirtle.png')
charmander=pygame.image.load('charmander.png')
bulbasaur=pygame.image.load('bulbasaur.png')

# Rect objects for each of the four buttons
YELLOWRECT = pygame.Rect(XMARGIN, YMARGIN, BUTTONSIZE, BUTTONSIZE)
BLUERECT   = pygame.Rect(XMARGIN + BUTTONSIZE + BUTTONGAPSIZE, YMARGIN, BUTTONSIZE, BUTTONSIZE)
REDRECT    = pygame.Rect(XMARGIN, YMARGIN + BUTTONSIZE + BUTTONGAPSIZE, BUTTONSIZE, BUTTONSIZE)
GREENRECT  = pygame.Rect(XMARGIN + BUTTONSIZE + BUTTONGAPSIZE, YMARGIN + BUTTONSIZE + BUTTONGAPSIZE, BUTTONSIZE, BUTTONSIZE)

#blit images onto buttons
blit(pikachu, YELLOWRECT)

The only thing I changed from the example is adding the images and trying to use blit at the end.

The error message says:

NameError: name 'blit' is not defined".

I can't find any information on this, do I need to initialize something to make blit work?

Yuchen Ren
  • 287
  • 5
  • 13

1 Answers1

0

blit is a method on a pygame display.

i.e.

# Assuming there exists some player class with attributes `sprite` & `position`
from Player import Player

player = Player()
display = pygame.display
screen = pygame.display.set_mode(size=(WINDOW_WIDTH, WINDOW_HEIGHT))

screen.blit(player.sprite, player.position)
Leshawn Rice
  • 617
  • 4
  • 13
  • I changed blit(pikachu, YELLOWRECT) to screen.blit(pikachu, YELLOWRECT). I didn't get an error message but it just played like the example, I didn't see my image anywhere –  0427092 May 06 '22 at 00:40