Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

What is wrong with my code? NameError: name 'topDownPlayerControls' is not defined

Hello there! This is my first time using tree house in lord knows how long. I'm here to ask what is the problem with my code? I'm making a game engine, and I thought the easiest way to sort player types would be putting them into a different script and using them there. However, I am having a slight issue doing that, and have no clue how to fix it.

Edit: I fixed it! Kind of... The player doesn't spawn in though and it doesn't print anything when pressing the keys.

main.py

main.py:

import pygame
from playerSelection import *
from gui import *
from tkinter import *

import ctypes
from ctypes.wintypes import HWND, LPWSTR, UINT

# Define stuff
player = playerMovement()

# Window Size
display_w = 1366
display_h = 768

# Initialize and Set Display
pygame.init()
gameDisplay = pygame.display.set_mode((display_w, display_h))
pygame.display.set_caption("The Joker Engine (Version Undecided) - By Harrison Court")
clock = pygame.time.Clock()

# Ctypes ID's
IDYES = 6
IDNO = 7

x = (display_w * 0.45)
y = (display_h * 0.8)

# Colours
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)

running = True

gameDisplay.fill(white)

# Make sure the game isn't dying on us.
while running:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:

            # Misc.
            if event.key == pygame.K_ESCAPE:
                quitWindow = ctypes.windll.user32.MessageBoxW(0, "Are you sure you want to quit?", "Warning!", 4)

                # Confirm they want to leave.
                if quitWindow == IDYES:
                    quit()

    player.displayPlayer(x,y)
    player.topDownPlayerControls


pygame.display.update()
clock.tick(60)

pygame.quit()
quit()

playerSelection.py:

import pygame

from tkinter import Tk, Frame, Menu

import ctypes
from ctypes.wintypes import HWND, LPWSTR, UINT

x = (300)
y = (300)

display_w = 1366
display_h = 768

gameDisplay = pygame.display.set_mode((display_w, display_h))
playerImg = pygame.image.load('Player.png')

class playerMovement():

    def displayPlayer(self,x,y):
        gameDisplay.blit(playerImg, (x,y))

    def topDownPlayerControls(self):

        if event.type == pygame.KEYDOWN:
            # Left & Right
            if event.key == pygame.K_LEFT:
                x_change = -5
                print("Left")
            elif event.key == pygame.K_RIGHT:
                x_change = 5
                print("Right")

            # Up & Down
            if event.key == pygame.K_DOWN:
                y_change = 5
                print("Down")

            elif event.key == pygame.K_UP:
                y_change = -5
                print("Up")

        # If no keys are pressed, do this...
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                x_change = 0
            if event.key == pygame.K_DOWN or event.key == pygame.K_UP: 
                y_change = 0

        # Player Movement
        x += x_change
        y += y_change