Below you will find pages that utilize the taxonomy term “PyGame Series”
Basic Movement
Basic Kinematic Animation - Movement first
2D frame based animation (2D kinematic animation) is easier to explain so we will start there.
2D kinematic animation is controlling the movement of an object without physics involved. We simply move in a direction at set intervals. To keep the speed of the motion same on different size screens we need to take time into account. We will make use of the number Frames per second or more accurately the time difference between each frame (delta time) included in our distance calculation. Don’t worry its quite simple. We ‘animate’ our character by swapping the image with another image every few frames or fraction of a second.
Get an image to move
Lets start with an image that won’t change. I’m going to use Kenney’s website for the 2d art to download the car racing bundle. It’s free to use with option to support his efforts. The site and link above is https://kenney.nl/assets/racing-pack
Click the download Link Once downloaded, extract the files from the zip file and inside it is three folders. We want the png folder for our static image. Lets grab a car from the cars folder and place it with our code. I’m going to use the first one in my list “car_yellow_5.png”.
After a while you will start to see the folder and code get messy.
This is part of this tutorial series and I’ll show you one way to clean up the code and folders. For now lets continue to focus on the task at hand.
Getting Started with Pygame
This tutorial is for the beginner, the ‘PyGame Worm’ lessons are intermediate at a rough guess. IDK, those terms were never well defined when I started programming.
We dont teach the absolute basics of python here. You need to have a very basic understanding before you start these tutorials else you will be lost.
The style used here is code as we need, not the best method but a good way to try things out if you keep your changes small. We don’t use tests as that will distract from the lesson, but that will be taught soon.
Python relies heavily on indentation to correctly group blocks of code, but it doesn’t really care about space between lines of code.
Variables are mostly declared during first use by simple assignment.
We don’t go much deeper than that, so if you see a better way of doing things, good, but don’t email me, ok!
Using Python and Pygame to make games
-
download python from python.org and install
select the OS and architecture - desktops/laptops for windows are usually windows-amd64 Let it set environment variables if it asks and also to remove path length limits you likely need to reboot (You can also set up virtual environments but this tutorial doesn’t cover that)
-
Install Pygame
pygame.org gives instructions, though I find the following to work well Go into the folder you want to use for Python development and run terminal or cmd from there. (right click in the folder in windows and select “run terminal in folder”)
type in the following
python -m pip install -U pygame
-
Create a file in that folder and call it game.py. Windows users please enable file name extensions in windows explorer, or it will never work as you will have a name like game.py.txt which won’t work.
-
Using any text editor like Notepad (not WordPad) or an IDE like Visual Studio Code, we can open the file and edit.
From here on we switch between A) Notepad to edit and save our code B) and the terminal to run our code