Python Forum

Full Version: Pygame has missing files.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

DamonTattersfield

I'm watching this youtubers series on pygame, and I can make some stuff. I can make a window, and draw a box with a specific color, but for some reason, when I keep making new codes it pops up with things like "init is not defined" or "key is not defined.
An example is, that with this code I could make a window with a red box;

import pygame
pygame.init()

win = pygame.display.set_mode((400,600))

pygame.display.set_caption("My game")

x = 50
y = 50
width = 40
height = 60
vel = 5

run = True
while run:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
    pygame.display.update()

pygame.quit()
but when I try to make it move with keys like this;
import pygame
pygame.init()

win = pygame.display.set_mode((400,600))

pygame.display.set_caption("My game")

x = 50
y = 50
width = 40
height = 60
vel = 5

run = True
while run:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.keys.get_pressed()

    if keys[pygame.K_LEFT]:
        x -= vel
    if keys[pygame.K_RIGHT]:
        x += vel
    if keys [pygame.K_RIGHT]:
        y -= vel
    if keys [pygame.K_DOWN]:
        y +=vel

    pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
    pygame.display.update()

pygame.quit()
It says;
Traceback (most recent call last):
File "C:\Users\Damon Tattersfield\Desktop\video game\code\python codes\pygame mini test.py", line 22, in <module>
keys = pygame.keys.get_pressed()
AttributeError: module 'pygame' has no attribute 'keys'

even though the youtuber doing it along with me had no problems. Did I install pygame wrong? should I reinstall it?
thanks...
Pygame doesn't have attribute to keys. Pygame has attribute key.
If you can run code. Then next project doesn't. It normally a typo or misuse of command.
Pygame Docs.