Created
November 20, 2011 14:01
-
-
Save yoavram/1380291 to your computer and use it in GitHub Desktop.
2 players control 2 cars and try to avoid colissions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://richard.cgpublisher.com/product/pub.84/prod.11 | |
# INTIALISATION | |
import pygame, math, sys, random | |
from pygame.locals import * | |
TURN_SPEED = 5 | |
ACCELERATION = 2 | |
MAX_FORWARD_SPEED = 10 | |
MAX_REVERSE_SPEED = 5 | |
BG= (0,0,0) | |
MAX_Y = 768 | |
MAX_X = 1024 | |
COLLISION_DISTANCE = (MAX_Y*10) | |
def check_speed(speed): | |
if speed > MAX_FORWARD_SPEED: | |
speed = MAX_FORWARD_SPEED | |
if speed < MAX_REVERSE_SPEED: | |
speed = MAX_REVERSE_SPEED | |
return speed | |
def check_direction(direction): | |
return direction % 360 | |
def is_collision(pos1, pos2): | |
distance = (pos1[0]-pos2[0])**2+(pos1[1]-pos2[1])**2 | |
print distance | |
return distance<COLLISION_DISTANCE | |
def main(): | |
# initialize the screen with size (MAX_X, MAX_Y) | |
screen = pygame.display.set_mode((MAX_X, MAX_Y)) | |
num_cars = 2 | |
# load picture file - Car: http://www.xnadevelopment.com/tutorials/introducingxnagamestudioexpress/Car.png | |
car = [] | |
while len(car)<num_cars: | |
car.append(pygame.image.load('car.png')) | |
car.append(pygame.image.load('car2.png')) | |
# initialize the sound mixer | |
pygame.mixer.init() | |
# load horn sound http://www.freesound.org/people/KRAFTWERK2K1/sounds/32417/ | |
horn = pygame.mixer.Sound('car-horn.wav') | |
clock = pygame.time.Clock() # load clock | |
k_up = k_down = k_left = k_right = k_c = k_z = k_s = k_x = 0 # init key values | |
# start speed, direction & position | |
speed = [0]*num_cars | |
direction = [0]*num_cars | |
position = [(100, 100)]*num_cars | |
play = True | |
while play: | |
# USER INPUT | |
clock.tick(30) | |
# get events from the user | |
for event in pygame.event.get(): | |
# not a key event | |
if not hasattr(event, 'key'): | |
continue | |
# check if presses a key or left it | |
down = event.type == KEYDOWN # key down or up? | |
# key events: http://pygame.org/docs/ref/key.html | |
if event.key == K_RIGHT: | |
k_right = down * TURN_SPEED | |
elif event.key == K_LEFT: | |
k_left = down * TURN_SPEED | |
elif event.key == K_UP: | |
k_up = down * ACCELERATION | |
elif event.key == K_DOWN: | |
k_down = down * ACCELERATION | |
if event.key == K_c: | |
k_c = down * TURN_SPEED | |
elif event.key == K_z: | |
k_z = down * TURN_SPEED | |
elif event.key == K_s: | |
k_s = down * ACCELERATION | |
elif event.key == K_x: | |
k_x = down * ACCELERATION | |
elif event.key == K_RETURN: | |
horn.play() | |
elif event.key == K_ESCAPE: | |
play = False | |
speed[0] += (k_up - k_down) | |
speed[0] = check_speed(speed[0]) | |
direction[0] += (k_left - k_right) | |
direction[0] = check_direction(direction[0]) | |
speed[1] += (k_s - k_x) | |
speed[1] = check_speed(speed[1]) | |
direction[1] += (k_z - k_c) | |
direction[1] = check_direction(direction[1]) | |
screen.fill(BG) | |
for j in range(num_cars): | |
x, y = position[j] | |
rad = direction[j] * math.pi / 180 | |
x += speed[j]*math.sin(rad) | |
y += speed[j]*math.cos(rad) | |
# make sure the car doesn't exit the screen | |
if y < 0: | |
y = MAX_Y | |
elif y > MAX_Y: | |
y = 0 | |
if x < 0: | |
x = MAX_X | |
elif x > MAX_X: | |
x = 0 | |
position[j] = (x, y) | |
# RENDERING | |
# .. rotate the car image for direction | |
rotated = pygame.transform.rotate(car[j], direction[j]) | |
# .. position the car on screen | |
rect = rotated.get_rect() | |
rect.center = position[j] | |
# .. render the car to screen | |
screen.blit(rotated, rect) | |
if is_collision(position[0],position[1]): | |
horn.play() | |
pygame.display.flip() | |
sys.exit(0) # quit the game | |
if __name__=='__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment