Created
April 2, 2019 05:16
-
-
Save foopod/5611941d1568cd1fcae10421de89aff7 to your computer and use it in GitHub Desktop.
A super fun game that we made at codeclub (kinda broken)
This file contains 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
platform = {} | |
player = {} | |
obstacle = {} | |
gameover = {} | |
rbutton = {} | |
score = {} | |
function love.load() | |
love.physics.setMeter(64) --the height of a meter our worlds will be 64px | |
world = love.physics.newWorld(0, 9.81*64, true) --create a world for the bodies to exist in with horizontal gravity of 0 and vertical gravity of 9.81 | |
world:setCallbacks(beginContact) | |
rbutton.image = love.graphics.newImage('restart.png') | |
rbutton.x = 370 | |
rbutton.y = 200 | |
score = 0 | |
platform.width = love.graphics.getWidth()*10 | |
platform.height = love.graphics.getHeight() | |
platform.body = love.physics.newBody(world, 0, platform.height) --remember, the shape (the rectangle we create next) anchors to the body from its center, so we have to move it to (650/2, 650-50/2) | |
platform.shape = love.physics.newRectangleShape(platform.width, platform.height) --make a rectangle with a width of 650 and a height of 50 | |
platform.fixture = love.physics.newFixture(platform.body, platform.shape); --attach shape to body | |
player.x = love.graphics.getWidth() / 10 | |
player.y = love.graphics.getHeight() / 2 - 200 | |
player.body = love.physics.newBody(world, player.x, player.y, "dynamic") --place the body in the center of the world and make it dynamic, so it can move around | |
player.shape = love.physics.newCircleShape(20) --the ball's shape has a radius of 20 | |
player.fixture = love.physics.newFixture(player.body, player.shape, 1) -- Attach fixture to body and give it a density of 1. | |
player.fixture:setRestitution(0.2) --let the ball bounce | |
player.fixture:setUserData("Player") | |
player.speed = 200 | |
player.img = love.graphics.newImage('purple.png') | |
obstacle.x = love.graphics.getWidth() | |
obstacle.y = love.graphics.getHeight() / 2 - 18 | |
obstacle.img = love.graphics.newImage('orange.png') | |
obstacle.frequency = 1 | |
love.graphics.setBackgroundColor( 0.7, 0.8, 1 ) | |
obstacle.body = love.physics.newBody(world, obstacle.x, obstacle.y, "dynamic") --place the body in the center of the world and make it dynamic, so it can move around | |
obstacle.shape = love.physics.newCircleShape(20) --the ball's shape has a radius of 20 | |
obstacle.fixture = love.physics.newFixture(obstacle.body, obstacle.shape, 1) -- Attach fixture to body and give it a density of 1. | |
obstacle.fixture:setUserData("Obstacle") | |
highscore = getHighScore(); | |
isHighScore = false | |
gameover = false | |
font = love.graphics.newFont("font.ttf", 15) | |
end | |
function love.update(dt) | |
--[[if love.keyboard.isDown('d') then | |
if player.x < (love.graphics.getWidth() - player.img:getWidth()) then | |
player.x = player.x + (player.speed * dt) | |
end | |
elseif love.keyboard.isDown('a') then | |
if player.x > 0 then | |
player.x = player.x - (player.speed * dt) | |
end | |
end | |
]]-- | |
world:update(dt) | |
-- jumping | |
if love.keyboard.isDown('space') then | |
if player.body:isTouching(platform.body) then | |
player.body:applyForce(0, -5000) | |
end | |
end | |
-- set obstacle speed | |
xSpeed, ySpeed = obstacle.body:getLinearVelocity() | |
if xSpeed > -500 then | |
obstacle.body:applyForce(-5000*dt,0) | |
end | |
-- back to beginning | |
if obstacle.body:getX() < -300 and gameover == false then | |
obstacle.body:setX( love.graphics.getWidth()+100 ) | |
score = score+1 | |
end | |
-- check game is over | |
end | |
function love.draw() | |
love.graphics.setColor(0.28, 0.63, 0.05) -- set the drawing color to green for the ground | |
love.graphics.polygon("fill", platform.body:getWorldPoints(platform.shape:getPoints())) | |
love.graphics.setColor(1,1,1) | |
love.graphics.draw(player.img, player.body:getX(), player.body:getY(), 0, 1, 1, 0, 32) | |
love.graphics.draw(obstacle.img, obstacle.body:getX(), obstacle.body:getY(), 0, 1, 1, 0, 32) | |
if gameover then | |
GameOverScreen(font, rbutton) | |
end | |
love.graphics.printf({{1,1,1,1}, score},font, -30, 15, 800, 'center',0,2,2,0,0,0,0) -- don't add qoutes on score as we are not printing the word "score" we are printing the variable score. | |
love.graphics.printf({{1,0,0,1},"HIGH SCORE"..highscore},font, -410, 100, 800, 'center',0,2,2,0,0,0,0) | |
end | |
function beginContact(a, b, coll) | |
if a:getUserData() == "Obstacle" or a:getUserData() == "Player" then | |
if b:getUserData() == "Obstacle" or b:getUserData() == "Player" then | |
gameover = true | |
loadMaxScore() | |
highscore = getHighScore() | |
if score > highscore then | |
isHighScore = true | |
end | |
end | |
end | |
end | |
function love.mousepressed(x, y, button) | |
if button == 1 and gameover then --Left click | |
if x >= rbutton.x and x <= rbutton.x+rbutton.image:getWidth() and y >= rbutton.y and y <= rbutton.y+rbutton.image:getHeight() then --Detect if the click was inside the button | |
love.load() | |
end | |
end | |
end | |
function GameOverScreen(font, rbutton) | |
love.graphics.printf({{1,0,0,1},"GAME OVER"},font, -410, 100, 800, 'center',0,2,2,0,0,0,0) | |
love.graphics.draw(rbutton.image, rbutton.x, rbutton.y, 0, 0.1, 0.1, 0, 32) | |
if isHighScore then | |
love.graphics.printf({{1,0,0,1}, "You Bet The High Score!!!!"},font, -410, 400, 800, 'center',0,2,2,0,0,0,0) | |
love.graphics.printf({{1,0,0,1}, score},font, -410, 400, 800, 'center',0,2,2,0,0,0,0) | |
end | |
end | |
function saveMaxScore() | |
--love.graphics.printf({{1,0,0,1},"It WORKSSSSS"},font, -410, 100, 800, 'center',0,2,2,0,0,0,0) | |
-- Save the table to the "savegame.txt" file: | |
love.filesystem.write("savegame.txt", score) | |
end | |
function getHighScore() | |
temphighscore = 0 | |
if love.filesystem.getInfo("savegame.txt") ~= nil then | |
temphighscore = love.filesystem.read("savegame.txt", all) | |
temphighscore = tonumber(temphighscore) | |
end | |
return temphighscore | |
end | |
function loadMaxScore() | |
--if love.filesystem.getInfo("C:\Users\Rache\OneDrive\Documents\LOVEGames\savegame.txt") == nil then | |
--if love.filesystem.getInfo("savegame.txt") == nil then | |
-- saveMaxScore() | |
--else | |
--oldscore = love.filesystem.read("savegame.txt", all) | |
--oldscore = tonumber(oldscore) | |
if getHighScore() == 0 then | |
saveMaxScore() | |
end | |
-- Load the data table: | |
--local data = love.filesystem.load("savegame.txt")() | |
-- Copy the variables out of the table: | |
--maxScor = data.maxScor | |
--return maxScor | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment