why am i not getting print message? - python

while running:
screen.fill((255,255,255))
screen.blit(background,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# keystroke controlling i am not getting this print message
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_SPACE:
print("space is pressed ")
if event.type == pygame.KEYUP:
if event.type == pygame.K_SPACE:
print("released")
playerx += playerx_change
player(playerx , playery)
cactus(cactusx , cactusy)
pygame.display.update()
game()

This is because event.type cannot be KEYUP AND K_SPACE at the same time .
Rewrite the code as :
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
print("space is pressed ")
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
print("released")

Related

pygame on mac does not close nor register keys

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

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.

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

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

Pygame holding down key

I am trying to make a fun program with a guy who opens his mouth whenever a press space. The problem is he only opens it for .1 seconds and then it closes again. I want to make it so that the mouth is open whenever i hold space.
Code:
import pygame
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Open The Mouth")
clock = pygame.time.Clock()
faceImg = pygame.image.load("face_shut.png")
faceOpenImg = pygame.image.load("face_open.png")
def face(x,y):
gameDisplay.blit(faceImg,(face_x,face_y))
def faceOpen(x,y):
gameDisplay.blit(faceOpenImg,(faceOpen_x,faceOpen_y))
faceOpen_x = (1)
faceOpen_y = (1)
face_x = (1)
face_y = (1)
programRunning = True
while programRunning:
for event in pygame.event.get():
if event.type == pygame.QUIT:
programRunning = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
faceOpen(faceOpen_x,faceOpen_y)
pygame.display.update()
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
face(face_x,face_y)
pygame.display.update()
face(face_x,face_y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
The issue appears to be here:
while programRunning:
for event in pygame.event.get():
if event.type == pygame.QUIT:
programRunning = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
faceOpen(faceOpen_x,faceOpen_y)
pygame.display.update()
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
face(face_x,face_y)
pygame.display.update()
# Here, always drawn closed at the end of the while
face(face_x,face_y)
pygame.display.update()
clock.tick(60)
Regardless of what's being set in the while, you are redrawing the face as closed at the end.
How about this instead:
# Drawn before the loop starts
face(face_x,face_y)
pygame.display.update()
while programRunning:
for event in pygame.event.get():
event_occurred = True
if event.type == pygame.QUIT:
programRunning = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
faceOpen(faceOpen_x,faceOpen_y)
pygame.display.update()
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
face(face_x,face_y)
pygame.display.update()
clock.tick(60)

Categories