'event' has no "key" attribute - python

** this is a part of a code **
def _cheak_events(self):
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.key == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
self.ship.moving_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
**
error shows that:
38, in _cheak_events
elif event.key == pygame.KEYDOWN:
AttributeError: 'Event' object has no attribute 'key'

Replace all event.key with event.type

Related

Can't get 'function' to move Ship left and right in pygame

New to python and pygame. I was trying to make the ship move left or right continuously, which I did by creating a class "move_ship" and passing self.arguments as false. Then created a function inside which works when self.arguments are True and increase or decreases the x-position value of ship. Then called the class with variable "ship_moving". ...created a event which would set self.argument as true and at last called the function inside the class to move the ship:-
class move_ship():
def __init__(self):
self.moving_left=False
self.moving_right=False
def moving(self):
if self.moving_left:
ship_rect.centerx-=1
if self.moving_right:
ship_rect.centerx+=1
ship_moving=move_ship()
def run_game:
pygame.init()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type ==pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
ship_moving.moving_right=True
elif event.key == pygame.K_LEFT:
ship_moving.moving_left=True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
ship_moving.moving_right=False
elif event.key == pygame.K_LEFT:
ship_moving.moving_left=False
ship_moving.moving()
which worked perfectly fine but the I wanted to know why it didn't work when I tried the same thing but with function only.
moving_right=False
moving_left= False
def move_ship():
if moving_left:
ship_rect.centerx-=1
if moving_right:
ship_rect.centerx+=1
def run_game:
pygame.init()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type ==pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
moving_right=True
elif event.key == pygame.K_LEFT:
moving_left=True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
moving_right=False
elif event.key == pygame.K_LEFT:
moving_left=False
move_ship()
You must use the global statement if you want to set variables in global namespace within a function:
def run_game():
global moving_right, moving_left
pygame.init()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type ==pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
moving_right=True
elif event.key == pygame.K_LEFT:
moving_left=True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
moving_right=False
elif event.key == pygame.K_LEFT:
moving_left=False
If you do not specify moveing_right, moveing_left to be interpreted as global, only local variables with the same name are set in the scope of the run_game function, but the global variables are never changed.

Pygame keys convention

I want to use w a s d keys for movement rather than arrows key
But I can't figure out how to do so with the following piece of code. I want to replace pygame.KEYDOWN
with a button for s but I can't figure out how to do so.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
Pacman.changespeed(-30, 0)
if event.key == pygame.K_RIGHT:
Pacman.changespeed(30, 0)
if event.key == pygame.K_UP:
Pacman.changespeed(0, -30)
if event.key == pygame.K_DOWN:
Pacman.changespeed(0, 30)
You don't need to change the event type. w, a, s, d are represented by pygame.K_w, pygame.K_a, pygame.K_s and pygame.K_d, respectively, but the event type is still pygame.KEYDOWN:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
Pacman.changespeed(-30, 0)
if event.key == pygame.K_a:
Pacman.changespeed(30, 0)
if event.key == pygame.K_w:
Pacman.changespeed(0, -30)
if event.key == pygame.K_s:
Pacman.changespeed(0, 30)
Note: KEYDOWN does not mean the "down" key, but that a key is pressed down. The KEYDOWNevent occurs when a key is pressed and the KEYUP event occurs when a key is released. See pygame.event and pygame.key.

How to correct Syntax error with my elif, if statement. what am I doing wrong?

import sys
import pygame
def check_events(ship):
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
# Move the ship to the right.
ship.moving_right = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
ship.moving_right = False
def update_screen(ai_settings, screen, ship):
'''Update images on the screen and flip to the new screen.'''
Error:
File "/home/mark/python_work/game_functions.py", line 8
elif event.type == pygame.KEYDOWN:
^
SyntaxError: invalid syntax
[Finished in 0.2s with exit code 1]
In your function, your first condition should start with an if.
def check_events(ship):
if event.type == pygame.KEYDOWN: # <--- note the change here
if event.key == pygame.K_RIGHT:
# Move the ship to the right.
ship.moving_right = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
ship.moving_right = False
You have an elif without an if preceding it. Just change the first elif to an if.
You cannot use elif before an if statement. Elif cannot be used without an if statement.
This is the right syntax.
import sys
import pygame
def check_events(ship):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
# Move the ship to the right.
ship.moving_right = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
ship.moving_right = False
def update_screen(ai_settings, screen, ship):
'''Update images on the screen and flip to the new screen.'''

Image not responding to different keys Pygame Python

Good day. I am trying to make a ground move left and right when the opposite keys are pressed, but for some reason, the image just keeps moving to right. Here's the reasonable part of my code:
while not crashed and not timeOut and not Quit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
Quit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
gx_change = 2.5
elif pygame.key == pygame.K_RIGHT:
gx_change = -2.5
if pygame.key == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
gx_change = 0
print (event)
gx += gx_change
As I said, it just keeps going right.
UPDATE: It is fixed
Thank you!!!
The problem is that your if statement for KEYUP is actually inside the if statement for KEYDOWN. This is the correct code:
while not crashed and not timeOut and not Quit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
Quit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
gx_change = 2.5
elif pygame.key == pygame.K_RIGHT:
gx_change = -2.5
if pygame.key == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
gx_change = 0
print (event)
gx += gx_change

Python keyboard controls

Here is my code I have trouble with, it's a basic side scroller game but I am having troubles with my keyboard control definitions, they don't seem to work, and I can't find the problem.
if keys[K_LEFT]:
newmove= LEFT
moveLeft(guy,10)
climb(guy)
if keys[K_RIGHT]:
newmove=RIGHT
moveRight(guy,10)
climb(guy)
if keys[K_SPACE] and guy[ONGROUND]:
guy[VY] = -14
else:
frame=0
if move==newmove:
frame=frame+0.1
if frame>=len(pics[move]):
frame=1
elif newmove!=-1:
move=newmove
frame=1
Have you tried using this? or something like this?
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
xChange = -5
print("Left")
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
xChange = 5
print("Right")
if event.type == pygame.KEYUP:
if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
xChange = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
xChange = 0

Categories