GameBox is not currently on market release. Please take everything with a grain of salt.
While GaemBox does create visuals it does so through a lybrary called pygame. In addition to this the tilemaps and many other features require numpy to run fast and clean. To get these features you will need to navigate to the terminal (crt + shift + ` in vs code) and run the following commands. It is also vital that you have python installed and set up as all these packages require it and it is what you will be using to create your fantastic game.
pip install --upgrade pygame
pip install --upgrade numpy
and lastly:
pip install --upgrade GameBox
At this point you should have a basic pygame window.
NOTE: ignore the src. Infront of the GameBox import, this is because the file is in a test folder withing GameBox itself.
A game can be simplified to three cycles: loading, running, and exiting / quitting. The game loop is just the cycle that will run your game. Normally a while loop it will do actions like move the player, update the screen, and get key presses. The game window is like any other window, like google or steam it is what the game displays on.
To start creating you game you need GameBox and pygame.
import pygame
Simple, but there are two ways to import GameBox:
from GameBox import *
This will let you call objects like Player by just saying the name, but may get confusing in the long run with separating what is what.
import GameBox
This will look good long term But you will have to call stuff by using GameBox*dot*object
For this example we will use the first option as I find it better for small projects / games.
Objects are anything like a tilemap, or the player; in python they are classes that will be assigned to variables. GameBox requires a few certain objects to be initialized to run. This includes the Game object which renders the screen and controlled drawing order, And the Camera which being static or dynamic will define where in the game is being rendered. We will also create some variables that store the screens width and height to be used later.
width = 800
height = 600
Now for the Game object, this takes a few parameters such as screen size, background color, and the window title. We will assign this to a variable named game, but feel free to name it whatever you want.
game = Game(width, height)
#optionaly get the screen: pygame.Surface
screen = game.get_screen()
And now the camera. This is a lot simpler than the Game as it uses no parameters.
cam = Camera()
The game loop will need a few variables and uses pygame so lets go over each part. First we create a while loop that runs when variable running is True.
running = True
while running:
Note: from this point on watch your indentation as there will be some nested statements
We now bring in the pygame. The following code will control when the loop ends, for this we will use pygame events to detect when someone presses the 'X' in the top right of the screen
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
running = False
Next we update the game and optionally set the frame rate (default is 60 fps)
game.update(events, fps=60) #add framerate as an integer if you want
This will be outside the loop and run when running is set to False. In the future it will do stuff like save tilemaps after edits.
game.quit()
pygame.quit() #optional but highly recommended
And that's it, run the file and enjoy, its your first GameBox window!
In GameBox the player is a predefined object that aims to make games easier and faster to create. Note that the player is not required for games but is recemented for learning GameBox.
3 lines of code for a player! Great work if your this far!
we will be using GameBox's built in Player object that creates a great starting points for platforming and top down games. The player uses a couple parameters for creation, they are as follows: pos: tuple, dim: tuple, color: str,. For this tutorial the pos will be the middle of the screen, with the dim being 50 by 50.
code this underneath creating the camera
player = Player((width / 2, height / 2), (50, 50) "green")
Next we add physics, this cannot be skipped when creating a player. It will be a lot of parameters including: speed, gravity, jump force, maximum speed, friction. Feel free to play around with the following.
player.add_physics(0, 0, 0, (25, 25), (0.75, 0.75))
moving the player may be useful in many games, GameBox includes some built in options to get you started.
Place this right before your game.update() code.
player.move.by_WSAD(3.75)
At this point you should test the code and see a player moving around, but it can go off srceen. Bad, you don't want to get lost in the world.
This is a simple problem with a couple ways to go about it. We could check the player movement and stop it.. or cheat a little. if we construct Rects around the screen the player will be boxed in, yeah lets do that!
We will create four rects just outside the screen so they won't be shown.
Note: place this code under creating the player
#create world bounds
color = "black"
top = Rect((0, -10), (width, 10), color, False)
left = Rect((-10, 0), (10, height), color, False)
right = Rect((width, 0), (10, height), color, False)
bottom = Rect((0, height), (width, 10), color, False)
Thats it, have fun zipping around the screen!
In the game we're making you will collect 'orbs' to get points, these will be GameBox rects with random positions and colors.
For this we will need the random package, but not the whole thing. We just need one function, so to make it easier we will import just that.
from random import randint as Ri
we will use a python list and fill it in through a for loop.
orbs = []
#fill in list
for _ in range(45) : # this will be the number of orbs
pos = (Ri(0, width), Ri(0, height))
size = Ri(25, 50)
color = (Ri(0, 255), Ri(0, 255), Ri(0, 255))
orbs.append(Rect(pos, (size, size), color, collision=False)) #we don't want to run into the orbs, so no collision
Test it out, colorful orbs just waiting to be collected. Lets do that now, in the game loop above the player movement
for orb in orbs[:]:
if orb.collide(player):
orb.delete() #this will remove it from the screen
orbs.remove(orb)
We will be using text as an ui element, that means that it will drawn on top of everything and will always be where we put it onscreen. (so if you move the camera the text will stat at say 0, 0)
We will create two variables, one to have the GameBox text object and one to keep track of the score. (this will be right after player creation)
score = 0
scoreUI = Text((0, 0), 'Score: 0', pygame.font.SysFont("Arial", 32), "white")
This will be right after removing an orb, still withing the if statement.
score += 1
scoreUI.change(f"Score: {score}")
#optionaly add a little screen shake
cam.shake(3, 2, (0, 0))
And thats it! Great job if you stayed till the end. Go and experiment with the game,. Change the number of orbs, play with the player physics, make the boundary bigger and use the camera to follow the player. The game is yours, all yours.
import pygame
from random import randint as Ri
from GameBox import *
width = 800
height = 600
game = Game(width, height)
screen = game.get_screen()
cam = Cammera()
player = Player(pygame.Vector2(width / 2, height / 2), pygame.Vector2(50, 50), "green")
player.add_physics(0, 0, 0, pygame.Vector2(25, 25), pygame.Vector2(0.75, 0.75))
score = 0
scoreUI = Text(pygame.Vector2(0, 0), 'Score: 0', pygame.font.SysFont("Arial", 32), "white")
#create world bounds
color = "black"
top = Rect(pygame.Vector2(0, -10), pygame.Vector2(width, 10), color, False)
left = Rect(pygame.Vector2(-10, 0), pygame.Vector2(10, height), color, False)
right = Rect(pygame.Vector2(width, 0), pygame.Vector2(10, height), color, False)
bottom = Rect(pygame.Vector2(0, height), pygame.Vector2(width, 10), color, False)
orbs = []
#fill in list
for _ in range(45) : # this will be the number of orbs
pos = pygame.Vector2(Ri(0, width), Ri(0, height))
size = Ri(25, 50)
color = (Ri(0, 255), Ri(0, 255), Ri(0, 255))
orbs.append(Rect(pos, pygame.Vector2(size, size), color, collision=False))
running = True
while running:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
running = False
for orb in orbs[:]:
if orb.collide(player):
orb.delete()
orbs.remove(orb)
score += 1
scoreUI.change(f"Score: {score}")
cam.shake(3, 2, (0, 0))
player.move.by_WSAD(3.75)
game.update(events, fps=60)
game.quit()
pygame.quit()
Done with this tutorial and want to learn more? Check out the many other games demonstrating the power of GameBox and learn new features along the way.