Skip to content

Complete interactive programming #3

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 41 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
2de033a
Create main game class
vivienyuwenchen Oct 18, 2017
7d4d56a
Jumping Dot That Moves Side To Side
hthomas60 Oct 19, 2017
7881c09
Add player and keyboard control
vivienyuwenchen Oct 19, 2017
a72aed8
Merge branch 'master' of https://github.com/vivienyuwenchen/Interacti…
vivienyuwenchen Oct 19, 2017
ae5ded3
Add player class, collision detection, and game over
vivienyuwenchen Oct 21, 2017
035bf5d
Added player summuned obsticals
Oct 22, 2017
860cf7c
Add stamina bar
vivienyuwenchen Oct 23, 2017
dd67c71
player two spawns obsticals at different hights
Oct 23, 2017
9f2156e
merged
Oct 23, 2017
a98883f
Ad project writeup and reflection
vivienyuwenchen Oct 24, 2017
41fc448
added player 2 stamia bar
Oct 24, 2017
757dba6
Merge branch 'master' of https://github.com/vivienyuwenchen/Interacti…
Oct 24, 2017
7bab5b1
Fix isCollide to work at different heights
vivienyuwenchen Oct 25, 2017
0ede561
Change restart game key to space bar
vivienyuwenchen Oct 26, 2017
3d5ef9d
Add docstrings and comments
vivienyuwenchen Oct 26, 2017
6b22ee8
Fixed stamina bar regeneration
Oct 28, 2017
d55ede1
Added score and changed endgame scene
Oct 29, 2017
a4057a2
Update project writeup and reflection
vivienyuwenchen Oct 29, 2017
c6a297c
Add screenshots of game
vivienyuwenchen Oct 30, 2017
522df62
Add screenshots of game
vivienyuwenchen Oct 30, 2017
ea85407
Update README.md
vivienyuwenchen Oct 30, 2017
734db7c
Update project writeup and reflection
vivienyuwenchen Oct 30, 2017
4edcbf7
Merge branch 'master' of https://github.com/vivienyuwenchen/Interacti…
vivienyuwenchen Oct 30, 2017
58d7c1b
Added Captions To figures
hthomas60 Oct 30, 2017
af2ed11
Fixed Caption Spacing
hthomas60 Oct 30, 2017
0f26809
Update inline comments
vivienyuwenchen Oct 30, 2017
c66f558
Merge branch 'master' of https://github.com/vivienyuwenchen/Interacti…
vivienyuwenchen Oct 30, 2017
dd5ea39
Replace Barleft with Bar
vivienyuwenchen Oct 30, 2017
f877988
Added Player reviews
hthomas60 Oct 30, 2017
a896506
Added Player reviews
hthomas60 Oct 30, 2017
20ba136
Added Player reviews
hthomas60 Oct 30, 2017
d33d9c2
Added Player reviews
hthomas60 Oct 30, 2017
4441f05
Added Player reviews
hthomas60 Oct 30, 2017
6a55531
Added Player reviews
hthomas60 Oct 30, 2017
5fb8cc9
Added Player reviews
hthomas60 Oct 30, 2017
ce5b8eb
Same
hthomas60 Oct 30, 2017
886ec96
Add UML diagram
vivienyuwenchen Oct 30, 2017
a8bfe17
Merge branch 'master' of https://github.com/vivienyuwenchen/Interacti…
vivienyuwenchen Oct 30, 2017
eea96f3
Add UML diagram
vivienyuwenchen Oct 30, 2017
b97a0c0
Update project writeup and reflection
vivienyuwenchen Oct 30, 2017
cea81cc
Revised for MP5 with suggestions from feedback and start menu
vivienyuwenchen Dec 10, 2017
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
231 changes: 231 additions & 0 deletions Game4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
"""
Blockade is competitive arcade game. It has two players. Player 1 is a white
block that is continually moving forward. Player 2’s job is to stop Player 1 by
throwing obstacles in its way. Player 1 can avoid obstacles by jumping over or
ducking under them. Both players are limited by a stamina bar. Actions such
jumping or throwing obstacles depletes player’s stamina and if the stamina gets
too low players cannot perform actions. The game uses keyboard inputs as commands.

@author: Vivien Chen and Harrison Young
"""


import os, sys

Choose a reason for hiding this comment

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

Please add a few lines of comments explaining what this cool game is about!

import pygame
from random import randint

from models import *
from config import *


class Game4Main:
"""The Main Game4 Class - This class handles the main
initialization and creating of the Game."""

