SPEAKER: All right, welcome to the CS50 Games Track.
This is the first lecture where we're going
to start taking a look at Pong, the first half of the track.
We have two halves, there's Pong, and there's the Mario track.
Interestingly, Pong was sort of the grandfather to the video games
industry, so to speak, at least the first actor
that ended up Atari was really.
And they created Pong.
And that was responsible for a huge sort of climb
in terms of the market share of that industry.
And unfortunately, Atari was sort of to blame
for the crash of the games industry in the early '80s, them
and several other people.
But thankfully, Mario and the Nintendo Entertainment System
came to save the day in the '80s as well.
And well, the rest is history.
We're here now.
And Mario is iconic, but so is Pong.
And Pong is also very easy and nice and illustrative.
And for that reason, we're going to look at it as our first game example.
And this is what it's going to end up looking like at the end.
We took a look at this earlier.
But again, we have some very basic shapes, text, that sort of thing,
very simple game mechanics.
But before we end up coding anything, we need
to first talk about how we're going to approach tooling, what we're
going to use as our programming language, what
we're going to use as our environment.
And Lua, to get started, will be our programming language.
If you've seen JavaScript yet, it's pretty similar to JavaScript.
And you might not have seen JavaScript yet in the course.
It's kind of similar to Python in some senses as well.
It's not whitespace-sensitive.
But it does have this thing called the table, which
is very similar to a Python dictionary.
And in Lua, the table is literally the only real data structure
you have, aside from things like strings and integers, functions.
But Python has lists, it has sets, it has dictionaries.
Lua just has tables.
And we're going to see that as we get through the games track.
Now, Lua itself was created in 1993.
It was a Brazilian programming language, Portuguese for moon.
And it was originally intended to be used for large commercial applications
so that you didn't have to recompile a code base in order
to change some simple things about it.
If you want to make a small change to something monolithic--
and this is circa 1993--
then you might be looking at hours of recompilation time.
Now, it's not so bad nowadays.
But it still fits very well with the mold of video games,
because game engines are typically compiled to this day in a language like
C or C++.
And making a change to a game engine can take a long time.
There are still hundreds of thousands, if not millions of lines
of code, things even like Unity.
And so recompiling it is better not done.
Better do it instead, sort of expose some of your functionality
to a scripting language that can be manipulated on the fly,
and avoid the problem altogether.
And we'll look at that in the future.
LoVE is sort of an example of this, actually.
LoVE is a 2D game development framework.
But it's written in C++.
And in that sense, it's very similar to a game engine like Unity.
But the difference is that LoVE is very laid back, very simple, non-graphical
in terms of the UI for actually manipulating things.
You're purely expected to use Lua as your way of interacting
with LoVE and source code.
So in addition to just graphics too, it's
important to note that LoVE has things like keyboard input functions,
because that is another thing that we need to take care of, math, audio,
windowing, and even physics.
There's even a Box2D wrapper, Box2D being a very famous 2D physics
library used all over the place, all sorts of engines.
And LoVE has abstracted away their own API
to use Box2D, which is super-convenient.
Nice thing too about LoVE is it's completely free, and it's portable.
So if you write something in LoVE, in Lua on your Mac,
you can export it to a Windows machine or to a Linux machine.
And they can run it immediately.
And it also has ties to Android and iOS as well.
And it's free, so if you want to end up maybe building a game
and selling it on Steam or something, you totally have that option.
Now, you might decide that you want to publish a game on,
let's say, the Nintendo Switch or something,
and they don't have a way to take in LoVE source code.
Well, LoVE is a great prototyping platform
that you can use to get something up and ready
and battle tested, proof of concept, and then end up rewriting it
later down in Unity or something else that is exportable.
But at least test everything in LoVE, because it's
so easy and nice and clean to get up and running, as we'll see very soon.
So the first order of business is going to be installing LoVE.
And if you go to love2d.org/#download and the Getting Started wiki there
in that second link, you'll see a bunch of resources for actually getting it up
and running on your platform.
So let's go ahead and take a look here at the love2d.org web page.
So as you can see, if love2d.org, you have links for Windows machines,
for Mac, for Linux, and resources if you want to get started on Android and iOS,
as well as for older versions of the library.
So choose the version that's most suitable for you.
You can also click this Wiki link here at the very top right.
So if you click this Wiki link, you'll get taken over
to love2d.org/wiki/main_page, where there are resources like Getting
Started, tutorials, resources for learning Lua,
and even a very basic illustration of a program here if you want to get started
with literally the most foundational code possible.
So the Getting Started link in particular
is important, because this gives you instructions
to get LoVE working in a much more easy way for your operating system
than would sort of maybe be the instinctual default.
So here I have a shortcut on my desktop.
Now, if you download LoVE, it'll be an APP file on Mac.
It will be an EXE on Windows.
It'll be probably a .DEB or other Linux-based binary if you're on a Linux
machine.
But normally, what you can do is click and drag a folder
onto the LoVE icon or the LoVE application,
depending on where you are.
And as an illustration, we'll do that very thing.
In order to do that very thing though, we're going to also need a text editor.
Now, I'm pretty partial to a Visual Studio Code, VS Code for short.
And it has a version for all major operating systems.
You can also use Atom.
You can use Sublime Text.
You can use a multitude of text editors out there.
The reason that I like Visual Studio Code
is because it has a really convenient extension plugin, so to speak,
that lets you run LoVE right from the text editor, which is incredibly
useful using just a shortcut, command-L on a Mac,
control-L on a Windows machine.
And I'm going to go ahead and open up VS Code.
So this'll be what it looks like once you have actually downloaded it.
And let's go ahead.
And I'm going to go to my jharvard, dev, games, Pong.
I'm going to create just a test folder in here.
I'm not go ahead, and I'm going to click and drag that test folder onto VS Code.
You can open up folders in VS Code or files.
A folder will take in an entire project, which is useful for the extension
that we're going to talk about.
I'm going to create a new file here in VS Code.
I'm going to call it main.lua.
Now, this is very important. main.lua is expected
to exist in a LoVE application.
This is the Bootstrap file that LoVE is going to look for in order to run.
So I'm going to just type something very simple.
I'm going to type in function love.draw.
And let me just give myself a little bit of room here by full-screening this.
And I'm going to say love.graphics.print hello world, just like that.
And that's almost exactly what's shown on the wiki page.
So now that we have that, I'm going to go back to my desktop.
I'm going to click and drag this folder on to the LoVE app on my desktop,
just like that.
LoVE can't be opened, because it's from an unidentified developer.
That is a Mac security issue.
I'm going to go ahead and fix that.
So let's go down to Security and Privacy, Open Anyway, Open.
And boom.
So that did not end up working the way that I wanted it to.
But I'm going to click this and open that.
And that ended up working the way I wanted it to.
So I think maybe when trying to open it from the uncertified developer,
it ended up opening the default state, which is worth looking at too.
We'll take a look at that again in just a second.
But as you can see here, I do have a window that says, "Hello world."
And this is sort of the very basic easy way to open something.
And this is akin to how you would open almost any other file
in any other program.
But again, also if you just open the binary by itself,
you get this nice little image of a duck balloon so to speak which says,
"No Game," which just means you haven't loaded LoVE with a game file,
as in found a main.lua.
You just opened it sort of emptily.
Now, what I want to do is find a better way to do this probably.
And there are better ways to do this.
If you're on different operating systems that
have different editors and plugins and whatnot,
one way that you can do it is you can alias LoVE at the command prompt,
which I have done.
So if you're on a Mac, it's as simple as writing alias
love equals wherever the location of love
is to what's called your bash profile.
And again, the instructions are right here to do just that.
And there are alternative ways to do this on a Windows machine, as you
can see here, or on a Linux machine.
Actually, Linux is very similar to Mac in that same sense.
Now, even better, if you're on a Windows machine or a Mac
or a graphical version of Linux, you can just
hit Command-L If you have a specific extension like I just did.
And you can see, it opened up the same program just at a keystroke.
Now, if we go over here, if I go to my extension browser and type in Love2D,
you'll see that there is a Love2D support
extension through Pixelbyte Studios.
Highly recommend if you're using VS Code that you download this extension so
that you can run games just by hitting Command-L. It's that simple.
Now, if you're on a Windows machine, I've
noticed that you will have to specify, depending
on where you have placed the EXE, you will
need to tell the settings in VS Code where
to do that, which you can click the Settings thing here.
You should be able to.
Well, actually, you can go to your settings
over in your Preferences, Settings, and then eventually find the Pixelbyte--
where is it?
It is right here, LoVE2D Config.
You'll need to tell the Love 2D Config the Pixelbyte path
right here where you've installed love.exe.
And you actually can see that it assumes that you've installed it
at Program Files, LoVE, love.exe.
So maybe install it there if you haven't already installed it there.
OK, so let's go back to our slide deck here.
We've installed LoVE.
Now a couple of orders of business too in terms of just conceptual thinking
before we approach Pong 0.
The 2D coordinate system is very important.
The 2D coordinate system is essentially just a way
of looking at the screen or your game world.
Both are attached to one another.
And just like you learned in school, you have an x-axis and a y-axis.
And that's sort of how you can position things like lines and elements
on a graph.
In games, it's very similar.
You have characters and lines and other shapes
that you position on a 2D coordinate system plane.
The only difference here is that we're going to be looking at the y-axis
as being inverted from what you might have learned in geometry, where
0 was the origin, and then going down was negative and going up was positive,
it's actually the reverse.
In LoVE and in a lot of game engines, y-axis going down is positive,
going upwards is negative, going left, in this case, is still negative,
and going right is still positive.
But the y-axis is flipped.
And specifically the origin is at the top left side of the screen,
not sort of anywhere in the middle, like you might expect.
So that's the end of this sort of first introductory bit of programming,
of setting up LoVE and Lua, getting it to actually run on your computer.
Again, follow the instructions relevant for your operating system.
In the next video, we're going to look at Pong 0,
where we take a look at the day zero update and actually get something up
and running on the screen more germane to our application.
So we'll see you then in Pong 0.