top of page
Screenshot 2023-01-08 182557.png

Window

Introduction (20 points)

In this article you will learn the basics which are needed for Level 1.

 - You will learn how to use RG_MainScript's functions.

 - You will write your first windowed program in RGame.

 - You will set your window's title and background color.

Why Before?

In a game you probably want to run different bits of code at different times. For example you might want to have a separate bit of code to set up the game then one that runs while the game is being played. Before is the bit of code that RGame runs before it sets up any graphics or physics. Here is a complete list of every time RGame can run your code:

  • Before: Before Physics and Graphics is setup

  • Main: Before Physics and Graphics starts running

  • PhysicsTick: Every time Physics updates

  • Render: Every time Graphics updates

Here is how to use them:

def Before(self):
        pass

def
Main(self):
        pass

def
PhysicsTick(self, dt):
        pass

def
Render(self):
        pass


main = RG_MainScript(
         
 before = Before,
         
 main = Main,
           
physicsTick = PhysicsTick,
           
render = Render)

Run(main)

 

Here we changed a few things:

 

First take note of how in PhysicsTick you don't just have self but also dt. To explain why, first you need to understand what these are. If you have a group of code and then you name it you may also want it to get some information to use while it is running. The way you do that is by putting the name of that information in the brackets. If you want another bit of information you can just add a comma and give the name of that information. Then when that code is run it will need to be provided with that bit of information. What RGame gives you is the mainscript which you used to run this code under the name 'self '. With physics you might need time for a lot of calculations so dt gives you the amount of time that passed since the last time the physics code was run. We call these names for information variables.

Here instead of directly giving Run the mainscript we first give it the name 'main '. We do this simply because it looks better that way.

When giving MainScript the names of the code we write the name by which MainScript will call it then we use '=' and then we write the name we called it. '=' is what you use to name a bit of information. Here we rename it to what RG_MainScript will call it to make sure it does not mix up one name with the other and running our before code as the main etc.

The way we did it last time is that we relied on the order in which we feed it our information for it to know what we meant. Here before is first; Main is second...

but with this approach we cannot get the order wrong.

from rgame import*

This tells python that you will use rgame and that you will not write out rgame.something when you want to use something from rgame.

So what?

Last time we used the line:

self.Stop = True

To prevent RGame from setting up a window. So surely we do not need to go through all this trouble of naming our code and passing it to a main script to get a window if that is the default? Well yes, that is true. In fact this amount of code would suffice:

Run(RG_MainScript())

That is all great! However if you want to use that window to make a game you will have to be able to give it code to run.

Variables

The concept of variables is one of the if not the most important concept in all of programming and game development. You will have to understand how and when to use them. The basic principle though is this: You store some information somewhere and then you name that place where you store it. It is like a box with a label. You access it using the label and it stores information. 

In RGame self is where MainScript is stored. Mainscript also has other variables stored in it like the main window. The window also has variables like the background color. This is how object orientated programming works and it is what you use to make games because of how accurately you can model reality. You can have a player with a size with a height and width.

Here is an example:

def Main(self):
       
self.MainWindow.WindowBackground = "light blue"
        pass

mainScr = RG_MainScript(main = Main)
Run(mainScr)

Summary

You learnt what is a variable and how to use RG_MainScript to its fullest. If a lot this does not make sense do not worry that is natural. For the moment all you have to know is how to use this and all will be fine.

Task

Let us practice what we learnt with a windowed program:

 - Make a program which prints the time passed for each physics tick (hint: you don't need quotation marks around a variable name)

 - Sets the window background to red

 - And Print hi every time there is a render.

bottom of page