Skip to content

Final Submission MP4 #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Audi_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Project Reflection-MP4.pdf
Binary file not shown.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Binary file added apocalypse red sky.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ball.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added concrete.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
252 changes: 252 additions & 0 deletions game.py
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By convention, constants are ALL_CAPS e.g. WHITE.

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)

"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a docstring (it's not documenting a module, class, method, or function), so it should be a # comment instead of a """string""".

Initialize the game
"""
pygame.init()
Copy link
Contributor

@osteele osteele Dec 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To take this to the next level, moving the top-level code into a function.

Then you can name the function, which makes the code more readable. For example, if the function is called initialize_game or init_game or game_init, then it's evident what it does even without the comment. Also, this gives you a place to attach the docstring (and examples!), if it is still useful.

This also makes it easier to test the code. This is (mostly) beyond the scope of this class, but it's a good habit to get into.

The global variables would be become attributes of a class, e.g. Game. The functions below would use e.g. self. barriers_list instead of global barriers_list.

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parens not necessary.

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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you organized your code into a bunch of different functions rather than cramming it all into the main loop 😺

"""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()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: leave space between functions

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()
44 changes: 44 additions & 0 deletions players.py
Original file line number Diff line number Diff line change
@@ -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</i>
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)
Binary file added road.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added road1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added road2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added road3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading