forked from sd17fall/InteractiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Revised for MP5 with suggestions from feedback and start menu #1
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
1
commit into
release
Choose a base branch
from
master
base: release
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
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 |
|---|---|---|
| @@ -1,8 +1,21 @@ | ||
| """ | ||
| 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 misc import * | ||
| from models import * | ||
| from config import * | ||
|
|
||
|
|
||
| class Game4Main: | ||
|
|
@@ -20,73 +33,96 @@ def __init__(self, width=480,height=360): | |
| # create clock | ||
| self.clock = pygame.time.Clock() | ||
| self.score = 0 | ||
| self.oneplayer = True | ||
|
|
||
|
|
||
| def mainLoop(self): | ||
| 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.""" | ||
| # initialize player | ||
| # start count | ||
| count = 0 | ||
| play_len = 25 | ||
| play_x = 100 | ||
| play_y = self.height - play_len | ||
| player = Player(play_x, play_y, play_len, self.screen) | ||
|
|
||
| # initialize player | ||
| player = Player(PLAY_X, self.height-PLAY_LEN, PLAY_LEN, self.screen) | ||
|
|
||
| # initialize stamina bars | ||
| P1_stamina_bar = StaminaBar(self.screen,25,"WHITE") | ||
| P2_stamina_bar = StaminaBar(self.screen,350,"RED") | ||
| 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_len = 25 | ||
| obs_x = self.width | ||
| obs_y = self.height - obs_len | ||
| obstical_height = self.height - obs_len | ||
| OBS_X = self.width | ||
| OBS_Y = self.height - OBS_LEN | ||
|
|
||
| # create list of obstacles | ||
| obstacles = [] | ||
| new_obstical = False | ||
| # uncomment and place line below in [] of obstacles = [] to generate random obstacles: | ||
| # Obstacle(obs_x, obs_y, obs_len, self.screen,'BLUE') | ||
|
|
||
| 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 1: | ||
| while True: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay while |
||
| 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() | ||
| # check keyboard for obstacle player | ||
| if event.type == pygame.KEYDOWN: | ||
| # z to create obstacle at ground level | ||
| if event.key == pygame.K_z: | ||
| new_obstical = True | ||
| obstical_height = obs_y | ||
| # x to create obstacle at second level | ||
| if event.key == pygame.K_x: | ||
| new_obstical = True | ||
| obstical_height = obs_y-25 | ||
| # c to create obstacle at third level | ||
| if event.key == pygame.K_c: | ||
| new_obstical = True | ||
| obstical_height = obs_y-50 | ||
| 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() | ||
| # up to jump | ||
| if pressed[pygame.K_UP] and P1_stamina_bar.bars >= player.jumpcost: | ||
| if player.play_y == (360 - player.play_len): | ||
| P1_stamina_bar.decreaseBar(player.jumpcost) | ||
| 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() | ||
| # left to move left | ||
| if pressed[pygame.K_LEFT]: | ||
| player.moveLeft() | ||
| # right to move right | ||
| player.move_left() | ||
| if pressed[pygame.K_RIGHT]: | ||
| player.moveRight() | ||
| player.move_right() | ||
|
|
||
| # refresh screen | ||
| self.screen.fill(colors['BLACK']) | ||
|
|
@@ -98,68 +134,98 @@ def mainLoop(self): | |
| # update current time | ||
| current_time = pygame.time.get_ticks() | ||
|
|
||
| # generate obstacle at random time | ||
| if (current_time - prev_time > obs_dt): | ||
| # uncomment below to generate random obstacles: | ||
| # obstacles.append(Obstacle(obs_x, obs_y, obs_len, self.screen,'BLUE')) | ||
| new_obstical = False | ||
| prev_time = current_time | ||
| obs_dt = randint(1000, 3000) | ||
| # generate obstacle player's obstacle at appropriate height | ||
| if (new_obstical == True and P2_stamina_bar.bars >= 33): | ||
| new_obstical = False | ||
| P2_stamina_bar.decreaseBar(33) | ||
| obstacles.append(Obstacle(obs_x, obstical_height, obs_len, self.screen,'RED')) | ||
| 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.moveForward() | ||
| obstacle.move_forward() | ||
| obstacle.draw() | ||
| # check for collision between player and obstacles | ||
| if player.isCollide(obstacle.obs_x, obstacle.obs_y, obstacle.obs_len): | ||
| self.gameOver(str(count)) | ||
| 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.isGone()] | ||
| obstacles = [obstacle for obstacle in obstacles if not obstacle.is_gone()] | ||
|
|
||
| # update stamina bars | ||
| P1_stamina_bar.increaseBar() | ||
| P2_stamina_bar.increaseBar(1.5) | ||
| P1_stamina_bar.increase_bar() | ||
| P1_stamina_bar.draw() | ||
| P2_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", 72) | ||
| text = font.render(str(count), True, (255,255,255)) | ||
| self.screen.blit(text,[200,10]) | ||
| 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 gameOver(self,score): | ||
| def game_over(self, score): | ||
| """Game over screen.""" | ||
| # main event loop | ||
| while 1: | ||
| for event in pygame.event.get(): | ||
| if event.type == pygame.QUIT: | ||
| sys.exit() | ||
| 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.mainLoop() | ||
| 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", 28) | ||
| text = font.render("Player 1 recived a score of " + str(score), True, (255,255,255)) | ||
| self.screen.blit(text,[50,140]) | ||
| text = font.render("Press Space Bar to play again", True, (255,255,255)) | ||
| self.screen.blit(text,[50,178]) | ||
| 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().mainLoop() | ||
| 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 |
|---|---|---|
| @@ -1,25 +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] | ||
| 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] |
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.
awesome!! all CAPS constants are so much cleaner