0

I have been learning to use PyGame from pythonprogramming.net. I am currently on moving images on a window:

https://pythonprogramming.net/pygame-tutorial-moving-images-key-input/?completed=/displaying-images-pygame/

Here's the code I am running, in case the site is down when you check it out. For some reason, I don't know why, the car image isn't moving. I had typed everything out when I was following the video and when I noticed it didn't work, I copied and pasted the still couldn't get the predicted performance. I printed out the event to debug (This is the only line that I added and isn't on the code copied from the site).

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)

    print(event) # added for debugging

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

pygame.quit()
quit()

I used Spyder but had the window that popped up freeze on me and I had to quit Spyder on my Mac.I switched to PyCharm and the terminal and found that there was no freezing in both but still no movement.

The printed out put on the terminal was:

libpng warning: iCCP: known incorrect sRGB profile
<Event(4-MouseMotion {'pos': (364, 569), 'rel': (364, 569), 'buttons': (0, 0, 0)})>
<Event(5-MouseButtonDown {'pos': (364, 569), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (364, 569), 'button': 1})>
<Event(4-MouseMotion {'pos': (491, 27), 'rel': (127, -542), 'buttons': (0, 0, 0)})>
^[[D^[[D^[[D^[[D^[[D^[[D^[[D^[[D<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
^[[D^[[D^[[D<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
^[[D^[[D^[[D^[[C^[[C<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(12-Quit {})>

I understand that the first line of the code is a warning from How do I disable the libpng warning? (python, pygame) and shouldn't affect the code in this manner.

Community
  • 1
  • 1
heretoinfinity
  • 1,528
  • 3
  • 14
  • 33
  • The code works correctly for me, too. Are you maybe trying to move the car with the mouse buttons (because you only show `MouseButtonDown/Up` events)? You should move it with the arrow keys. – skrx Mar 25 '17 at 21:20

1 Answers1

0

Sorry it has been a while I guess they have changed some things now that I am reading the documentation. It has changed a whole lot I thoroughly apoligize. One thing I would like to point out is it is a good practice to fill in the background before the game starts. I will look more into this I am going to test it myself.

Edit: I have just tested it and for me it works completely fine. It may be your keyboard layout but, my only thought would be, did you select the window?

user140052
  • 176
  • 10