Skip to content
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

Assignment 1 Required Alterations #2

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
60 changes: 60 additions & 0 deletions Bird - Copy.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
--[[
Bird Class
Author: Colton Ogden
[email protected]

The Bird is what we control in the game via clicking or the space bar; whenever we press either,
the bird will flap and go up a little bit, where it will then be affected by gravity. If the bird hits
the ground or a pipe, the game is over.
]]

Bird = Class{}

-- i am so bad at the game I needed to make it easier to properly test
local GRAVITY = 10 --20

function Bird:init()
self.image = love.graphics.newImage('bird.png')
self.x = VIRTUAL_WIDTH / 2 - 8
self.y = VIRTUAL_HEIGHT / 2 - 8

self.width = self.image:getWidth()
self.height = self.image:getHeight()

self.dy = 0
end

--[[
AABB collision that expects a pipe, which will have an X and Y and reference
global pipe width and height values.
]]
function Bird:collides(pipe)
-- the 2's are left and top offsets
-- the 4's are right and bottom offsets
-- both offsets are used to shrink the bounding box to give the player
-- a little bit of leeway with the collision
if (self.x + 2) + (self.width - 4) >= pipe.x and self.x + 2 <= pipe.x + PIPE_WIDTH then
if (self.y + 2) + (self.height - 4) >= pipe.y and self.y + 2 <= pipe.y + PIPE_HEIGHT then
return true
end
end

return false
end

function Bird:update(dt)
self.dy = self.dy + GRAVITY * dt

-- burst of anti-gravity when space or left mouse are pressed
if love.keyboard.wasPressed('space') or love.mouse.wasPressed(1) then
-- making easier so I could properly test
self.dy = -2 --5
sounds['jump']:play()
end

self.y = self.y + self.dy
end

function Bird:render()
love.graphics.draw(self.image, self.x, self.y)
end
6 changes: 4 additions & 2 deletions Bird.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

Bird = Class{}

local GRAVITY = 20
-- I am so bad at the game I needed to make it easier to properly test
local GRAVITY = 10 --20

function Bird:init()
self.image = love.graphics.newImage('bird.png')
Expand Down Expand Up @@ -46,7 +47,8 @@ function Bird:update(dt)

-- burst of anti-gravity when space or left mouse are pressed
if love.keyboard.wasPressed('space') or love.mouse.wasPressed(1) then
self.dy = -5
-- making easier so I could properly test
self.dy = -2 --5
sounds['jump']:play()
end

Expand Down
Binary file added Pause.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions PipePair.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
PipePair = Class{}

-- size of the gap between pipes
local GAP_HEIGHT = 90
--local GAP_HEIGHT = 90

function PipePair:init(y)
function PipePair:init(y, g) --previously just took y now adding a g variable for gap
-- flag to hold whether this pair has been scored (jumped through)
self.scored = false

Expand All @@ -26,7 +26,7 @@ function PipePair:init(y)
-- instantiate two pipes that belong to this pair
self.pipes = {
['upper'] = Pipe('top', self.y),
['lower'] = Pipe('bottom', self.y + PIPE_HEIGHT + GAP_HEIGHT)
['lower'] = Pipe('bottom', self.y + PIPE_HEIGHT + g)
}

-- whether this pipe pair is ready to be removed from the scene
Expand Down
27 changes: 27 additions & 0 deletions Trophy.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--[[
Trophy Class
Author: Cameron Nelson

]]

Trophy = Class{}


-- Take in the image file as paremeter
function Trophy:init(i)
self.image = love.graphics.newImage(i) -- New image based off of the passed parameter

self.width = self.image:getWidth()
self.height = self.image:getHeight()

-- Set the x to place the center of image centered on screen horizantally
self.x = VIRTUAL_WIDTH / 2 - (self.width / 2)
-- Set y value to be in lower third of screen
self.y = VIRTUAL_HEIGHT * .75
end



function Trophy:render()
love.graphics.draw(self.image, self.x, self.y)
end
Binary file added bronze.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 gold.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 19 additions & 12 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ push = require 'push'
-- https://github.com/vrld/hump/blob/master/class.lua
Class = require 'class'

-- physical screen dimensions
WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720

-- virtual resolution dimensions
VIRTUAL_WIDTH = 512
VIRTUAL_HEIGHT = 288

-- require the class files
require 'Trophy'
require 'Bird'
require 'Pipe'
require 'PipePair'

-- a basic StateMachine class which will allow us to transition to and from
-- game states smoothly and avoid monolithic code in one file
require 'StateMachine'
Expand All @@ -36,20 +50,10 @@ require 'StateMachine'
require 'states/BaseState'
require 'states/CountdownState'
require 'states/PlayState'
require 'states/PauseState'
require 'states/ScoreState'
require 'states/TitleScreenState'

require 'Bird'
require 'Pipe'
require 'PipePair'

-- physical screen dimensions
WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720

-- virtual resolution dimensions
VIRTUAL_WIDTH = 512
VIRTUAL_HEIGHT = 288

local background = love.graphics.newImage('background.png')
local backgroundScroll = 0
Expand All @@ -62,6 +66,7 @@ local GROUND_SCROLL_SPEED = 60

