I have been learning to use PyGame from pythonprogramming.net. I am currently on moving images on a window:
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.