0

I'm starting out with python, I only know the basics and I want to create a top-down type of game to the one I have currently. I've tried some solutions on the internet and sadly, none of them has worked for me.

As I said, I can't find a working solution as of right now but I've tried a few, including this one How can I make the player “look” to the mouse direction? (Pygame / 2D). I'm also using pygame, forgot to mention that. I also have pgzero if you need to know.

This code works and it just places a new actor when you click on it, but I want to have a type of 'character' with a weapon that can move around and 'look' at the mouse.

import pgzrun

from random import randint


apple = Actor("apple")


score = 0


def draw():

    screen.clear()

    apple.draw()

def place_apple():

    apple.x = randint(10, 800)
    apple.y = randint(10, 600)

def on_mouse_down(pos):

    global score
    if apple.collidepoint(pos):
        print("Good Shot!")
        place_apple()
        score = score + 1
        print(score)

    else:
        print("You Missed!")
        quit()

pgzrun.go()

I just need to know how to make the picture 'look' at the cursor and then what each bit does to I know hot to do it for next time.

The first time I tried with the link given above, It returned a positional argument and I can't figure it out. I may have messed something up when I put in the fixed code but I don't know, I was kind of tired.

R Yoda
  • 8,358
  • 2
  • 50
  • 87
  • 1
    Could you share your attempt at implementing the looking algorithm from the link you shared, and what error you got? – Tom Holmes Jun 19 '19 at 23:17
  • *" I want to have a type of 'character' [...] that can move around and 'look' at the mouse."* - what does "look at" mean in this context? Do you've different sprites for looking left, right, up and down? Do you want to rotate the sprite by an certain angle dependent on the direction to the mouse? – Rabbid76 Jun 20 '19 at 06:22

1 Answers1

0

I am not sure what exactly you mean with "aim at the mouse" but I guess you want to change the drawn image according to some program logic.

You do this simply setting the image property of the Actor to another picture, eg. when you click on the image, see my modified version of your

def on_mouse_down(pos):

    global score
    if apple.collidepoint(pos):
        print("Good Shot!")
        place_apple()
        score = score + 1
        apple.image = "cactus" # ADDED: Change the image if your click hit the Actor
        print(score)

    else:
        print("You Missed!")
        quit()

You can find the documentation of the complete API (function calls and properties) here:

https://pygame-zero.readthedocs.io/en/stable/builtins.html

R Yoda
  • 8,358
  • 2
  • 50
  • 87