Pygame/Python game not receiving inputs - python

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()
...

Related

Pygame : event.type works only for one case

I have a problem with my pygame code, which seems correct but doesn't work as it should.
I want to do different actions depending the key pressed; when I put two cases, only one works.
I put my code below :
gamestate.draw(window)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.type == pygame.K_ESCAPE):
finis = True
if (event.type == pygame.KEYDOWN and event.key == pygame.K_UP):
print("ok")
pygame.display.update()
Here, my code print "ok" in the terminal when I press the UP key, but doesn't quit when I keep the escape key pressed.
It isn't this particular case that doesn't work, before it quit when i pressed escape and didn't print "ok" when i pressed the up key.
Do you have any idea to solve my issue ? Thanks a lot !
pygame.K_ESCAPE is not an event type but you check for event.type:
... and event.type == pygame.K_ESCAPE
It should be:
... and event.key == pygame.K_ESCAPE
Also usually if you have to check for multiple keys on pygame.KEYDOWN event, then use nested ifs:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
finis = True
if event.key == pygame.K_UP:
print('ok')
Also you need to call pygame.display.update only ONCE in the loop (most likely at the end) NOT twice

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

Pygame not responding to keys

I've recently started Python and I'm learning from a book but I think the book either uses an old version or it's not made very well. Using some code I tried to create a simple moving character.
#!/usr/bin/python3
import pygame
from pygame.locals import *
pygame.init()
running = True
gamewindow=pygame.display.set_mode((800, 600))
pygame.display.set_caption("Game")
black=(0, 0, 0)
white=(255, 255, 255)
img=pygame.image.load("/home/leo/Downloads/pixel.png")
def sprite(x,y):
gamewindow.blit(img, (x,y))
x=(800*0.2)
y=(600*0.735)
xchange=0
imgspeed=0
while running:
for event in pygame.event.get():
if event.type==QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LEFT:
xchange=-5
elif event.type == pygame.K_RIGHT:
xchange=5
if event.type==pygame.KEYUP:
if event.type==pygame.K_LEFT or event.key==pygame.K_RIGHT:
xchange=0
x= x + xchange
gamewindow.fill(white)
sprite(x,y)
pygame.display.update()
pygame.quit()
Using this code, the character doesn't move and I think it may be due to the wrong functions in this section:
while running:
for event in pygame.event.get():
if event.type==QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LEFT:
xchange=-5
elif event.type == pygame.K_RIGHT:
xchange=5
if event.type==pygame.KEYUP:
if event.type==pygame.K_LEFT or event.key==pygame.K_RIGHT:
xchange=0
x= x + xchange
Is the book completely wrong or is there just a few small things I have to change. Thank you!
You check type of event instead of the event key. For example you should use event.key == pygame.K_LEFT instead of event.type == pygame.K_LEFT etc.
How do you think event.type can be equal pygame.K_LEFT and pygame.KEYDOWN in the same time?
See How to use pygame.KEYDOWN for the correct example.

Pygame keyboard input issue

I am trying to get user keyboard input using pygame. However, the problem is that after I run my code on IDLE, the keyboard input is never read by the program, and whatever I type is shown on the shell. Same issue if I run my code on PyCharm. Any idea? Below is my code:
pygame.init()
screen = pygame.display.set_mode((800, 600))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == KEYDOWN and event.key == pygame.K_w:
print("Yup!")
pygame.display.flip()
I'm having the exact same issue, also on a Mac using Pycharm and python 3.6. I'm printing the events and only MouseMotion events are recorded, rather than KeyDown.
Edit: apparently it's a known issue: Window does not get focus on OS X with Python 3
This is my code, and it also should work:
while not crashed:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
crashed = True
# get current list
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP]:
print("UP")
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
print('this DOES work! :)')
elif event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
elif pressed[pygame.K_UP]:
print("UP")
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
pygame.display.flip()
x += x_change
gameDisplay.fill(black)
ship(x, y)
pygame.display.update()
clock.tick(60)
This code works perfectly for me
import pygame
pygame.init()
windowSize = width,height = 800,600
screen = pygame.display.set_mode(windowSize)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
print("Yup!")
pygame.display.flip()

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