Yet another shooting game

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
derkonstantin
Newbie
Posts: 10
Joined: Wed Jan 25, 2017 9:30 am
Contact:

Yet another shooting game

#1 Post by derkonstantin »

I need some shooting game. So I look here
viewtopic.php?f=51&t=41347
and here
viewtopic.php?f=51&t=13830#p267179
and create my version of shooting game

Code: Select all

init python:
    message_target = 'OK!! I am waiting!!!'  

    class Clicked(Action): #used when you hit the target

        def __init__(self, object, value):
            self.object = object
            self.value = value
        
        def __call__(self):
            self.object.change_health(self.value)
            renpy.restart_interaction()

        def get_selected(self):
            return False

    class registerClick(Action): #used when you miss
        def __call__(self):
            global message_target
            message_target = 'Shitty shooter'
            renpy.restart_interaction()
        
transform rot(x1, y1): # used for move target on the screen 
    parallel:
        ypos y1
        linear 0.6 ypos renpy.random.random()
        linear 0.6 ypos renpy.random.random()
        linear 0.6 ypos renpy.random.random()
        linear 0.6 ypos renpy.random.random()
        linear 0.6 ypos renpy.random.random()
        linear 0.6 ypos y1
        repeat
    parallel:
        xpos x1
        linear 0.6 xpos renpy.random.random()
        linear 0.6 xpos renpy.random.random()
        linear 0.6 xpos renpy.random.random()
        linear 0.6 xpos renpy.random.random()
        linear 0.6 xpos renpy.random.random()
        linear 0.6 xpos x1
        repeat

        
init -1 python:
    class target: 
        def __init__(self, img, x, y):
            self.img = img
            self.x = x
            self.y = y
            self.sh = True
            self.health = 100

        def change_health(self, value):
            self.health+=value
            self.x = renpy.random.random()
            self.y = renpy.random.random()
            global message_target
            message_target = 'You hit me'
            if self.health <=0:
                self.sh = False

    target_list = []
    target_count = 3
    target_image = '1.png'
    target_max_hp = 0

    def reset_game():
        global target_list
        target_list = []
        i = 0
        while i < target_count:
            target_list.append(target(target_image, renpy.random.random(), renpy.random.random()))
            i+=1

    def is_all_killed(): # win condition
        for i in target_list:
            if i.sh:
                return False
        return True

    def get_all_hp(_list):
        full_hp = 0
        for _target in _list:
            full_hp += _target.health
        return full_hp 

label startGame:
    $ reset_game()
    call screen shooting_game
    return

screen shooting_game:
    if not is_all_killed():
        text message_target size 40 yalign 1.0 xalign 0.5 color '#f00'
    
    bar: #health bar
        range target_max_hp
        value get_all_hp(target_list)
    fixed:
        imagebutton idle "empty.png" hover "empty.png" action registerClick()
        for _target in target_list:
            if _target.sh:
                imagebutton idle _target.img hover _target.img xanchor 0.5 yanchor 0.5 insensitive "empty.png" focus_mask True at rot(_target.x, _target.y)  action If(_target.sh == True, Clicked(_target, -10), None)
        if is_all_killed():
            textbutton "You kill me" xalign 0.5 yalign 0.5 action Return("ok")
to start game use

Code: Select all

call startGame
#TODO:
1) change cursor to aim marker
2) change health bar style
3) add some shake reaction

Post Reply

Who is online

Users browsing this forum: No registered users