AttributeError: module 'pygame' has no attribute 'KEYLEFT' [duplicate] - python

This question already has answers here:
How to get keyboard input in pygame?
(11 answers)
Closed 2 years ago.
I am learning Python in Python Crashcourse and am making the Alien Invasion game.
This is what I have in ship.py file to control the Spaceship's X position
# Movement flag
self.moving_right = False
self.moving_left = False
def update(self):
"""Update the ship''s position based on the movement flag"""
if self.moving_right:
self.rect.x += 1
if self.moving_left:
self.rect.x -= 1
And here is the main alien_invasion.py file using pygame.KEYLEFT and KEYRIGHT
def _check_events(self):
# Watch for keyboard and mouse movements.
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
self.ship.moving_right = True
elif event.key == pygame.KEYLEFT:
self.ship.moving_left = True
elif event.type == pygame.KEYUP:
if event.key == pygame.KEYRIGHT:
self.ship.move_right = False
elif event.key == pygame.KEYLEFT:
self.ship.moving_left = False
And I keep getting the error
AttributeError: module 'pygame' has no attribute 'KEYLEFT' when I run the game and press the left key. Sorry if this is an overload of stuff for a small problem, not sure how much info is needed to help fix. Thank you

There is a KEYUP and KEYDOWN, but no KEYRIGHT and KEYLEFT. You are looking for K_LEFT and K_RIGHT.
def _check_events(self):
# Watch for keyboard and mouse movements.
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
self.ship.moving_right = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
self.ship.move_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False

Related

how to make blit rotate toward mouse? [duplicate]

This question already has answers here:
How to rotate an image(player) to the mouse direction?
(2 answers)
How do I make my player rotate towards mouse position?
(1 answer)
Closed 9 months ago.
This post was edited and submitted for review 9 months ago and failed to reopen the post:
Original close reason(s) were not resolved
so im making a top down shooter game and i want to make the player face the mouse at all times. whenever i search it up, i cant figure out what the answer actually means. ive tried:
mouseY, mouseX = pygame.mouse.get_pos()
angle_to_pointer = math.degrees(math.atan2(T_rect.y - mouseY, T_rect.x - mouseX)) + 180
but that only makes it spin uncontrollably off the screen.
heres my main loop:
run = True
while run:
screen.blit(Tonk,(T_rect))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
pr_l = True
if event.key == pygame.K_RIGHT:
pr_r = True
if event.key == pygame.K_UP:
pr_u = True
if event.key == pygame.K_DOWN:
pr_d = True
if event.key == pygame.K_SPACE:
pr_s = True
elif event.type == pygame.KEYUP: # check for key releases
if event.key == pygame.K_LEFT: # left arrow turns left
pr_l = False
elif event.key == pygame.K_RIGHT: # right arrow turns right
pr_r = False
elif event.key == pygame.K_UP: # up arrow goes up
pr_u = False
elif event.key == pygame.K_DOWN: # down arrow goes down
pr_d = False
elif event.key == pygame.K_SPACE:
pr_s = False
elif event.type == pygame.MOUSEMOTION:
m_m = True
if pr_l == True:
screen.fill(colour)
T_rect.x -= speed
screen.blit(Tonk,(T_rect))
if pr_r == True:
screen.fill(colour)
T_rect.x += speed
screen.blit(Tonk,(T_rect))
if pr_u == True:
screen.fill(colour)
T_rect.y -= speed
screen.blit(Tonk,(T_rect))
if pr_d == True:
screen.fill(colour)
T_rect.y += speed
screen.blit(Tonk,(T_rect))
if m_m == True:
screen.fill(colour)
screen.blit(Tonk,(T_rect))
pygame.display.flip()
clock.tick(60)
please help?
Edit: answered on my previous question: how can i make an image point towards the mouse in python

key.event confusion // pygame.K_a doesn't work

I'm new in this python industry, when i press 'd' my character should go right, but this isn t hapening, same thing to the 'a'. When I change K_a and K_d with K_LEFT and K_RIGHT it works, my character is moving well, but if I wanna make the character move with 'a' and 'd', it doesn't work, what's wrong with my code:
import pygame
import sys
import os
pygame.init()
screen = pygame.display.set_mode((1366,768))
pygame.display.set_caption("Adventure in the Woods")
icon = pygame.image.load('tree.png')
pygame.display.set_icon(icon)
#Player
playerImg = pygame.image.load('ninja.png')
playerX = 100
playerY = 650
velocity = 0.1
run = True
while run:
screen.fill( (9, 66, 2) )
screen.blit(playerImg, (playerX,playerY) )
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
print('a')
playerX -= velocity
if event.key == pygame.K_d:
print('d')
playerX += velocity
pygame.display.update()
Problem Insight
The problem lies in the scope of your if event.type == pygame.KEYDOWN: ... as it should be within the for-loop for event in pygame.event.get(): ... :
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN: #<-------------- From here
if event.key == pygame.K_a:
print('a')
playerX -= velocity
if event.key == pygame.K_d:
print('d')
playerX += velocity #<-------------- to here
Solution
This can be fixed by indenting the code block starting from if event.type == pygame.KEYDOWN: ..., like so:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN: #<-------------- Now inside the for-loop
if event.key == pygame.K_a:
print('a')
playerX -= velocity
if event.key == pygame.K_d:
print('d')
playerX += velocity
Other Remarks
To be more familiar with python code structures and styles, review some of the fundamental syntaxes like Indentation by visiting the style guide # https://www.python.org/dev/peps/pep-0008/
Cheers!

Pygame- Disabling a Key with pygame.event.set_blocked() crashes window and returns error

