Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7057551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:59:59+00:00 2026-05-28T03:59:59+00:00

When i try to run this simple game. it says: screen.blit(plumbers,(50 + M ,337

  • 0

When i try to run this simple game. it says:

    screen.blit(plumbers,(50 + M ,337 + N))
UnboundLocalError: local variable 'M' referenced before assignment

this is from the bellow code:

import pygame
import os, sys
import itertools
import pygame
from pygame.sprite import Sprite

cloud_background = pygame.image.load('clouds.bmp')
brick_tile = pygame.image.load('brick_tile.png')
plumbers = pygame.image.load('Mario_sideways_sprite_2xL.png')

pink = (255, 64, 64)
w = 640
h = 480
screen = pygame.display.set_mode((w, h))
running = 1

def setup_background():
    screen.fill((pink))
    screen.blit(cloud_background,(0,0))
    brick_width, brick_height = brick_tile.get_width(), brick_tile.get_height()
    for x,y in itertools.product(range(0,640,brick_width),
                                 range(390,480,brick_height)):
        # print(x,y)
        screen.blit(brick_tile, (x,y))


def show_sprites():
    screen.blit(plumbers,(50 + M ,337 + N))
    if event.key == pygame.K_UP:
        M = 1
        N = 0

while running:
    setup_background()
    show_sprites()
    move()
    event = pygame.event.poll()
    if event.type == pygame.QUIT: sys.exit()

But when i add the variable above “screen.blit” so it looks like this:

def show_sprites():
    if event.key == pygame.K_UP:
        M = 1
        N = 0
    screen.blit(plumbers,(50 + M ,337 + N))

I get this error:

Traceback (most recent call last):
  File "C:\Users\Enrique\Dropbox\GAMEZ_PYGAME\gamez.py", line 36, in <module>
    show_sprites()
  File "C:\Users\Enrique\Dropbox\GAMEZ_PYGAME\gamez.py", line 28, in show_sprites
    if event.key == pygame.K_UP:
NameError: global name 'event' is not defined

What do i have to add/ change to get these simple controls to work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:59:59+00:00Added an answer on May 28, 2026 at 3:59 am

    That means you have two problems. Python sees the first one and stops with an exception.

    In :

    def show_sprites():
        screen.blit(plumbers,(50 + M ,337 + N))
        .......
    

    M and N still don’t exist when called.
    Try for example:

    def show_sprites():
        print M, N
        screen.blit(plumbers,(50 + M ,337 + N))
        .......
    

    In

    def show_sprites():
        if event.key == pygame.K_UP:
    

    neither event has been defined.

    You should check a tutorial about function arguments/parameters and local variables.

    your function should looks like this (for example):

    def show_sprites(M, N, event):
    

    or most likely:

    def show_sprites(event):
        if event.key == pygame.K_UP:
            M = 1
            N = 0
        else:
            M = 0  # ? up to you
            N = 0  # ?
        screen.blit(plumbers,(50 + M ,337 + N))
    

    with:

    while running:
        setup_background()
        event = pygame.event.poll()
        if event.type == pygame.QUIT: sys.exit()
        show_sprites(event)
        move()
    

    Finally, this code will send all kind of events to your function so you should filter them:

    if event.type == pygame.QUIT:
        sys.exit()
    elif event.type == pygame.KEYDOWN:
        show_sprites(event)
        move()
    

    check this for example

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this script and when i try to run it, it just says
I try to run a simple aop example in this site. I have spring
I'm trying to run this simple query: try { $dsn = mysql:host=localhost;dbname:mydb; $PDO =
If I try to run this query in SQL Server 2005: SELECT 1 WHERE
I get a generic failure when I try to run: saslpasswd2 username This was
Whenever i try to run sqlmetal, i get this: 'sqlmetal' is not recognized as
I get this, when I try to run my PHP. When I comment out
I've set up this simple script on my local machine: #!/usr/bin/python import socket from
Ok I have a very simple mysql database but when i try to run
I've got a very simple question about a game I created (this is not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.