I'm trying to follow this tutorial: https://www.youtube.com/watch?v=UdsNBIzsmlI
I've copied the code (I think exactly) but when I run it in Terminal, any key events just go to the Terminal. Nothing happens in the pygame window and when I close it, all of the key events attempt to execute in the Terminal. Am I missing something?
FWIW, when I added print(keys)
the value that appeared in the list when I hit a key was not the value given for that key by pygame.K_LEFT
(or whatever key). It was the value the interpreter shows for that key ([(C
or whatever).
My stuff:
- Python 3.6.5
- Latest version of pyGame installed with Pip yesterday
- Mac 10.9
Here's the code I have:
import pygame
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("Learning pyGame")
x = 50
y = 425
width = 40
height = 60
vel = 5
isJump = False
jumpCount = 10
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
ev = pygame.event.poll()
if ev.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
if keys[pygame.K_RIGHT] and x < 500 - width - vel:
x += vel
if not(isJump):
if keys[pygame.K_UP] and y > vel:
y -= vel
if keys[pygame.K_DOWN] and y < 500 - height - vel:
y += vel
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -= 1
else:
isJump = False
jumpCount = 10
win.fill((0, 0, 0))
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
pygame.quit()