Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

When I run this code in Python2 it works (to read the keyboard), but in Python3 it doesn't. In Python3, I notice:

  • The game window opens, but it doesn't come to the front. The terminal window stays up front.
  • Even if I click into the game window to bring it to the foreground, keystrokes appear in the terminal window.
  • Clicking on the close window widget works in both Python2 and Python3.

Maybe I've done something wrong when I installed Pygame for Python3? I'm using virtualenv, and the lssitepackages command gives me this:

Robin:tests(0) mikec$ lssitepackages
__pycache__         pkg_resources
_markerlib          pygame
easy_install.py         pygame-1.9.2a0-py3.4.egg-info
pip             setuptools
pip-7.0.0.dist-info     setuptools-15.0.dist-info

I'm not sure how to move forward to troubleshoot this. Here's the Python code, taken from this tutorial: http://pythonprogramming.net/pygame-tutorial-moving-images-key-input/?completed=/displaying-images-pygame/

import pygame

pygame.init()

display_width = 800
display_height = 600


gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('A bit Racey')

black = (0, 0, 0)
white = (255, 255, 255)

clock = pygame.time.Clock()
crashed = False
carImg = pygame.image.load('racecar.png')


def car(x, y):
    gameDisplay.blit(carImg, (x, y))


x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
car_speed = 0

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)
    car(x,y)

    pygame.display.update()
    clock.tick(60)

pygame.quit()
quit()
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.