def __init__(self, width=480,height=360):
# initialize pygame
pygame.init()
# set window size
self.width = width
self.height = height
# create screen
self.screen = pygame.display.set_mode((self.width, self.height))
# create clock
self.clock = pygame.time.Clock()
self.score = 0
self.oneplayer = True


def start_menu(self):
"""Start screen for game."""
while True:
if pygame.QUIT in {event.type for event in pygame.event.get()}:
sys.exit()

# display start menu
self.screen.fill(colors['BLACK'])
# title
font = pygame.font.SysFont("comicsansms", int(self.width/8))
text = font.render("Blockade", True, colors['WHITE'])
text_rect = text.get_rect(center=(self.width/2, self.height/3))
self.screen.blit(text, text_rect)
# instructions
font = pygame.font.SysFont("comicsansms", int(self.width/30))
text = font.render("Player 1: Arrow Keys, Player 2: ZXC", True, colors['WHITE'])
text_rect = text.get_rect(center=(self.width/2, self.height/2))
self.screen.blit(text, text_rect)
# buttons
self.button("One Player",self.width*1/8-20,self.height*3/4,colors['GREEN'],True,self.main_loop)
self.button("Two Player",self.width*5/8-20,self.height*3/4,colors['RED'],False,self.main_loop)

# update screen
pygame.display.flip()


def main_loop(self):
"""Main screen for game."""
# start count
count = 0

# initialize player
player = Player(PLAY_X, self.height-PLAY_LEN, PLAY_LEN, self.screen)

# initialize stamina bars
P1_stamina_bar = StaminaBar(self.screen, P1_STAMINA_BAR_OFFSET, 'WHITE')
P2_stamina_bar = StaminaBar(self.screen, self.width - P2_STAMINA_BAR_OFFSET, 'RED')

# initialize obstacle length, x and y coordinates
OBS_X = self.width
OBS_Y = self.height - OBS_LEN

# create list of obstacles
if self.oneplayer:
obstacles = [Obstacle(OBS_X, OBS_Y, OBS_LEN, self.screen,'BLUE')]
else:
obstacles = []
new_obstacle = False
obstacle_height = OBS_Y

# initialize time variables
prev_time = 0
obs_dt = 500

# main event loop
while True:
count+=1
# if pygame.QUIT in {event.type for event in pygame.event.get()}:
# sys.exit()
# elif pygame.KEYDOWN in {event.type for event in pygame.event.get()}:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if not self.oneplayer:
# check keyboard for obstacle player
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_z:
new_obstacle = True
obstacle_height = OBS_Y-LEVEL_OFFSETS['ground']
if event.key == pygame.K_x:
new_obstacle = True
obstacle_height = OBS_Y-LEVEL_OFFSETS['first']
if event.key == pygame.K_c:
new_obstacle = True
obstacle_height = OBS_Y-LEVEL_OFFSETS['second']

# check keyboard for main player
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP] and P1_stamina_bar.bars >= JUMP_COST:
if player.play_y == (self.height - player.play_len):
P1_stamina_bar.decrease_bar(JUMP_COST)
player.jump()
P1_stamina_bar.draw()
if pressed[pygame.K_LEFT]:
player.move_left()
if pressed[pygame.K_RIGHT]:
player.move_right()

# refresh screen
self.screen.fill(colors['BLACK'])

# update and draw player
player.update()
player.draw()

# update current time
current_time = pygame.time.get_ticks()

if self.oneplayer:
# generate obstacle at random time
if (current_time - prev_time > obs_dt):
obstacles.append(Obstacle(OBS_X, OBS_Y, OBS_LEN, self.screen,'GREEN'))
prev_time = current_time
obs_dt = randint(250, 1000)
if not self.oneplayer:
# generate obstacle player's obstacle at appropriate height
if (new_obstacle == True and P2_stamina_bar.bars >= OBSTACLE_COST):
new_obstacle = False
P2_stamina_bar.decrease_bar(OBSTACLE_COST)
obstacles.append(Obstacle(OBS_X, obstacle_height, OBS_LEN, self.screen,'RED'))

# move each obstacle forward
for obstacle in obstacles:
obstacle.move_forward()
obstacle.draw()
# check for collision between player and obstacles
if player.is_collide(obstacle.obs_x, obstacle.obs_y, obstacle.obs_len):
self.game_over(str(count))
# remove obstacle from list if off screen
obstacles = [obstacle for obstacle in obstacles if not obstacle.is_gone()]

# update stamina bars
P1_stamina_bar.increase_bar()
P1_stamina_bar.draw()
if not self.oneplayer:
P2_stamina_bar.increase_bar(1.5)
P2_stamina_bar.draw()

