#! /usr/bin/python3 # ================================================================== # Based on: pythonprogramming.net/pygame-python-3-part-1-intro/ # ================================================================== # Use the arrow keys to move the car up, down, left, and right. # Use the space bar to change the car image. # ================================================================== # Things to Do: # a. See the web site code and this code to create game where you # must avoid hitting an astroid. # b. Add multiple astroids comming from all directions. # (left, right, top, bottom, at an angle, etc.) # c. Add more "car" images. (rockets, tanks, cars, ships, etc.) # d. Randomly pick "car" image at start up? # e. Store "car" objects (images) in a list? Select/use "car" # objects from the list? Allow the uswe to change "cars". # f. Read runtime "car" image file names, speed, etc. from a # configuration file? # g. Add keys to speed up or show down movement. # ================================================================== # ----- initialize pygame import pygame pygame.init() # ----- pygame display display_width = 800 display_height = 600 gameDisplay = pygame.display.set_mode((display_width,display_height)) pygame.display.set_caption('A Bit Racey') # ----- set car location function def moveCar(img,x,y): gameDisplay.blit(img,(x,y)) # ----- define some colors black = (0,0,0) white = (255,255,255) red = (255,0,0) green = (0,255,0) blue = (0,0,155) yellow = (255,255,0) cyan = (0,255,255) magenta = (255,0,255) # ----- car image, etc. carImg1 = pygame.image.load('racecar_green.png') carImg2 = pygame.image.load('racecar_red.png') car = carImg1 car_width = car.get_width() car_height = car.get_height() # ----- initial car position and movement values x = int(display_width * 0.45) y = int(display_height * 0.8) x_change = 0 y_change = 0 # ----- runtime "stuff" print('-----Starting Runtime---------------') print('Display width = ',display_width) print('Display height = ',display_height) print('Car width = ',car_width) print('Car height = ',car_height) print('Car start X = ',x) print('Car Start Y = ',y) print('Car change X = ',x_change) print('Car change Y = ',y_change) if car == carImg1: print('Car = carImg1') else: print('Car = carImg2') print('------------------------------------') # ----- event loop clock = pygame.time.Clock() done = False while not done: # ----- process events for event in pygame.event.get(): if event.type == pygame.QUIT: done = 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.key == pygame.K_DOWN: y_change = 5 elif event.key == pygame.K_UP: y_change = -5 if event.key == pygame.K_SPACE: if car == carImg1: car = carImg2 else: car = carImg1 car_width = car.get_width() car_height = car.get_height() if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or pygame.K_RIGHT or \ pygame.K_DOWN or pygame.K_UP: x_change = 0 y_change = 0 # ----- car's location, do not go outside the display boundry x += x_change y += y_change if x < 0: x = 0 elif x > (display_width - car_width): x = display_width - car_width if y < 0: y = 0 elif y > (display_height - car_height): y = display_height - car_height # ----- referesh display gameDisplay.fill(white) moveCar(car,x,y) pygame.display.update() clock.tick(60) # ----- end progrm pygame.quit() quit()