AttributeError: 'Python' object has no attribute 'control' - python

I'm a beginner at Python and this project is my first project.
I made a code that moves snakes, and when I play the game and press the direction key, the game stops and this error occurs.
This Error is last error in my projcet.
I don't understand this error.
What should I do?
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_UP:
python.control(UP)
elif event.key == K_DOWN:
python.control(DOWN)
elif event.key == K_LEFT:
python.control(LEFT)
elif event.key == K_RIGHT:
python.control(RIGHT)

Please check if you have defined a function named 'control' or see if you are referring the right object name.

Related

PYGAME, check for second press

So I want to change the variable from False to True on the first key press, and then on the second press (of the same key for e.g. "g") I want the variable to change back to "False"
Here is an example of not working code:
...
if event.type == pygame.KEYUP:
if event.key == pygame.K_g:
show_location=True
if event.type == pygame.KEYUP:
if event.key == pygame.K_g:
show_location=False
...
Can someone explain to me how to do it right?
Rather than hard-coding True or False in your assignment, just negate the current value:
if event.type == pygame.KEYUP:
if event.key == pygame.K_g:
show_location = not show_location

Pygame AttributeError: 'Event' object has no attribute 'key'

I cant understand what is problem
while done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = False
elif event.type == pygame.KEYDOWN:
if event.key== pygame.K_RIGHT:
figure.moveRight(screen)
elif event.key== pygame.K_LEFT:
figure.moveLeft(screen)
else:
figure.moveDown(screen)
pygame.display.flip()
pygame.quit()
i still dont know what in problem was but i replaced pygame.KEYDOWN to pygame.KEYUP and the program started working well

How to move an object when a key is held down?

I know this question can be seen as a duplicate, but I spent some hours searching and figuring out what is wrong at my code.
My problem is that my object, called player, doesn't move constantly when left or right key is being held down:
for event in pygame.event.get():
if event.type == QUIT:
self.terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
self.terminate()
if event.key == K_LEFT or event.key == K_a:
self.moveRight = False
self.moveLeft = True
if event.key == K_RIGHT or event.key == K_d:
self.moveLeft = False
self.moveRight = True
if event.type == KEYUP:
if event.key == K_LEFT or event.key == K_a:
self.moveLeft = False
if event.key == K_RIGHT or event.key == K_d:
self.moveRight = False
# Move the player around
if self.moveLeft :
# Moves the player object to left with self.PLAYERMOVERATE pixels.
self.player.setLeftRight(-1 * self.PLAYERMOVERATE)
if self.moveRight :
self.player.setLeftRight(self.PLAYERMOVERATE)
I also tried this alternative:
while True:
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.player.setLeftRight(-1 * self.PLAYERMOVERATE)
if keys[pygame.K_RIGHT]:
self.player.setLeftRight(self.PLAYERMOVERATE)
I think that the problem is that you are not handling the input in the main game loop.
In your code you seem to be handling the input inside a method of the object Player. This is not how input should be handled. In your second code example there is a while True: loop which will mean the loop is never exited from and thus the method's execution is never finished. I suspect that there may be a similar issue in your first example.
Instead you should:
Create all objects and classes.
Write the main game loop.
The main game loop should handle input, then process the logic of the game and then render whatever should be rendered.
Here is a short code example.
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() # Exit from pygame window
quit() # End python thread
if event.type == KEYDOWN:
if event.key == K_LEFT or event.key == K_a:
player.moveRight = False
player.moveLeft = True
if event.key == K_RIGHT or event.key == K_d:
player.moveLeft = False
player.moveRight = True
if event.type == KEYUP:
if event.key == K_LEFT or event.key == K_a:
player.moveLeft = False
if event.key == K_RIGHT or event.key == K_d:
player.moveRight = False
# Move player using method
if player.moveLeft:
# Move player
# ...
# Render player
I hope that this helped you and if you have any further questions please feel free to post them in the comment section below!

Pygame/Python game not receiving inputs

I'm trying to make a game on python, my problem is that I can't get the game to read inputs while running. (Not even the Escape key to exit).
I can't find the problem, I thought it was the lack of the tick method but I just added it and it still doesn't work.
Any help is appreciated.
Thanks.
Here's the code:
Edit Now just the game loop
astronut=Player()
astronut.paintplayer()
while True:
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.type == K_ESCAPE:
terminate()
if event.type == K_UP:
astronut.acceleration +=1
astronut.speed = astronut.speed + astronut.acceleration
astronut.posY= astronut.posY + astronut.speed
elif astronut.posY<(WINDOWHEIGHT-OVERTHELINE-playerHeight):
astronut.acceleration -=1
astronut.speed = astronut.speed + astronut.acceleration
astronut.posY= astronut.posY + astronut.speed
astronut.paintplayer()
pygame.display.update()
mainClock.tick(FPS)
I think you need to check the event.key instead of event.type:
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
terminate()
...

Sprite lagging in pygame

Here is some pygame code it works fine except to move the sprite i have to repeatedly tap the arrow keys, is there a way to make the sprite move by holding down the arrow keys? Below is my code:
while True: #main game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYUP:
if event.key == K_RIGHT:
LionCubX+= 10
elif event.key == K_LEFT:
LionCubX-= 10
elif event.key == K_UP:
LionCubY-= 10
elif event.key == K_DOWN:
LionCubY+= 10
DISPLAYSURF.fill(GREEN)
DISPLAYSURF.blit(LionCubImg,(LionCubX,LionCubY))
pygame.display.update()
The problem that I can see here is that you check for events of type KEYUP. This means that your Code only gets executed when you release the key. So change
elif event.type == KEYUP:
to
elif event.type == KEYDOWN:
Another possibility would be that you didn't set key repeat:
pygame.key.set_repeat(1, 30)
Put this before your main game loop to activate key repeat. Also see the docs.
The problem is you're moving your sprite only if a key has been depressed and released KEYUP. You need to move the sprite in the key state KEYDOWN.
Change this:
elif event.type == KEYUP:
To:
elif event.type == KEYDOWN:

Categories