周末在油管上看到的一个教程,跟着学习了一段时间,后来网络不稳定放弃了,不过可以到github上下载源码,作为游戏入门挺不错的。
Topics
-Lua
-LOVE2D -- 基于lua的游戏引擎
-Drawing Shapes
-Drawing Text
-DeltaTime and Velocity
-Game State
-Basic OOP(Object-Oriented Programming)
-Box Collision(Hitboxes)
-Sound Effects(with bfxr)
Installing love2D
https://love2d.org/#download
https://love2d.org/wiki/Getting_Started
Downloading demo code
https://github.com/games50/pong
What is Lua?
-Portuguese for "moon"; invented in 1993 in Brazil
-Flexible,lightweight scripting language focused around "tables"
-Intended for embedded use in larger applications
-Very popular in the video game industry
-Similar(ish) to JavaScript
-Excellent for storing data as well as code(data-driven design)
What is LOVE2D?
-Fast 2D game development framework written in C++
-Uses Lua as its scripting language
-Contains modules for graphics, keyboard input, math, audio, windowing,
physics and much more
-Completely free and portable to all major desktops and Android/IOS
-Great for prototyping!
What is a game loop?
infinite loop: process input -> update game -> render
2D Coordinate System
System where objects have an X and Y coordinate(X,Y) and are drawn accordingly;
(0,0) would be the top-left of our system, with positive directions moving down
and to the right and negative values moving up and to the left.
Lecture's Scope
-Draw shapes to the screen (paddles 球拍 and ball)
-Control 2D position of paddles based on input
-Collision detection between paddles and ball to deflect ball back toward opponent.
-Collision detection between ball and map boundaries to keep ball within
vetical bounds and to detect score (outside horizontal bounds)
-Sound effects when ball hits paddles/walls or when a point is scored for flavor
-Scorekeeping to determine winner
pong-0目录
main.lua
WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720
function love.load()
love.window.setMode(WINDOW_WIDTH,WINDOW_HEIGHT, {
fullscreen = false,
resizable = false,
vsync = true
})
end
pong-0: Important Functions, p.1
-love.load()
--Used for initializing our game state at the very beginning of program execution.
-love.update(dt)
--called each frame by LOVE; dt will be the elapsed time in seconds since the last frame, and we can use this to scale any changes in our game for even behavior across frame rates.
-love.draw()
--Called each frame by LOVE after update for drawing things to the screen once they've changed.
LOVE2D expects these functions to be implemented in main.lua and calls them internally; if we don't define them, it will still fucntion, but our game will be fundamentally imcomplete, at least if update or draw are missing!
pong-0: Important functions, P.2
-love.graphics.printf(text,x,y, [width], [align])
--versatile 通用的 print function that can align text left, right,or center on the screen.
-love.window.setMode(width, height, params)
--Used to initialize the window's dimensions and to set parameters like vsync(vertical sync), whether we're fullscreen or not, and whether the window is resizable after startup. Won't be using past this example in favor of the push virtual resolution library, which has its own method like this, but useful to know if encoutered in other code.
pong-1: Important Functions
-love.graphics.setDefaultFilter(min,mag)
--Sets the texture scaling filter when minimizing and magnifying 放大 textures and fonts; default is bilinear, 双线性 which causes blurriness, 模糊 and for our use cases we will typically want nearest-neighbor filtering('nearest'), which results in perfect pixel upscaling and downscaling, simulating a retro feel.
-love.keypressed(key)
--A LOVE2D callback function that executes whenever we press a key, assuming we've implemented this in our main.lua, in the same vein as love.load(), love.update(dt), and love.draw().
-love.event.quit()
--Simple function that terminates the application.
Texture Filtering
-Point == Nearest neighbor ('nearest' in our source code)
-Bilinear/trilinear/anisotropic filtering cause blurriness in 2D, as seen below!
pong-2: Important Functions, p.1
-love.graphics.newFont(path, size)
--Loads a font file into memory at a specific path, setting it to a specific size, and storing it in an object we can use to globally change the currently active font that LOVE2d is using to render text (functioning like a state machine).
-love.graphics.setFont(font)
--Sets LOVE2D currently active font (of which there can only be one at a time) to a passed-in font object that we can create using love.graphics.newFont.
-love.graphics.clear(r,g,b,a)
--Wipes the entire screen whith a color defined by an RGBA set, each component of which being from 0-255
pong-2: Important Functions, p.2
--love.graphics.rectangle(mode, x,y,width,height)
--Draws a rectangle onto the screen using whichever our active color is (love.graphics.setColor, which we don't need to use in this particular project since most everyting is white, the default LOVE2D color).
mode can be set to 'fill' or 'line', which result in a filled or outlined rectangle, respectively, and the other four parameters are its position and size dimensions.
This is the cornerstone drawing function of the entirely of oure Pong implementation!
pong-3: Important Functions
-love.keyboard.isDown(key)
--Returs true or false depending on whether the specified key is currently held down;
differs from love.keypressed(key) in that this can be called arbitrarily and will continuously return true if the key is pressed down, where love.keypressed(key) will only fire its code once every time the key is initially pressed down. However, since we want to be able to move our paddles up and down by holding down the appropriate keys, we need a function to test for longer periods of input, hence the use of love.leyboard.isDown(key)!