local BACKGROUND_LOOPING_POINT = 413


function love.load()
-- initialize our nearest-neighbor filter
love.graphics.setDefaultFilter('nearest', 'nearest')
Expand All @@ -85,6 +90,7 @@ function love.load()
['explosion'] = love.audio.newSource('explosion.wav', 'static'),
['hurt'] = love.audio.newSource('hurt.wav', 'static'),
['score'] = love.audio.newSource('score.wav', 'static'),
['pause'] = love.audio.newSource('pause.wav', 'static'),

-- https://freesound.org/people/xsgianni/sounds/388079/
['music'] = love.audio.newSource('marios_way.mp3', 'static')
Expand All @@ -106,7 +112,8 @@ function love.load()
['title'] = function() return TitleScreenState() end,
['countdown'] = function() return CountdownState() end,
['play'] = function() return PlayState() end,
['score'] = function() return ScoreState() end
['score'] = function() return ScoreState() end,
['pause'] = function() return PauseState() end
}
gStateMachine:change('title')

Expand Down
Binary file added pause.wav
Binary file not shown.
Binary file added silver.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion states/CountdownState.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ CountdownState = Class{__includes = BaseState}
-- takes 1 second to count down each time
COUNTDOWN_TIME = 0.75



function CountdownState:init()
self.count = 3
self.timer = 0
Expand All @@ -34,7 +36,7 @@ function CountdownState:update(dt)

-- when 0 is reached, we should enter the PlayState
if self.count == 0 then
gStateMachine:change('play')
gStateMachine:change('play', {timer = -1})
end
end
end
Expand Down
30 changes: 30 additions & 0 deletions states/PauseState.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@


PauseState = Class{__includes = BaseState}


-- enter the pause state with the current game's class
function PauseState:enter(params)
-- save all the game's data into an object
self.bird = params.bird
self.score = params.score
self.pipePairs = params.pipePairs
self.timer = params.timer
self.lastY = params.lastY
sounds['music']:pause()-- pause the music
sounds['pause']:play() -- play the pause sound
end

function PauseState:update(dt)
-- go back to play with p is pressed
if love.keyboard.wasPressed('p') then
sounds['music']:resume() -- resume the song
gStateMachine:change('play', self) -- go to play state and pass back the current game
end
end

function PauseState:render()
-- render the pause graphic
love.graphics.draw(love.graphics.newImage('pause.png'), VIRTUAL_WIDTH / 2 - (50), VIRTUAL_HEIGHT / 2 - 50)

end
31 changes: 23 additions & 8 deletions states/PlayState.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ function PlayState:init()
self.lastY = -PIPE_HEIGHT + math.random(80) + 20
end

gGame = PlayState()

function PlayState:update(dt)
-- update timer for pipe spawning
self.timer = self.timer + dt

-- spawn a new pipe pair every second and a half
if self.timer > 2 then
-- create pipes randomly between 2 and 100 dt instead of just 2 dt
if self.timer > math.random(2, 100) then
-- modify the last Y coordinate we placed so pipe gaps aren't too far apart
-- no higher than 10 pixels below the top edge of the screen,
-- and no lower than a gap length (90 pixels) from the bottom
Expand All @@ -41,7 +44,7 @@ function PlayState:update(dt)
self.lastY = y

-- add a new pipe pair at the end of the screen at our new Y
table.insert(self.pipePairs, PipePair(y))
table.insert(self.pipePairs, PipePair(y, math.random(70, 120)))

-- reset timer
self.timer = 0
Expand Down Expand Up @@ -99,6 +102,11 @@ function PlayState:update(dt)
score = self.score
})
end

-- if p key is pressed then change gamestate to pause and pass the current game's object
if love.keyboard.wasPressed('p') then
gStateMachine:change('pause', self)
end
end

function PlayState:render()
Expand All @@ -112,14 +120,21 @@ function PlayState:render()
self.bird:render()
end

--[[
Called when this state is transitioned to from another state.
]]
function PlayState:enter()
-- if we're coming from death, restart scrolling


function PlayState:enter(params)
-- timer variable used to check if we are entering from countdown state or pause state
timer = params.timer
-- if from pause state then load in the current game
if timer > -1 then
self.bird = params.bird
self.score = params.score
self.pipePairs = params.pipePairs
self.timer = params.timer
self.lastY = params.lastY
end
scrolling = true
end

--[[
Called when this state changes to another state.
]]
Expand Down
13 changes: 13 additions & 0 deletions states/ScoreState.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,17 @@ function ScoreState:render()
love.graphics.printf('Score: ' .. tostring(self.score), 0, 100, VIRTUAL_WIDTH, 'center')

love.graphics.printf('Press Enter to Play Again!', 0, 160, VIRTUAL_WIDTH, 'center')


-- based off of the score recieved render the appropriate trophy
if self.score > 10 then
trophy = Trophy('gold.png')
trophy:render()
elseif self.score > 5 then
trophy = Trophy('silver.png')
trophy:render()
elseif self.score > 2 then
trophy = Trophy('bronze.png')
trophy:render()
end
end