-
Notifications
You must be signed in to change notification settings - Fork 8
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
vivienyuwenchen
wants to merge
41
commits into
sd17fall:master
Choose a base branch
from
vivienyuwenchen:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 7d4d56a
Jumping Dot That Moves Side To Side
hthomas60 7881c09
Add player and keyboard control
vivienyuwenchen a72aed8
Merge branch 'master' of https://github.com/vivienyuwenchen/Interacti…
vivienyuwenchen ae5ded3
Add player class, collision detection, and game over
vivienyuwenchen 035bf5d
Added player summuned obsticals
860cf7c
Add stamina bar
vivienyuwenchen dd67c71
player two spawns obsticals at different hights
9f2156e
merged
a98883f
Ad project writeup and reflection
vivienyuwenchen 41fc448
added player 2 stamia bar
757dba6
Merge branch 'master' of https://github.com/vivienyuwenchen/Interacti…
7bab5b1
Fix isCollide to work at different heights
vivienyuwenchen 0ede561
Change restart game key to space bar
vivienyuwenchen 3d5ef9d
Add docstrings and comments
vivienyuwenchen 6b22ee8
Fixed stamina bar regeneration
d55ede1
Added score and changed endgame scene
a4057a2
Update project writeup and reflection
vivienyuwenchen c6a297c
Add screenshots of game
vivienyuwenchen 522df62
Add screenshots of game
vivienyuwenchen ea85407
Update README.md
vivienyuwenchen 734db7c
Update project writeup and reflection
vivienyuwenchen 4edcbf7
Merge branch 'master' of https://github.com/vivienyuwenchen/Interacti…
vivienyuwenchen 58d7c1b
Added Captions To figures
hthomas60 af2ed11
Fixed Caption Spacing
hthomas60 0f26809
Update inline comments
vivienyuwenchen c66f558
Merge branch 'master' of https://github.com/vivienyuwenchen/Interacti…
vivienyuwenchen dd5ea39
Replace Barleft with Bar
vivienyuwenchen f877988
Added Player reviews
hthomas60 a896506
Added Player reviews
hthomas60 20ba136
Added Player reviews
hthomas60 d33d9c2
Added Player reviews
hthomas60 4441f05
Added Player reviews
hthomas60 6a55531
Added Player reviews
hthomas60 5fb8cc9
Added Player reviews
hthomas60 ce5b8eb
Same
hthomas60 886ec96
Add UML diagram
vivienyuwenchen a8bfe17
Merge branch 'master' of https://github.com/vivienyuwenchen/Interacti…
vivienyuwenchen eea96f3
Add UML diagram
vivienyuwenchen b97a0c0
Update project writeup and reflection
vivienyuwenchen cea81cc
Revised for MP5 with suggestions from feedback and start menu
vivienyuwenchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
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 | ||
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() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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
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] |
This file contains hidden or 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
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() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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!