# display score
font = pygame.font.SysFont("comicsansms", int(self.width/8))
text = font.render(str(count), True, colors['WHITE'])
text_rect = text.get_rect(center=(self.width/2, self.height/8))
self.screen.blit(text, text_rect)

# update screen
pygame.display.flip()
self.clock.tick(30)


def game_over(self, score):
"""Game over screen."""
# main event loop
while True:
if pygame.QUIT in {event.type for event in pygame.event.get()}:
sys.exit()

# check keyboard for space key to restart game
pressed = pygame.key.get_pressed()
if pressed[pygame.K_SPACE]:
self.main_loop()
if pressed[pygame.K_v]:
self.start_menu()

# display game over screen
self.screen.fill(colors['BLACK'])
font = pygame.font.SysFont("comicsansms", int(self.width/16))
text = font.render("Score: " + str(score), True, colors['WHITE'])
text_rect = text.get_rect(center=(self.width/2, self.height*1/3))
self.screen.blit(text,text_rect)
text = font.render("Press Space Bar to play again", True, colors['WHITE'])
text_rect = text.get_rect(center=(self.width/2, self.height*1/2))
self.screen.blit(text,text_rect)
text = font.render("Press V to go to home screen", True, colors['WHITE'])
text_rect = text.get_rect(center=(self.width/2, self.height*2/3))
self.screen.blit(text,text_rect)

# update screen
pygame.display.flip()


def button(self, msg, x, y, color, oneplayer, action=None):
"Button for start screen."
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
font = pygame.font.SysFont("comicsansms", int(self.width/20))
text = font.render(msg, True, colors['WHITE'])
w = self.width/3
h = self.width/12

if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(self.screen, color, (x,y,w,h))

if click[0] == 1 and action != None:
self.oneplayer = oneplayer
action()
else:
pygame.draw.rect(self.screen, colors['BLACK'], (x,y,w,h))

self.screen.blit(text,[x+20,y])


if __name__ == "__main__":
Game4Main().start_menu()
Binary file added Old Files/gameover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions Old Files/playermoves.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pygame
from pygame.locals import *
import time
from sys import exit
pygame.init()
def jump(ctime, startloc):
"""
Changes the y position up one frame
#for i in range(21):

#print(jump(i,0)[1])
"""
over = False
h = 100
t = 20
b = startloc
c = t/2
a = h/((t/2)**2)
x = (ctime%20)
recty = (a*(x - c)**2)+b


if (x == 0):
over = True
return [recty, over]
115 changes: 115 additions & 0 deletions Old Files/pygame_base_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
"""
Pygame base template for opening a window

Sample Python/Pygame Programs
Simpson College Computer Science
http://programarcadegames.com/
http://simpson.edu/computer-science/

Explanation video: http://youtu.be/vRB_983kUMc
"""



import pygame
from playermoves import *


# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
jumping = False
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()
frames = 0;
startjump = 0
x_min = 0
x_max = 200
rectx = 100
recty = 400

# -------- Main Program Loop -----------
while not done:

# --- Main event loop

for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
#if event.type == pygame.KEYDOWN:
#
# if event.key == pygame.K_SPACE:
# if frames - startjump > 20:
# jumping = True
# startjump = frames

pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP]:
if frames - startjump > 20:
jumping = True
startjump = frames
if pressed[pygame.K_LEFT]:
if rectx > x_min:
rectx -= 5
if pressed[pygame.K_RIGHT]:
if rectx < x_max:
rectx += 5





# --- Game logic should go here

# --- Screen-clearing code goes here

# Here, we clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.

# If you want a background image, replace this clear with blit'ing the
# background image.
screen.fill(BLACK)
pygame.draw.rect(screen, RED, [0,425,700,100])
jumpclock = frames%21

# --- Drawing code should go here
# --- Drawing code should go here

jumpover = False

#if False:#if detect_key_press() == 'space':
# jumping = True #if the spacebar is pressed start the jump
# print("boing")
#else:
# print(detect_key_press())
if jumping == True:
recty = jump(frames-startjump+1,300)[0]
jumpover = jump(frames-startjump+1,300)[1]
if jumpover == True:

jumping = False

pygame.draw.rect(screen, WHITE, [rectx,recty,25,25])
# --- Go ahead and update the screen with what we've drawn
pygame.display.flip()
frames+=1 # increments frame count

# --- Limit to 60 frames per second
clock.tick(60)


# Close the window and quit.
pygame.quit()
Loading