Dec-27-2019, 06:34 AM
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;
but when I try to make it move with keys like this;
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...
An example is, that with this code I could make a window with a red box;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
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() |
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...