Pygrr - input (part 1)

Input! What would we do without it? From phone touches, to mouse clicks, and keyboard presses, our interaction with technology would not be possible at all without it. Whether it's a flashy button, or a metallic lever, all computers from time have had to have input. Even biometrics and voice are a form of input! Without it, programs would well... Have to be a video, with no control from the user - not very fun!

So, computers! What do they have?
RAM, CPU, GPU?
No, not like that!
Wires, resistors, transistors?
Now you're being silly - who even are you!?
Oops...
Yes, oops - now shut up!

Computers! What do they have? They have a keyboard, with many keys, and a mouse, with buttons - and - get this - you can move the mouse for a different type of input called 'motion'! That's pretty cool, no?

How does this fit into Pygrr, I hear you ask? Well, let's skip the introductions, you know what a computer is... Right? What do I need for Pygrr input? Mouse clicks, keyboard clicks, and mouse movement... Nice!

Tkinter has built in "event" systems that can be bound to functions - these are called whenever a keyboard button (from here on referred to as a "key") is released or pressed, or a mouse button (from here on referred to as a "button") is released or pressed, or when the mouse is moved (from here on referred to as a "motion").

To log when keys are pressed (i.e. clicked down, not held), all we need to do is put the key's symbol into an array (a list of data), which will be wiped each frame. Why wiped each frame? Otherwise the program will constantly claim that the key was just pressed down, when it should only be the frame that they were pressed in! This is the same as when keys are released (stopped being pressed), just with a different array!

For log when keys are returning (i.e. they are held down), I can just create an array, and when the key is pressed, add the symbol to the array, and, when it is released, remove it from the array. This doesn't need any wiping every frame, as the events handle it all themselves.

Checking the input is pretty simple, I can just create a function, under pygrr.input, to which the user passes the key symbol as input. To decide whether or not the function returns True, all I need to do is check whether the symbol is in the array chosen, or not...

Here's all three key input commands:

if pygrr.input.key_down("space"): # if the key is pressed that frame

if pygrr.input.key_up("space"): # if the key is released that frame

if pygrr.input.key("space"): # if the key is held that frame

We can even make a small game now, with this easy code:

import pygrr


pygrr.create_window()


enemy = pygrr.Object()

enemy.set_model(pygrr.create_model.triangle(10,20))

enemy.set_fillcolor("red")

enemy.pack()


player = pygrr.Object()

player.set_model(pygrr.create_model.circle(5))

player.set_y(100)

player.pack()


while True: # this is the game loop!

    speed = 75 * pygrr.deltatime

        

    if pygrr.input.key("left"):

        player.move_left(speed)

    if pygrr.input.key("right"):

        player.move_right(speed)

    if pygrr.input.key("up"):

        player.move_up(speed)

    if pygrr.input.key("down"):

        player.move_down(speed)

    

    enemy.point_towards(player.get_x(), player.get_y())

    enemy.move_forward(50 * pygrr.deltatime)

    

    pygrr.next_frame()

So far, Pygrr is shaping up to be a huge success, here's the output from that:

Isaac, over and out...

Popular posts from this blog

Messing around with procedural art - Turtle graphics

The fossils of Morocco | Mosasaurus beaugei

Blog post #0 - Hello, world!