diff --git a/Audi_1.png b/Audi_1.png new file mode 100644 index 0000000..16d69f5 Binary files /dev/null and b/Audi_1.png differ diff --git a/Project Reflection-MP4.pdf b/Project Reflection-MP4.pdf new file mode 100644 index 0000000..f91fd9d Binary files /dev/null and b/Project Reflection-MP4.pdf differ diff --git a/README.md b/README.md index 825cba1..0900f88 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,19 @@ -# InteractiveProgramming - -This is the base repo for the interactive programming project for Software Design at Olin College. +# Skyroads +Developing a game for Mini Project 4 for the SoftDes course + +Installed libraries: pygame +To install pygame: + +MacOS +Install Home Brew. Then: +$ brew install sdl sdl_image sdl_mixer sdl_ttf portmidi +$ pip install pygame + +Windows +$ pip install pygame + +To start the game, run the file game.py in the Command Prompt and click on 'Play now', in the opened window. + +To get out of the way of the obstacles and avoid collision, use the left and right arrow keys to move your car left or right. The game counter in the left upper corner gives you information on how good you perform. To exit the game either close the window or press the escape-key. + +Link to Project Write-up/Reflection: [Project Reflection](Project\ Reflection-MP4.pdf) diff --git a/apocalypse red sky.jpg b/apocalypse red sky.jpg new file mode 100644 index 0000000..7d281f9 Binary files /dev/null and b/apocalypse red sky.jpg differ diff --git a/ball.jpg b/ball.jpg new file mode 100644 index 0000000..4b52518 Binary files /dev/null and b/ball.jpg differ diff --git a/concrete.png b/concrete.png new file mode 100644 index 0000000..747eea9 Binary files /dev/null and b/concrete.png differ diff --git a/game.py b/game.py new file mode 100644 index 0000000..f1e54cf --- /dev/null +++ b/game.py @@ -0,0 +1,252 @@ +""" +Created on Thursday Oct 15, 2017 +SkyRoads: MiniProject 4 + +@authors: Felix Eberhardt & Shreya Rangarajan +""" +from threading import Timer +import time +import random +import os, sys, time +import pygame +from pygame.locals import * + +if not pygame.font: + print('Warning, fonts disabled') + +running = True +speed = 3 + +""" +Define all the inital variables +""" +white = (255, 255, 255) +black = (0, 0, 0) +green = (0, 200, 0) +red = (255, 0, 0) +green2 = (0, 255, 0) +blue = (0, 0, 255) +brown = (165, 42, 42) + +car = "audi_1.png" +barrier = "concrete.png" +road = "road.png" +barrier2 = "concrete.png" + +display_width = 1024 +display_height = 750 + +background_size = (display_width, display_height) +car_size = (100, 67) + +""" +Initialize the game +""" +pygame.init() +myfont = pygame.font.SysFont("monospace", 40) + +screen = pygame.display.set_mode(background_size) +clock = pygame.time.Clock() +timer = pygame.time.get_ticks() +barrier_limit = random.randint(1,3) + +background_image = pygame.image.load(road).convert() +player_image = pygame.image.load(car).convert() +player_image_rect = player_image.get_rect() +barriers_list = [] +barriers_list_rect = [] +barriers_list_pos = [] +concrete_img = pygame.image.load(barrier) +barriers_list.append(concrete_img) +barriers_list_pos.append([420,100]) +concrete_img_rect = concrete_img.get_rect() +barriers_list_rect.append(concrete_img_rect) + +""" +Initialize images +""" +background_colour = (white) +pygame.display.set_caption('Skyroads') +screen.fill(background_colour) +player_image.set_colorkey(white) +concrete_img.set_colorkey(white) +screen.blit(background_image, (0, 0)) +pygame.display.flip() + +def text_objects(text, font): + """display a text interface""" + textSurface = font.render(text, True, black) + return textSurface, textSurface.get_rect() + +def return_message(text): + """Create text output """ + largeText = pygame.font.Font('freesansbold.ttf',115) + TextSurf, TextRect = text_objects(text, largeText) + TextRect.center = ((display_width/2),(display_height/2)) + screen.blit(TextSurf, TextRect) + + pygame.display.update() +def barriers(): + """Randomized barriers falling down at different times """ + global timer + global barrier_limit + current_time = pygame.time.get_ticks() + timer_run = (current_time - timer)/1000 + + if timer_run > barrier_limit: + barrier_x = random.randint(325,575) + barrier_y = 0 + concrete = pygame.image.load(barrier) + concrete_rectangle = concrete.get_rect() + + global barriers_list + barriers_list.append(concrete) + global barriers_list_rect + barriers_list_rect.append(concrete_rectangle) + global barriers_list_pos + barriers_list_pos.append([barrier_x,barrier_y]) + timer = pygame.time.get_ticks() + barrier_limit = random.randint(1,4) + +def crash(): + """return message if crashed & remove barriers to restart game""" + return_message('Game Over') + + global barriers_list + barriers_list = [] + global barriers_list_rect + barriers_list_rect = [] + global barriers_list_pos + barriers_list_pos = [] + time.sleep(2) + +def score_count(score): + """count up the score with every loop """ + score += 1 + scoretext = myfont.render("Score {0}".format(score), 1, (red)) + screen.blit(scoretext, (5, 10)) + +def button(msg,x,y,w,h,ic,ac,action=None): + """Create a button""" + + mouse = pygame.mouse.get_pos() + click = pygame.mouse.get_pressed() + + if x+w > mouse[0] > x and y+h > mouse[1] > y: + pygame.draw.rect(screen, ac,(x,y,w,h)) + if click[0] == 1 and action != None: + action() + else: + pygame.draw.rect(screen, ic,(x,y,w,h)) + + smallText = pygame.font.SysFont("monospace",20) + textSurf, textRect = text_objects(msg, smallText) + textRect.center = ( (x+(w/2)), (y+(h/2)) ) + screen.blit(textSurf, textRect) + +def game_intro(): + """ Create a startmenu""" + intro = True + + while intro: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + quit() + + screen.fill(white) + largeText = pygame.font.SysFont("monospace",115) + TextSurf, TextRect = text_objects("Play Skyroads!", largeText) + TextRect.center = ((display_width/2),(display_height/2)) + screen.blit(TextSurf, TextRect) + + button("Play now!",(display_width/2-150),(display_width/2),300,100,green2,green2,game_loop) + + pygame.display.update() + clock.tick(15) + +def game_loop(): + """ + Run the game + """ + + running = True + speed = 6 + # Initialize game variables + score = 0 + x_speed_coord = 0 + y_speed_coord = 0 + concrete_motion = 4 + # Initialize position + x_car_coord = 420 + y_car_initial = 10 + dx = 10 + dy = 20 + + while running: + for event in pygame.event.get(): + # Check if player quits the game + if event.type == pygame.QUIT: + pygame.quit() + quit() + elif event.type == pygame.KEYDOWN: + # Figure out if it was an arrow key. If so adjust speed. + if event.key == pygame.K_LEFT: + x_speed_coord = -speed + elif event.key == pygame.K_RIGHT: + x_speed_coord = speed + elif event.key == pygame.K_ESCAPE: + running = False + # user leaves key + elif event.type == pygame.KEYUP: + # If it is an arrow key, reset vector back to zero + if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: + x_speed_coord = 0 + elif event.key == pygame.K_UP or event.key == pygame.K_DOWN: + y_speed_coord = 0 + + # Move the car and barriers according to the speed vector. + x_car_coord += x_speed_coord + if x_car_coord > 555: + x_car_coord = 555 + if x_car_coord < 320: + x_car_coord = 320 + y_car_coord = background_size[1]*0.98 - car_size[1] + + screen.blit(background_image, (0, 0)) + screen.blit(player_image, [x_car_coord, y_car_coord]) + player_image_rect.x = x_car_coord + player_image_rect.y = y_car_coord + + for i in range(0,len(barriers_list_rect)): + # Update barrier position in the position list + barriers_list_pos[i][1] = barriers_list_pos[i][1] + concrete_motion + screen.blit(barriers_list[i], [barriers_list_pos[i][0], barriers_list_pos[i][1]]) + barriers_list_rect[i].x = barriers_list_pos[i][0] + barriers_list_rect[i].y = barriers_list_pos[i][1] + + # add score + score += 1 + scoretext = myfont.render("Score {0}".format(score), 1, (red)) + screen.blit(scoretext, (5, 10)) + + pygame.display.flip() + clock.tick(60) + if x_car_coord <= 0 or x_car_coord >= background_size[0]- car_size[0]: + crash() + + for i in range(0,len(barriers_list_rect)): + hit = player_image_rect.colliderect(barriers_list_rect[i]) + if hit: + crash() + game_intro() + break + + barriers() + +""" +Run the game +""" +game_intro() +pygame.quit() +quit() diff --git a/players.py b/players.py new file mode 100644 index 0000000..9621aa1 --- /dev/null +++ b/players.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +""" +Created on Thursday Oct 19, 2017 +SkyRoads: MiniProject 4 +Players + +@author: Shreya Rangarajan +""" +import os +import pygame +from pygame.locals import * + + +pygame.init() +pygame.font.init() +class GameObject: + def __init__(self, image, height, speed): + self.speed = speed + self.image = image + self.pos = image.get_rect().move(0, height) + def move(self): + self.pos = self.pos.move(0, self.speed) + if self.pos.right > 600: + self.pos.left = 0 + +screen = pygame.display.set_mode((640, 480)) +player = pygame.image.load('ball.jpg').convert() +background = screen.fill([255,0,0])#pygame.image.load('background.bmp').convert() +screen.blit(background, (0, 0)) +objects = [] +for x in range(10): #create 10 objects + o = GameObject(player, x*40, x) + objects.append(o) +while 1: + for event in pygame.event.get(): + if event.type in (QUIT, KEYDOWN): + sys.exit() + for o in objects: + screen.blit(background, o.pos, o.pos) + for o in objects: + o.move() + screen.blit(o.image, o.pos) + pygame.display.update() + pygame.time.delay(100) diff --git a/road.png b/road.png new file mode 100644 index 0000000..02c38ec Binary files /dev/null and b/road.png differ diff --git a/road1.jpg b/road1.jpg new file mode 100644 index 0000000..2f6d2c5 Binary files /dev/null and b/road1.jpg differ diff --git a/road2.jpg b/road2.jpg new file mode 100644 index 0000000..c887955 Binary files /dev/null and b/road2.jpg differ diff --git a/road3.jpg b/road3.jpg new file mode 100644 index 0000000..3f07463 Binary files /dev/null and b/road3.jpg differ diff --git a/test.py b/test.py new file mode 100644 index 0000000..95ecd48 --- /dev/null +++ b/test.py @@ -0,0 +1,210 @@ +import pygame + +# Define some colors +BLACK = (0, 0, 0) +WHITE = (255, 255, 255) +GREEN = (0, 255, 0) +RED = (255, 0, 0) + + +def draw_stick_figure(screen, x, y): + # Head + pygame.draw.ellipse(screen, BLACK, [1 + x, y, 10, 10], 0) + + # Legs + pygame.draw.line(screen, BLACK, [5 + x, 17 + y], [10 + x, 27 + y], 2) + pygame.draw.line(screen, BLACK, [5 + x, 17 + y], [x, 27 + y], 2) + + # Body + pygame.draw.line(screen, RED, [5 + x, 17 + y], [5 + x, 7 + y], 2) + + # Arms + pygame.draw.line(screen, RED, [5 + x, 7 + y], [9 + x, 17 + y], 2) + pygame.draw.line(screen, RED, [5 + x, 7 + y], [1 + x, 17 + y], 2) + +# Setup +pygame.init() + +# Set the width and height of the screen [width,height] +size = [700, 500] +screen = pygame.display.set_mode(size) + +pygame.display.set_caption("My Game") + +# Loop until the user clicks the close button. +done = False + +# Used to manage how fast the screen updates +clock = pygame.time.Clock() + +# Hide the mouse cursor +pygame.mouse.set_visible(0) + +# Speed in pixels per frame +x_speed = 0 +y_speed = 0 + +# Current position +x_coord = 10 +y_coord = 10 + +# -------- Main Program Loop ----------- +while not done: + # --- Event Processing + for event in pygame.event.get(): + if event.type == pygame.QUIT: + done = True + # User pressed down on a key + + elif event.type == pygame.KEYDOWN: + # Figure out if it was an arrow key. If so + # adjust speed. + if event.key == pygame.K_LEFT: + x_speed = -3 + elif event.key == pygame.K_RIGHT: + x_speed = 3 + elif event.key == pygame.K_UP: + y_speed = -3 + elif event.key == pygame.K_DOWN: + y_speed = 3 + + # User let up on a key + elif event.type == pygame.KEYUP: + # If it is an arrow key, reset vector back to zero + if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: + x_speed = 0 + elif event.key == pygame.K_UP or event.key == pygame.K_DOWN: + y_speed = 0 + + # --- Game Logic + + # Move the object according to the speed vector. + x_coord = x_coord + x_speed + y_coord = y_coord + y_speed + + # --- Drawing Code + + # First, clear the screen to WHITE. Don't put other drawing commands + # above this, or they will be erased with this command. + screen.fill(WHITE) + + draw_stick_figure(screen, x_coord, y_coord) + + + # Go ahead and update the screen with what we've drawn. + pygame.display.flip() + + # Limit frames per second + clock.tick(60) + +# Close the window and quit. +pygame.quit() + + +# Create an 800x600 sized screen +# screen = pygame.display.set_mode([800, 600]) +# # This sets the name of the window +# pygame.display.set_caption('CMSC 150 is cool') +# # clock = pygame.time.Clock() + +# Set positions of graphics +# background_position = [0, 0] +# # Load and set up graphics. +# background_image = pygame.image.load("apocalypse red sky.jpg").convert() +# # player_image = pygame.image.load("playerShip1_orange.png").convert() +# player_image.set_colorkey(BLACK) +# done = False +# while not done: +# # for event in pygame.event.get(): +# # if event.type == pygame.QUIT: +# # done = True +# # elif event.type == pygame.MOUSEBUTTONDOWN: +# # click_sound.play() +# # Copy image to screen: +# screen.blit(background_image, background_position) +# # Get the current mouse position. This returns the position +# # as a list of two numbers. +# # player_position = pygame.mouse.get_pos() +# x = player_position[0] +# y = player_position[1] +# # Copy image to screen: +# screen.blit(player_image, [x, y]) +# pygame.display.flip() +# clock.tick(60) +# +# pygame.quit() +# pygame.mouse.set_visible(0) +# +# background = pygame.Surface(screen.get_size()) +# background = background.convert() +# background.fill((250, 250, 250)) +# + + + + + +# # Put Text On The Background, Centered +# if pygame.font: +# font = pygame.font.Font(None, 36) +# text = font.render("Pummel The Chimp, And Win $$$", 1, (10, 10, 10)) +# textpos = text.get_rect(centerx=background.get_width()/2) +# background.blit(text, textpos) +# +# # Display The Background While Setup Finishes +# screen.blit(background, (0, 0)) +# pygame.display.flip() +# +# # prepare game objects +# whiff_sound = load_sound('whiff.wav') +# punch_sound = load_sound('punch.wav') +# chimp = Chimp() +# fist = Fist() +# allsprites = pygame.sprite.RenderPlain((fist, chimp)) +# clock = pygame.time.Clock() +# +# # Main loop +# while 1: +# clock.tick(60) +# +# # handle all input events +# for event in pygame.event.get(): +# if event.type == QUIT: +# return +# elif event.type == KEYDOWN and event.key == K_ESCAPE: +# return +# elif event.type == MOUSEBUTTONDOWN: +# if fist.punch(chimp): +# punch_sound.play() #punch +# chimp.punched() +# else: +# whiff_sound.play() #miss +# elif event.type == MOUSEBUTTONUP: +# fist.unpunch() +# +# allsprites.update() +# +# # draw all objects +# screen.blit(background, (0, 0)) +# allsprites.draw(screen) +# pygame.display.flip() +def get_init(): + """return true if the pygame module has been initialized.""" + # return "sys" in sys.modules + pass + +def load_image(name, colorkey=None): + """Loading Resources""" +# fullname = os.path.join('data', name) +# try: +# image = pygame.image.load(fullname) +# except(pygame.error, message): +# print('Cannot load image:', name) +# raise(SystemExit, message) +# image = image.convert() +# if colorkey is not None: +# if colorkey is -1: +# colorkey = image.get_at((0,0)) +# image.set_colorkey(colorkey, RLEACCEL) +# return image, image.get_rect() + pass diff --git a/track.png b/track.png new file mode 100644 index 0000000..9b1e5c0 Binary files /dev/null and b/track.png differ