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
Related
I am having a problem with pygame on mac. It wont close when clicking on the 'x' button. It also would not move the space ship at all with the arrow keys. Any suggestions?
while running:
screen.fill((255,0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LEFT:
playerX_change = -0.1
if event.type == pygame.K_RIGHT:
playerX_change = 0.1
if event.type == pygame.KEYUP:
if event.type == pygame.K_LEFT or event.type == pygame.K_RIGHT:
playerX_change=0
playerX+=playerX_change
player(playerX, playerY)
pygame.display.update()
Ok, your code is not complete for me, but i have an idea that was occurr.
while running:
screen.fill((255,0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LEFT:
playerX_change = -0.1
playerX+=playerX_change
if event.type == pygame.K_RIGHT:
playerX_change = 0.1
playerX+=playerX_change
if event.type == pygame.KEYUP:
if event.type == pygame.K_LEFT or event.type == pygame.K_RIGHT:
playerX_change=0
player(playerX, playerY)
pygame.display.update()
Maybe if you transfer to position that change the respective position directly works fine
playerX+=playerX_change
I think the last three lines of code should be within the while loop. Without doing so, your display does not update with each iteration.
It is a matter of Indentation. The events need to be evaluated in the event loop:
while running:
screen.fill((255,0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# INDENTATION
#-->|
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LEFT:
playerX_change = -0.1
if event.type == pygame.K_RIGHT:
playerX_change = 0.1
if event.type == pygame.KEYUP:
if event.type == pygame.K_LEFT or event.type == pygame.K_RIGHT:
playerX_change=0
playerX+=playerX_change
player(playerX, playerY)
pygame.display.update()
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.
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.
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()
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()
...