How to use keyboard in pygame? - keyboard

I tried the simple script below, but I get no response. Maybe I don't understand what pygame is capable of doing. I simply want to assign a certain function to a certain key. Like if I press the letter 'h' I want "Bye" to print on the screen or if I press 'g' then play a certain song. And so on and so on...
import pygame
from pygame.locals import *
pygame.init()
print K_h #It prints 104
while True:
for event in pygame.event.get():
if event.type==KEYDOWN:
if event.key==K_h:
print "Bye" #Nothing is printed when I press h key

import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640,480))
clock = pygame.time.Clock()
while True:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == KEYDOWN:
if event.key == K_h:
print "bye"
for what ever reason for me atleast it seems the event command wouldn't work with out the use of a screen even if it has no purpose this will get you the results you want.

Related

Pygame gamepad won't run anymore with an Xbox 360 controller and an Ubuntu machine [duplicate]

This code goes into infinite loop. I cant use A button on xbox 360 controller
import pygame
from pygame import joystick
pygame.init()
joystick = pygame.joystick.Joystick(0)
pygame.joystick.init()
print("start")
while True:
if joystick.get_button(0) == 1 :
print("stoped")
break
I cant use A button on xbox 360 controller
Personnaly, I can, so this seems to be possible. You are just missing that pretty much every user input needs to be updated by pygame through pygame.event.get().
From the pygame documentation:
Once the device is initialized the pygame event queue will start receiving events about its input.
So, apparently you need to get the events in the while loop like such to make the joystick work:
import pygame
from pygame.locals import *
pygame.init()
joystick = pygame.joystick.Joystick(0)
while True:
for event in pygame.event.get(): # get the events (update the joystick)
if event.type == QUIT: # allow to click on the X button to close the window
pygame.quit()
exit()
if joystick.get_button(0):
print("stopped")
break
Also,
In the line if joystick.get_button(0) == 1, you don't need to type == 1 because the statement is already True.
You are initializing pygame.joystick twice: through the line pygame.init() and pygame.joystick.init().
You don't need to type from pygame import joystick because you already already have it in the line import pygame.
You can take this as reference and use it in your own way.
import pygame
import sys
pygame.init()
pygame.joystick.init()
clock = pygame.time.Clock()
WIDTH,HEIGHT = 500,500
WHITE = (255,255,255)
BLUE = (0,0,255)
BLUISH = (75,75,255)
YELLOW =(255,255,0)
screen = pygame.display.set_mode((WIDTH,HEIGHT))
smile = pygame.image.load("smile.jpg")
smile = pygame.transform.scale(smile,(WIDTH,HEIGHT))
idle = pygame.image.load("idle.jpg")
idle = pygame.transform.scale(idle,(WIDTH,HEIGHT))
joysticks = [pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count())]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.JOYBUTTONDOWN:
if event.button == 0: #press A button to smile
screen.fill(WHITE)
screen.blit(smile,(0,0))
pygame.display.update()
clock.tick(10)
elif event.type == pygame.JOYBUTTONUP:
if event.button == 0:
screen.fill(WHITE)
screen.blit(idle,(0,0))
pygame.display.update()
clock.tick(10)

Can't import any pygame functions (E1101)

When I run the following code, a white screen appears and then quickly disappears. I installed pygame on the VSCode terminal with pip3 (I am on mac) and now that I finally got it to import pygame, I get multiple errors for every function called such as "clock" or "pygame.KEYDOWN". They say "Module 'pygame' has no 'KEYDOWN' member" [E1101]. I also get an error with the "init" member.
I've seen other posts that tell me to copy and paste something into the json settings, but when I tried that I got even more errors.
#Game
#By Robert Smith
#Initialize python
import pygame
pygame.init()
#Set the screen
screen = pygame.display.set_mode((640, 480))
background = pygame.Surface(screen.get_size())
background.fill((255,255,255))
background = background.convert()
screen.blit(background, (0 , 0))
#Make the window exitable
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainloop == False #close the window
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
mainloop = False
#Set the framerate
milliseconds = clock.tick(60)#DO NOT GO FASTER THAN THIS FRAMERATE
Try this:
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainloop == False #close the window
elif event.type == keys[pygame.K_DOWN]:
if event.key == keys[pygame.K_ESCAPE]:
mainloop = False
And this:
milliseconds = pygame.time.Clock.tick(60)

Quit function is not working in pygame

