Pygame holding down key - python

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)

Related

Pygame movement causes the object to duplicate [duplicate]

This question already has answers here:
How can I move the ball instead of leaving a trail all over the screen in pygame?
(1 answer)
How can I make a sprite move when key is held down
(6 answers)
Closed 4 months ago.
So i programmed a block which should move in pygame
But when i press any of the movement buttons it doesnt move the block it just duplicates it and moves it and the old square doesnt do anything
Yeah im probably really retarded and the answer is really obviousä
here is the script:
import pygame
from sys import exit
pygame.init()
width=800
height=800
screen = pygame.display.set_mode((width,height))
posy=500
posx=500
clock = pygame.time.Clock()
pygame.display.set_caption("Block")
while True:
for event in pygame.event.get():
pygame.draw.rect(screen, "red", pygame.Rect(posy, posx, 60, 60))
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
posx=posx-10
elif event.key == pygame.K_s:
posx=posx+10
elif event.key == pygame.K_a:
posy=posy-10
elif event.key == pygame.K_d:
posy=posy+10
if event.key == pygame.K_ESCAPE:
pygame.event.post(pygame.event.Event(exit()))
pygame.display.update()
clock.tick(60)
It looks like you're creating a new object every time, so you need to adjust your code like this:
rect = pygame.Rect(posy, posx, 60, 60)
for event in pygame.event.get():
pygame.draw.rect(screen, "red", rect)
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
rect.move_ip(0, -10)
elif event.key == pygame.K_s:
rect.move_ip(0, 10)
elif event.key == pygame.K_a:
rect.move_ip(-10, 0)
elif event.key == pygame.K_d:
rect.move_ip(10, 0)

why am i not getting print message?

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

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.

why is my image getting covered?

I started watching a youtube video series on learning pygame by sentdex.
I have been following all his steps and i got to the point where i had a car(the image) and it was on a white screen. But, when i put in the functions to be able to move the car. Next time i ran it I could not see my car. I noticed that I could see it for a split second when i closed the screen.I have tried searching this question every way that i could think of. It is probably a small stupid mistake on my part. Thank you in advance
import pygame
pygame.init()
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
carImg = pygame.image.load('racecar.png')
def car(x, y):
gameDisplay.blit(carImg,(x,y))
x = (display_width * 0.45)
y = (display_height * 0.6)
x_change = 0
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
if event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(white)
car(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
Your indentation is messed up. Everything from for event in pygame.event.get(): to clock.tick(60) should be indented to show that it fits inside the while not crashed: loop. Also, crashed = True should be indented to show that it is inside the if event.type == pygame.QUIT: statement. In addition, the pygame.KEYDOWN and pygame.KEYUP comparisons should be inside your for event in pygame.event.get(): loop. Finally, you can improve speed by changing some of the ifs to elifs. This is the corrected version of the last section of your code:
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(white)
car(x, y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()

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

Categories