0

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()
DukeSilver
  • 458
  • 1
  • 6
  • 22
  • 1
    I can't reproduce the behavior you described. When I run the code, everything appears to work as expected. A single red rectangle appears, and when I press the arrow keys, the rectangle moves correspondingly. – Christian Dean Jun 16 '18 at 04:28
  • Weird. Any idea why mine might be giving me trouble? Does pygame need the display to be activated or anything? – DukeSilver Jun 16 '18 at 04:30
  • Honestly, I don't really have any ideas right now @Duke. Although it's admittedly far-fetched, you could have a bug with your Pygame install. Perhaps re-check where you downlowed the Pygame library from and how you installed it. – Christian Dean Jun 16 '18 at 04:32
  • Have you tried clicking on the PyGame window after it appears? If the terminal is still the foreground app, it still gets keyboard focus, meaning it'll see the arrow keys. – abarnert Jun 16 '18 at 04:33
  • @abarnert yeah, that didn't work. I did just remember though that 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). @ChristianDean I might look into that tomorrow. I had some trouble when I first installed so maybe that's it. – DukeSilver Jun 16 '18 at 04:52
  • @DukeSilver You didn't somehow build PyGame on top of a custom SDL with only the X11 backend and not the Aqua one, did you? – abarnert Jun 16 '18 at 04:55
  • Not that this answers your question, but as a side note, it is possible to miss input using `pygame.key.get_pressed` if your game loop takes a long time and lots of pressing and unpressing of a given key occured during the intervening time. If you want to capture all of those presses, you should iterate through each event with `pygame.event.get()`. – CodeSurgeon Jun 16 '18 at 05:08
  • 1
    Also, maybe [this answer](http://www.stackoverflow.com/a/47838832) is relevant, since you are on OSX. – CodeSurgeon Jun 16 '18 at 05:11
  • Ah! The `pythonw` answer did the trick! Thanks @CodeSurgeon. Separate note, I just realized that I'm running Python 3 via Anaconda. Might that have been causing problems? – DukeSilver Jun 17 '18 at 03:44
  • To be honest, I am not sure, since I do not have a Mac nor use the version of python provided by Anaconda. I can’t imagine that that should make a difference. I assume that the terminal is hijacking input; however, I personally have not run into that on Windows when running pygame scripts from the command prompt. Definitely is a strange issue though, and probably will be limiting to not have print statements for debugging purposes when using pythonw rather than just python. – CodeSurgeon Jun 17 '18 at 03:54
  • Although using a little more google-fu, I did find [an issue](http://github.com/pygame/pygame/issues/359) on github that might help explain things better. Maybe it is a distribution thing after all with anaconda... – CodeSurgeon Jun 17 '18 at 03:59
  • Yup. I 100% didn't get that far down on that Getting Started page. Thanks for the help! – DukeSilver Jun 17 '18 at 05:30

0 Answers0