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

Added a new section of projects related to Game Development along wit… #483

Open
wants to merge 1 commit into
base: main
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
36 changes: 36 additions & 0 deletions Game Dev Projects/Pong-Final/Ball.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Ball=Class{}

function Ball:init(x,y,width,height)
self.x=x
self.y=y
self.width=width
self.height=height
self.dx=math.random(2)==1 and 100 or -100
self.dy=math.random(-50,50)
end

function Ball:collides(paddle)
if self.x > paddle.x+paddle.width or paddle.x > ball.x+ball.width then
return false
end
if self.y> paddle.y+paddle.height or paddle.y > self.y+self.height then
return false
end
return true
end

function Ball:reset()
self.x=VIRTUAL_WIDTH/2-2
self.y=VIRTUAL_HEIGHT/2-2
self.dx = math.random(2) == 1 and -100 or 100
self.dy = math.random(-50, 50)
end

function Ball:update(dt)
self.x=self.x+self.dx*dt
self.y=self.y+self.dy*dt
end

function Ball:render()
love.graphics.rectangle('fill',self.x,self.y,self.width,self.height)
end
20 changes: 20 additions & 0 deletions Game Dev Projects/Pong-Final/Paddle.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Paddle=Class{}

function Paddle:init(x,y,width,height)
self.x=x
self.y=y
self.width=width
self.height=height
self.dy=0
end

function Paddle:update(dt)
if self.dy<0 then
self.y=math.max(0,self.y+self.dy*dt)
else
self.y = math.min(VIRTUAL_HEIGHT - self.height, self.y + self.dy * dt)
end
end
function Paddle:render()
love.graphics.rectangle('fill', self.x, self.y, self.width, self.height)
end
1 change: 1 addition & 0 deletions Game Dev Projects/Pong-Final/Pong-The-Game
Submodule Pong-The-Game added at 4e6ba0
98 changes: 98 additions & 0 deletions Game Dev Projects/Pong-Final/class.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
--[[
Copyright (c) 2010-2013 Matthias Richter

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

Except as contained in this notice, the name(s) of the above copyright holders
shall not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]--

local function include_helper(to, from, seen)
if from == nil then
return to
elseif type(from) ~= 'table' then
return from
elseif seen[from] then
return seen[from]
end

seen[from] = to
for k,v in pairs(from) do
k = include_helper({}, k, seen) -- keys might also be tables
if to[k] == nil then
to[k] = include_helper({}, v, seen)
end
end
return to
end

-- deeply copies `other' into `class'. keys in `other' that are already
-- defined in `class' are omitted
local function include(class, other)
return include_helper(class, other, {})
end

-- returns a deep copy of `other'
local function clone(other)
return setmetatable(include({}, other), getmetatable(other))
end

local function new(class)
-- mixins
class = class or {} -- class can be nil
local inc = class.__includes or {}
if getmetatable(inc) then inc = {inc} end

for _, other in ipairs(inc) do
if type(other) == "string" then
other = _G[other]
end
include(class, other)
end

-- class implementation
class.__index = class
class.init = class.init or class[1] or function() end
class.include = class.include or include
class.clone = class.clone or clone

-- constructor call
return setmetatable(class, {__call = function(c, ...)
local o = setmetatable({}, c)
o:init(...)
return o
end})
end

-- interface for cross class-system compatibility (see https://github.com/bartbes/Class-Commons).
if class_commons ~= false and not common then
common = {}
function common.class(name, prototype, parent)
return new{__includes = {prototype, parent}}
end
function common.instance(class, ...)
return class(...)
end
end


-- the module
return setmetatable({new = new, include = include, clone = clone},
{__call = function(_,...) return new(...) end})
Binary file added Game Dev Projects/Pong-Final/font.ttf
Binary file not shown.
193 changes: 193 additions & 0 deletions Game Dev Projects/Pong-Final/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
push= require 'push'
Class=require 'class'
require 'Paddle'
require 'Ball'

WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720

VIRTUAL_WIDTH = 432
VIRTUAL_HEIGHT = 243
PADDLE_SPEED=200
function love.load()
love.graphics.setDefaultFilter('nearest', 'nearest')
love.window.setTitle('Pong')
math.randomseed(os.time())
smallFont=love.graphics.newFont('font.ttf', 8)
scoreFont=love.graphics.newFont('font.ttf', 32)
largeFont=love.graphics.newFont('font.ttf', 20)
love.graphics.setFont(smallFont)
sounds = {
['paddle_hit'] = love.audio.newSource('sounds/paddle_hit.wav', 'static'),
['score'] = love.audio.newSource('sounds/score.wav', 'static'),
['wall_hit'] = love.audio.newSource('sounds/wall_hit.wav', 'static')
}
push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
fullscreen = false,
resizable = true,
vsync = true
})
player1Score = 0
player2Score = 0
servingPlayer=1
player1=Paddle(10,30,5,20)
player2=Paddle(VIRTUAL_WIDTH-10,VIRTUAL_HEIGHT-30,5,20)
ball=Ball(VIRTUAL_WIDTH / 2 - 2, VIRTUAL_HEIGHT / 2 - 2, 4, 4)
gameState='start'
end

