[SOLVED] If an image is in a certain xpos/ ypos, trigger an event

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Kinmoku
Miko-Class Veteran
Posts: 560
Joined: Mon Aug 11, 2014 9:39 am
Completed: One Night Stand
Projects: Love IRL, Memories
Tumblr: gamesbykinmoku
itch: kinmoku
Location: Germany
Contact:

[SOLVED] If an image is in a certain xpos/ ypos, trigger an event

#1 Post by Kinmoku » Tue Mar 12, 2019 10:50 am

Hi all.

I am trying to make a simple "shooter" game. It's basically an imagemap (where you click on the target and hit it), but because using a mouse to do that is super easy and boring, I wanted to use keyboard inputs. The "gun", in this case, is a catapult, so I wanted something that felt more clunky and difficult to use.

So far, I'm happy with the controls I have set up. The image I'm moving about is a crosshair. However, I'm stuck on how, when the target is hit, to register it...

Here's my catapult screen so far (note: I am using Elmiwisa's code from this thread viewtopic.php?f=8&t=23163#top ):

Code: Select all

init -2 python:
    class Coordinate:#this one is to manage the CATAPULT's location on the screen
        def __init__(self,x,y,xmin,ymin,xmax,ymax):
            self.x,self.y,self.xmin,self.ymin,self.xmax,self.ymax=x,y,xmin,ymin,xmax,ymax
            self.xoffset,self.yoffset=0,0
            return
      
        def transform(self,d,show_time,animate_time):
            self.x+=self.xoffset
            self.y+=self.yoffset
            self.xoffset,self.yoffset=0,0
            if self.x<self.xmin:
                self.x=self.xmin
            if self.y<self.ymin:
                self.y=self.ymin
            if self.x>self.xmax:
                self.x=self.xmax
            if self.y>self.ymax:
                self.y=self.ymax
            d.pos=(self.x,self.y)
            return 0
            
    catapult_coordinate=Coordinate(0.5,0.5,0.05,0.05,0.95,0.95)

screen catapult_target:
    add "catapult loc" anchor (0.5,0.5) at Transform(function=catapult_coordinate.transform) # the target image
   
    key "focus_left" action SetField(catapult_coordinate,"xoffset",-0.05) activate_sound "sounds/shuffle.ogg"
    key "focus_right" action SetField(catapult_coordinate,"xoffset",+0.05) activate_sound "sounds/shuffle.ogg"
    key "focus_up" action SetField(catapult_coordinate,"yoffset",-0.05) activate_sound "sounds/shuffle.ogg"
    key "focus_down" action SetField(catapult_coordinate,"yoffset",+0.05) activate_sound "sounds/shuffle.ogg"

    key "dismiss" action Return()
When it returns, it arrives here:

Code: Select all

call screen catapult_target
            
            if catapult_coordinate(0.1, 0.2, 0.1, 0.2):
                #if catapult_coordinate.xpos < 0.2:
                #    if catapult_coordinate.ypos > 0.1:
                #        if catapult_coordinate.ypos < 0.2:
                with vpunch
                "You Hit. "
                $ shots_fired += 1
                $ targets_hit += 1
                if shots_fired >= 3:
                    pass
                    
                else:
                    call attack # goes back to top
                    
            with vpunch
            "You Missed. "
            $ shots_fired += 1
            if shots_fired >= 3:
                pass
            
            else:
                call attack
As you can see, I don't really know what I'm doing! I tried two ways to say "if the crosshair (catapult loc) is within xpos 0.1 - 0.2 and ypos 0.1 - 0.2, then the target is hit", but neither worked. I get this error:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 5144, in script call
    call second_memory
  File "game/memories.rpy", line 7898, in script
    if catapult_coordinate(0.1, 0.2, 0.1, 0.2):
  File "game/memories.rpy", line 7898, in <module>
    if catapult_coordinate(0.1, 0.2, 0.1, 0.2):
TypeError: 'Coordinate' object is not callable

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 5144, in script call
    call second_memory
  File "game/memories.rpy", line 7898, in script
    if catapult_coordinate(0.1, 0.2, 0.1, 0.2):
  File "/Applications/renpy-6.99.11-sdk/renpy/ast.py", line 1762, in execute
    if renpy.python.py_eval(condition):
  File "/Applications/renpy-6.99.11-sdk/renpy/python.py", line 1944, in py_eval
    return py_eval_bytecode(code, globals, locals)
  File "/Applications/renpy-6.99.11-sdk/renpy/python.py", line 1937, in py_eval_bytecode
    return eval(bytecode, globals, locals)
  File "game/memories.rpy", line 7898, in <module>
    if catapult_coordinate(0.1, 0.2, 0.1, 0.2):
TypeError: 'Coordinate' object is not callable

I understand what it's saying but I don't know the solution. When the crosshair is in the right location, how do I get it to say "You Hit"? Any ideas?
Last edited by Kinmoku on Wed Mar 13, 2019 9:32 am, edited 1 time in total.

philat
Eileen-Class Veteran
Posts: 1853
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: If an image is in a certain xpos/ ypos, trigger an event

#2 Post by philat » Tue Mar 12, 2019 8:34 pm

Answering only the immediate question: probably something along the lines of if 0.1<= catapult_coordinate.x <= 0.2 and 0.1<= catapult_coordinate.y <= 0.2

User avatar
Kinmoku
Miko-Class Veteran
Posts: 560
Joined: Mon Aug 11, 2014 9:39 am
Completed: One Night Stand
Projects: Love IRL, Memories
Tumblr: gamesbykinmoku
itch: kinmoku
Location: Germany
Contact:

Re: If an image is in a certain xpos/ ypos, trigger an event

#3 Post by Kinmoku » Wed Mar 13, 2019 9:31 am

philat wrote:
Tue Mar 12, 2019 8:34 pm
Answering only the immediate question: probably something along the lines of if 0.1<= catapult_coordinate.x <= 0.2 and 0.1<= catapult_coordinate.y <= 0.2
That's it! Perfect :D

Post Reply

Who is online

Users browsing this forum: No registered users