Pygrr - input (part 2)

 Now we've done the keyboard, let's move onto the mouse. The clicks are registered in the same method, and queried with these statements:

if pygrr.input.mouse_down("left"): # if the button is pressed that frame

if pygrr.input.mouse_up("left"): # if the button is released that frame

if pygrr.input.mouse("left") # if the button is held that frame

And for the mouse position, we can bind a function with tkinter to return the new position of the mouse. Which makes life incredibly easy... So, yeah! You can use pygrr.input.mouse_position(), mouse_x(), or mouse_y() to get the values... 

Let's make a small game with Pygrr, with our new-found mouse input!

import pygrr, random # add pygrr library and the random library to the project


def round_to_nearest_5(number): # function that rounds the given number to the nearest 5

    return round(number / 5) * 5


pygrr.create_window(width=500, height=400) # create the pygrr window


player = pygrr.Object() # create the player

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

player.pack()


spawn_time = 5 # time for the enemies to spawn in seconds

timer = 0 # this is the number that ticks down


enemies = [] # store all the enemies in this array

while True: # this is the game loop!

    

    timer -= pygrr.deltatime # tick timer down by the time since last frame

    

    if timer <= 0: # if delta is less than or equal to 0


        # create an enemy

        enemy = pygrr.Object(model=pygrr.create_model.triangle(8,15),fill_color="red")

        enemy.set_x(random.randint(-250, 250)) # give the enemy a random x coordinate

        enemy.set_y(random.randint(-200, 200)) # give the enemy a random y coordinate

        enemy.pack()

        

        enemies.append(enemy) # add the enemy to the enemy array

        

        timer = spawn_time  # reset the timer


    for enemy in enemies: # run through all the enemies

        

        enemy.point_towards(player.get_x(), player.get_y()) # point enemy towards the player

        enemy.move_forward(50 * pygrr.deltatime) # move the enemy forwards 50 pixels a second

        

        # if the x coordinates of the mouse and the enemy are the same (rounded to nearest 5)

        if round_to_nearest_5(pygrr.input.mouse_x()) == round_to_nearest_5(enemy.get_x()):

            # if the y coordinates of the mouse and the enemy are the same (rounded to nearest 5) 

            if round_to_nearest_5(pygrr.input.mouse_y()) == round_to_nearest_5(enemy.get_y()):


                # thus, the mouse is over the enemy

                

                enemy.destroy() # remove the enemy from the canvas

                enemies.remove(enemy) # remove the enemy from the array

                spawn_time *= 0.9 # make the timer quicker by reducing the start time

    

    pygrr.next_frame() # rendering

Make sure you bear in mind that the hashtag (#) denotes a code-comment, so doesn't actually mean anything in the code - the point of it is to help people understand it! Here's the code without comments, see if you can still understand it:

import pygrr, random 


def round_to_nearest_5(number): 

    return round(number / 5) * 5


pygrr.create_window(width=500, height=400) 


player = pygrr.Object() 

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

player.pack()


spawn_time = 5 

timer = 0 


enemies = [] 

while True: 

    timer -= pygrr.deltatime 

    

    if timer <= 0: 

        enemy = pygrr.Object(model=pygrr.create_model.triangle(8,15),fill_color="red")

        enemy.set_x(random.randint(-250, 250)) 

        enemy.set_y(random.randint(-200, 200)) 

        enemy.pack()

        enemies.append(enemy)     

        timer = spawn_time  


    for enemy in enemies: 

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

        enemy.move_forward(50 * pygrr.deltatime) 


        if round_to_nearest_5(pygrr.input.mouse_x()) == round_to_nearest_5(enemy.get_x()):

            if round_to_nearest_5(pygrr.input.mouse_y()) == round_to_nearest_5(enemy.get_y()):

                enemy.destroy() 

                enemies.remove(enemy) 

                spawn_time *= 0.9 

    

    pygrr.next_frame()

Okay, I'll stop being cruel now, here's a video!


I promise there was meaning to me swamping you with code... You might have seen how finicky it was to code the "mouse over", and that when the enemies hit the player, nothing happened... 

You guessed it, collision is coming next to a Pygrr near you!

Isaac, over and out...

Popular posts from this blog

Messing around with procedural art - Turtle graphics

Blog post #0 - Hello, world!

The fossils of Morocco | Mosasaurus beaugei