I tried to add jumping physics to my game but it is necessary that after the player jumps, the Jump-Key has to be disabled until the player lands again.
Now, there isn't any problem with the gravity, but if the player keeps holding K_UP he could fly forever and the he can fly in a paarabola as he should, so I need to disable jump until the character lands and the jump Loop loops out as expected.
I tried pygame.event.set_blocked(pygame.K_UP) but when I jump the window crashes and there is an error code: "pygame.event.set_blocked(pygame.K_UP) ValueError: event type out of range".
As I couldn't find anything about this command expect that it exists I probablay did an mistake in the Game-Loop using .block().
Here is the Game-Loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#player1 buttons
if event.type == pygame.KEYDOWN:
#move
if event.key == pygame.K_LEFT:
playerX_change = -1
if event.key == pygame.K_RIGHT:
playerX_change = 1
#Jump
if event.key == pygame.K_UP:
playerY_change = -3
player_jump = True
pygame.event.set_blocked(pygame.K_UP)
if player_jump == True:
playerY_change += gravity
# for X player 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
#for Y player 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
if playerY == 900:
playerY_change = 0
player_jump = False
pygame.event.set_allowed(pygame.K_UP)
I'm new to Pygame (and programming in general) and treid a lot of things and this is the result, so maybe I have also unnecessary code in it. My problem is the part with pygame.event.set_blocked and pygame.event.set_allowed which I don't know how to use.
pygame.event.set_blocked cannot block a key, it can just block an event such as KEYDOWN or KEYUP.
Anyway, you don't have to "block" anything. Uses the Boolean state variable player_jump instead:
if not player_jump and event.key == pygame.K_UP:
playerY_change = -3
player_jump = True
playerY_change += gravity

PyGame not registering a third key press [duplicate]

This question already has an answer here:
Python Pygame press two direction key and another key to shoot there's no bullet
(1 answer)
Closed 2 years ago.
I'm making my first pygame project and it has been going smoothly. I wrote the following code to check for inputs and so far it was working ok.
def check_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running, self.playing = False, False
self.curr_menu = False
self.curr_level.run_level = False
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
self.DOWN_KEY = True
if event.key == pygame.K_UP:
self.UP_KEY = True
if event.key == pygame.K_RETURN:
self.START_KEY = True
if event.key == pygame.K_BACKSPACE or event.key == pygame.K_ESCAPE:
self.BACK_KEY = True
if event.key == pygame.K_LEFT:
self.LEFT_KEY = True
if event.key == pygame.K_RIGHT:
self.RIGHT_KEY = True
if event.key == pygame.K_SPACE:
self.SPACE_KEY = True
if event.key == pygame.K_RSHIFT or event.key == pygame.K_LSHIFT:
self.SHIFT_KEY = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN:
self.DOWN_KEY = False
if event.key == pygame.K_UP:
self.UP_KEY = False
if event.key == pygame.K_RETURN:
self.START_KEY = False
if event.key == pygame.K_BACKSPACE or event.key == pygame.K_ESCAPE:
self.BACK_KEY = False
if event.key == pygame.K_LEFT:
self.LEFT_KEY = False
if event.key == pygame.K_RIGHT:
self.RIGHT_KEY = False
if event.key == pygame.K_SPACE:
self.SPACE_KEY = False
if event.key == pygame.K_RSHIFT or event.key == pygame.K_LSHIFT:
self.SHIFT_KEY = False
I wanted to add a sprinting for the x (SHIFT) and y (SPACE) axis it doesn't work properly. Whenever hold down SPACE and RIGHT, pygame doesn't register the UP key getting pressed and whenever I hold SPACE and LEFT, pygame doesn't register both UP and DOWN.
Somehow, holding shift doesn't mess with the registration of the arrow keys.
Because of the issues I was experiencing with the sprinting I started investigating further and realized that pygame doesn't recognize me pressing the UP key when LEFT and RIGHT are pressed simultaneously, but recognizes the DOWN key press.
Is the problem in my code?
If you want to deal with pressed events you need to use:
pygame.key.get_pressed()
It returns the state of the key is pressed or not (True, False)
Example:
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_LSHIFT]:
sprintX()
if key_pressed[pygame.K_SPACE]:
sprintY()

On PyCharm my left key and right key isn't responding

#enable pygame mode
import pygame
pygame.init()
#create screen
screen = pygame.display.set_mode((900,600))
#Title + Logo
pygame.display.set_caption("Space Invader")
icon = pygame.image.load("chicken.png")
pygame.display.set_icon(icon)
#Player icon
player_icon = pygame.image.load("spaceship.png")
playerX = 400
playerY = 500
def player(x, y):
screen.blit(player_icon, (x, y))
#game loop
running = True
while running:
# backround colour RGB
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#If key pressed check wether its right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
print("key left pressed")
if event.key == pygame.K_RIGHT:
print("key right pressed")
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
print("key stroke has benn released")
#Player change in coordinates
playerX += 0
playerY += 0
player(playerX, playerY)
pygame.display.update()
I have been learning about pygame and game programing using python during this quarantine. I have been doing this by watching a tutorial on youtube. Please don't downgrade me I have tried harder to make my question better last time it was my first question and got 2 downgrades. Thankyou for your time.
It s a matter of Indentation. In your code the pygame.KEYDOWN event is only evaluated in case of event.type == pygame.QUIT. "Move" the pygame.KEYDOWN event handling:
running = True
while running:
# backround colour RGB
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# <--| INDENTATION
#If key pressed check wether its right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
print("key left pressed")
if event.key == pygame.K_RIGHT:
print("key right pressed")
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
print("key stroke has benn released")

Categories

Resources