I want the system to exit when I click the "x" on the window of pygame. I want to be able to call the quit_game function whenever I need to. So if the user does quit, it actually quits. I have tried sys exit and pygame.quit(), but I haven't been able to successfully implement those. Right now I just have the quit function built into the intro screen. I am using python 3.4.3. Here is the code.
import math
import random
import time
import pygame
import sys
import glob
pygame.init()
move=0
FPS=60
blue=(0,0,255)
white=(255,255,255)
black=(0,0,0)
green=(0,155,0)
display_width=800
display_height=600
gamedisplay=pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Stacker')
clock=pygame.time.Clock()
speed=2
smallfont=pygame.font.SysFont("Arial",int(display_width/32))
mediumfont=pygame.font.SysFont("Arial",int(display_width/16))
largefont=pygame.font.SysFont("Arial",int(display_width/10))
gamedisplay.fill(green)
block=display_height/12
pygame.display.update()
def quit_game():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
quit()
def intro_screen():
welcome_message = mediumfont.render(str("Welcome to Stacker!!!"), True,black)
gamedisplay.blit(welcome_message,(display_width/4,display_height/40))
how_to_play_1=smallfont.render(str("Your goal is to get to the major prize bar"),True,black)
gamedisplay.blit(how_to_play_1,(display_width/3.619909502,display_height/2))
how_to_play=smallfont.render(str("Press the space bar to stop the shape"),True,black)
gamedisplay.blit(how_to_play,(display_width/3.48583878,display_height/(12/7)))
quit_game()
pygame.display.update()
def middle_block():
pygame.draw.rect(gamedisplay, blue,(display_width/(32/15),display_height-block,block,block))
pygame.display.update()
def left_block():
pygame.draw.rect(gamedisplay, blue,(display_width/(32/13),display_height-block,block,block))
pygame.display.update()
def right_block():
pygame.draw.rect(gamedisplay, blue,(display_width/(32/17),display_height-block,block,block))
pygame.display.update()
def major_screen():
major_message = mediumfont.render(str("Major Prize Here!"), True,black)
gamedisplay.blit(major_message,(display_width/(10/3),display_height/40))
pygame.display.update()
intro_screen()
pygame.time.delay(8000)
gamedisplay.fill(green)
major_screen()
pygame.draw.rect(gamedisplay, blue,(display_width-display_width,display_height/8,display_width,display_height/60))
pygame.draw.rect(gamedisplay, blue,(display_width-display_width,display_height/2.4,display_width,display_height/60))
middle_block()
left_block()
right_block()
pygame.display.update()
Your code has no main game-loop. You may thinks this:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
quit()
Is your game-loop. While that is almost correct, it is missing a key part. A while loop. Your telling Python to loop through every single event that Pygame is tracking. After it finishes looping through the events once, it stops. You want Python to continually loop through the Pygame events, until the window is closed. To do this create a variable to control your loop. Such as running = True or not_quit = True. Next wrap a while-loop around your for-loop using the control variable as your condition. Lastly, if the user closes the window, set your control variable to False.
running = True # our variable to control the loop.
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False # break the loop
pygame.quit()
sys.exit()
quit()
This should allow you to close your window. I should note however that none of the text/shapes your blitting/drawing to the screen will be visible unless inside your game-loop. So you may need to rethink your code a bit.

pygame detect which key is pressed

I'm working on a scoreboard in a game I am creating and I know how to check if a key is pressed, but is there a way to check WHICH key is pressed, make a varibale that key and then add it to a string?
EDIT: Ok, incase I was unclear: I know that I can check if buttons like h or t can be pressed, but I want a way to just instantly press a button and add that to a string, and I don't want to build an event function for every letter in the alphabet.
if event.type == pygame.KEYDOWN :
print(event.key)
You can transform the given number into ascii with :
pygame.key.name(event.key)
If you have registered a callback to detect when a key has been pressed, UNQUESTIONABLY the identity of the key will be included in the structure passed to the callback's service routine.
Whether that offers the "keyboard code" as well as the plain-Jane ASCII value of the key I cannot tell you.
If you want simple textinput (for example if the user should enter their name for the highscore), take a look at EzText library.
Generally speaking, you can check which key was pressed in two ways.
If you use the event queue, you can check the event.key against the pygame key codes:
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
print("spacebar was pressed")
if event.key == pygame.K_q:
done = True
pygame.quit()
All keycodes are listed in the documentation. This will fire one event for every keypress, regardless of how long a single key is being held down.
Alternatively, you can use state checking:
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
state = pygame.key.get_pressed()
if state[pygame.K_SPACE]:
print("spacebar was pressed")
if state[pygame.K_q]:
done = True
pygame.quit()
Notice that this will fire many events if the key is being held down for longer. It may also miss key presses if the key is being pressed and released at a time when your program is busy doing something other than checking for key presses.
A bit late, but I think this is it:
import pygame
pygame.init()
# set screen size
screen = pygame.display.set_mode((100, 100))
# set loop flag
done = False
while not done:
# check events
for event in pygame.event.get():
# end if X was pressed
if event.type == pygame.QUIT:
done = True
# check if event was a key being pressed down
if event.type == pygame.KEYDOWN:
# end if Esc was pressed
if event.key == pygame.K_ESCAPE:
done = True
# unicode returns the char
print(event.scancode, event.key, event.unicode)
# be IDLE friendly
pygame.quit()
What you need is this:
if event.type == pygame.KEYDOWN:
print(event.key, event.unicode)
event.key show the code of an character that are pressed
event.unicode show the character that are preseed
Result would be this:
event.key: 97, event.unicode: a
event.key: 98, event.unicode: b
event.key: 99, event.unicode: c
event.key: 100, event.unicode: d
event.key: 101, event.unicode: e
event.key: 102, event.unicode: f

Pygame waiting the user to keypress a key

I am searching a method, where the program stops and waiting for a spesific key to be pressed by the user. May I can implement this one with a while loop? I need the best algorithm, if there exist a build-in function of waiting, to avoid the loop.
I found several information on the official website of pygame, but nothing help.
Here is a testing algorithms but won't work:
key = "f"
while key != "K_f":
key = pygame.key.get_pressed()
if key[Keys.K_f]:
do something...
If you are waiting for a key to be pressed you can use the event.wait() function. This is useful, because it does not require a-lot of processing.
import pygame
from pygame.locals import *
pygame.event.clear()
while True:
event = pygame.event.wait()
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key = K_f:
do something...
Note that event.wait() waits for events to appear in the event cache, the event cache should be cleared first.
pygame.event documentation
You could do it with a while loop and an event queue:
from pygame.locals import *
def wait():
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN and event.key == K_f:
return

Resources