function love.resize(w, h)
push:resize(w, h)
end

function love.update(dt)
if gameState=='serve' then
ball.dy=math.random(-50,50)
if servingPlayer==1 then
ball.dx=math.random(140,200)
else
ball.dx=-math.random(140,200)
end
elseif gameState == 'play' then
if ball:collides(player1) then
ball.dx=-ball.dx*1.07
ball.x=player1.x+5
if ball.dy < 0 then
ball.dy=-math.random(10,150)
else
ball.dy=math.random(10,150)
end
sounds['paddle_hit']:play()
end
if ball:collides(player2) then
ball.dx = -ball.dx * 1.03
ball.x = player2.x - 4
if ball.dy < 0 then
ball.dy = -math.random(10, 150)
else
ball.dy = math.random(10, 150)
end
sounds['paddle_hit']:play()
end
if ball.y<=0 then
ball.y=0
ball.dy=-ball.dy
sounds['wall_hit']:play()
end
if ball.y>=VIRTUAL_HEIGHT-4 then
ball.y=VIRTUAL_HEIGHT-4
ball.dy=-ball.dy
sounds['wall_hit']:play()
end
if ball.x<0 then
servingPlayer = 1
player2Score= player2Score + 1
sounds['score']:play()
if player2Score==10 then
winningPlayer=2
gameState='done'
else
gameState='serve'
ball:reset()
end
end
if ball.x > VIRTUAL_WIDTH then
servingPlayer = 2
player1Score = player1Score + 1
sounds['score']:play()
if player1Score==10 then
winningPlayer=1
gameState='done'
else
gameState='serve'
ball:reset()
end
end
end
if love.keyboard.isDown('w') then
player1.dy=-PADDLE_SPEED
elseif love.keyboard.isDown('s') then
player1.dy=PADDLE_SPEED
else
player1.dy=0
end
--player 2 movement
if love.keyboard.isDown('up') then
player2.dy=-PADDLE_SPEED
elseif love.keyboard.isDown('down') then
player2.dy=PADDLE_SPEED
else
player2.dy=0
end
if gameState=='play' then
ball:update(dt)
end
player1:update(dt)
player2:update(dt)
end

function love.keypressed(key)
if key=='escape' then
love.event.quit()
elseif key=='enter' or key=='return' then
if gameState=='start' then
gameState='serve'
elseif gameState=='serve' then
gameState='play'
elseif gameState=='done' then
gameState='serve'
ball:reset()
player1Score=0
player2Score=0
if winningPlayer==1 then
servingPlayer=2
else
servingPlayer=1
end
end
end
end

function love.draw()
push:apply('start')
love.graphics.clear(40/255,45/255,52/255,255/255)
--message
love.graphics.setFont(smallFont)
displayScore()
if gameState=='start' then
love.graphics.setFont(smallFont)
love.graphics.printf('Welcome to Pong!', 0, 10, VIRTUAL_WIDTH, 'center')
love.graphics.printf('Press Enter to begin!', 0, 20, VIRTUAL_WIDTH, 'center')
elseif gameState=='serve' then
love.graphics.setFont(smallFont)
love.graphics.printf('Player ' .. tostring(servingPlayer) .. "'s serve!", 0, 10, VIRTUAL_WIDTH, 'center')
love.graphics.printf('Press Enter to serve!', 0, 20, VIRTUAL_WIDTH, 'center')
elseif gameState=='play' then
--no messages
elseif gameState=='done' then
love.graphics.setFont(largeFont)
love.graphics.printf('Player ' .. tostring(winningPlayer) .. ' wins!',0, 10, VIRTUAL_WIDTH, 'center')
love.graphics.setFont(smallFont)
love.graphics.printf('Press Enter to restart!', 0, 30, VIRTUAL_WIDTH, 'center')
end
--first paddle
player1:render()
--second paddle
player2:render()
--ball
ball:render()
displayFPS()
push:apply('end')
end

function displayFPS()
love.graphics.setFont(smallFont)
love.graphics.setColor(0,1,0,1)
love.graphics.print('FPS: ' .. tostring(love.timer.getFPS()), 10, 10)
end

function displayScore()
love.graphics.setFont(scoreFont)
love.graphics.print(tostring(player1Score),VIRTUAL_WIDTH/ 2 - 50,VIRTUAL_HEIGHT/3)
love.graphics.print(tostring(player2Score),VIRTUAL_WIDTH/ 2 + 30,VIRTUAL_HEIGHT/3)
end
Loading