Python Forum
Pygame Tutorials Part 3 Image Handling
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pygame Tutorials Part 3 Image Handling
#1
Hi, I've recently started the Tutorials and am picking up some great tips. However, I'm getting an error message with the keys event:

Error:
Traceback (most recent call last): File "/data/user/0/ru.iiec.pydroid3/files/temp_iiec_codefile.py", line 34, in <module> player.update(keys) ^^^^
NameError: name 'keys' is not defined

Can you advise how I fix this?

Full code:

import pygame as pg
   
pg.init()
   
class Player:
    def __init__(self, screen_rect):
        self.image = pg.image.load('spaceship.png').convert()
        self.image.set_colorkey((255,0,255))
        self.transformed_image = pg.transform.rotate(self.image, 180)
        self.rect = self.image.get_rect(center=screen_rect.center)
        self.speed = 5
          
    def update(self, keys):
        if keys[pg.K_LEFT]:
            self.rect.x -= self.speed
        elif keys[pg.K_RIGHT]:
            self.rect.x += self.speed
           
    def draw(self, surf):
        surf.blit(self.transformed_image, self.rect)
   
screen = pg.display.set_mode((800,600))
screen_rect = screen.get_rect()
player = Player(screen_rect)
clock = pg.time.Clock()
done = False
while not done:
    screen.fill((0,0,0))
    keys = pg.key.get_pressed()
    for event in pg.event.get(): 
        if event.type == pg.QUIT:
            done = True
    player.update(keys)
    player.draw(screen)
    pg.display.update()
    clock.tick(60)
Thanks and sorry if I've done anything wrong in this post.
Larz60+ write Apr-07-2025, 08:17 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Tags have been added for you this time. Please use BBCode tags on future posts.
Also when posting traceback, be sure to include entire, unaltered traceback as it contains useful debugging info.
Reply
#2
Thanks Larz60 for doing that for me.

The full Traceback error was:

Error:
Traceback (most recent call last): File "/data/user/0/ru.iiec.pydroid3/files/temp_iiec_codefile.py", line 34, in <module> player.update(keys) ^^^^ NameError: name 'keys' is not defined
As I've recently started the tutorial this is the only error (so far!). No doubt I'll get more as I progress.
Reply
#3
put a print statement after:
keys = pg.key.get_pressed()
print(f'keys: {keys}')
is keys populated?
Reply
#4
Thanks again Larz60+

Error message has gone.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pygame Zero - no actor image toiling_away 2 3,188 Oct-14-2022, 12:16 AM
Last Post: toiling_away
  How to make an image move in relation to another in PYGAME CompleteNewb 1 2,960 Nov-10-2021, 03:38 PM
Last Post: metulburr
  open cv ->pygame: turn image into location Gamedeveloper 1 2,666 Jan-20-2020, 05:00 PM
Last Post: michael1789
  Handling multiple keyboard entries in Pygame mrcrease 2 5,711 Dec-13-2019, 01:12 AM
Last Post: Windspar
  [PyGame] pygame image loading error BlueClaw 6 8,225 Dec-10-2019, 08:50 PM
Last Post: BlueClaw
  pygame uploading image from opengameart fatimabttt 3 4,191 Apr-16-2019, 08:50 PM
Last Post: nilamo
  Finding the brightness of an image using pygame Zman350x 2 4,541 Feb-21-2019, 12:20 PM
Last Post: Zman350x
  Pygame loads only part of images Prof_Jar_Jar 3 4,250 Dec-22-2018, 03:42 PM
Last Post: Windspar
  [pyGame] Load Image, Problem Found ! JamieVanCadsand 2 10,268 Sep-29-2017, 06:26 PM
Last Post: metulburr
  [Pygame] Avoid SentDex pygame tutorials georgecoopers 1 4,717 Apr-09-2017, 12:40 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020