Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pygame has missing files.
#1
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...
Reply
#2
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.
99 percent of computer problems exists between chair and keyboard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to find the pygame example files? Piethon 12 9,898 Sep-04-2019, 11:29 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

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