I am running this very simple tutorial on Pygame:
https://www.youtube.com/watch?v=xh4SV3kF-zk
I can't get keyboard events to be recognised by the game window. I have read a number of similar questions about this, but the answers don't seem right for Macs.
I am using miniconda and it might be that I need to quit the virtualenvironment? I don't understand how to do that tho. Or maybe I need to set the focus to my window - but I don't know how to do that either. This must be an issue lots of people are having on Macs running El Capitan. My Mac is too old to upgrade properly to Sierra. Is there a way to get the keyboard input to work?
import pygame
pygame.init()
display_width = 1200
display_height = 800
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('A Bit Racey')
clock = pygame.time.Clock()
speederImg = pygame.image.load('speeder.png')
def speeder(x, y):
gameDisplay.blit(speederImg, (x, y))
x = (display_width * 0.4)
y = (display_height * 0.2)
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
elif 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)
speeder(x, y)
# print(event)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
K_RIGHT
andK_LEFT
, notK-RIGHT
,K-LEFT
. You should see error message when you run it in console/terminal. See other key constants in official documentation. – furas Dec 15 '17 at 19:14K_LEFT
, notK-LEFT
- moment in video – furas Dec 15 '17 at 19:19