GetJoystick to control a sprite

Game development specific discussions.
Red Rooster
Posts: 5
Joined: May 05, 2018 22:12

GetJoystick to control a sprite

Postby Red Rooster » May 05, 2018 22:29

I recently made a simply game and I use the arrow keys, with Inkey sentence, to control the main sprite of the game. That's ok, but I want to control it by the GamePad's POVs (you know direction keys, not the two sticks).

I try to use the GetJoystick sentence, axis seventh and axis eighth correspond to the GamePad's POVs or direction buttons, but the problem is that when I press once one of the direction buttons, up button for exemple, the sprite moves all to the top of the screen because is like pressing a lot of times this button when I pressed once.

What can I do?

Thanks.
BasicCoder2
Posts: 3194
Joined: Jan 01, 2009 7:03

Re: GetJoystick to control a sprite

Postby BasicCoder2 » May 06, 2018 9:22

Red Rooster wrote:... but the problem is that when I press once one of the direction buttons, up button for example, the sprite moves all to the top of the screen because is like pressing a lot of times this button when I pressed once.

Can you print the relevant code?
This only moves a sprite when a button is pressed.

Code: Select all

Screenres 640,480,32

Dim x As Single
Dim y As Single
Dim shared as integer buttons
Dim result As Integer
Dim a As Integer

Const JoystickID = 0

'This line checks to see if the joystick is ok.

If GetJoystick(JoystickID,buttons,x,y) Then
    Print "Joystick doesn't exist or joystick error."
    Print
    Print "Press any key to continue."
    Sleep
    End
End If

type SPRITE
    as integer x
    as integer y
    as integer w
    as integer h
end type

dim shared as SPRITE s
s.x = 320
s.y = 240
s.w = 50
s.h = 50

sub drawSprite()
    screenlock
    cls
    locate 2,2
    print buttons
    line (s.x,s.y)-(s.x+s.w,s.y+s.h),rgb(255,0,0),bf
    screenunlock
end sub



Do
    drawSprite
    result = GetJoystick(JoystickID,buttons,x,y)
    if buttons = 8 then
        s.y = s.y - 1
        if s.y < 0 then s.y = 0
    end if
    if buttons = 2 then
        s.y = s.y + 1
        if s.y+s.h > 479-s.h then
            s.y = 480-s.h
        end if
    end if
    if buttons = 1 then
        s.x = s.x - 1
        if s.x < 0 then s.x = 0
    end if
    if buttons = 4 then
        s.x = s.x + 1
        if s.x > 640-s.w then
            s.x = 640-s.w
        end if
    end if
   
    sleep 2

Loop until multikey(&H01)

grindstone
Posts: 491
Joined: May 05, 2015 5:35
Location: Germany

Re: GetJoystick to control a sprite

Postby grindstone » May 06, 2018 11:44

You need a repetition delay for the direction buttons. This is the function I use in my own programs to do that:

Code: Select all

Function joysubst () As Integer
   
   Dim As Single joyx, joyy
   Dim As Integer buttons, ret
   Static As Integer lock_ = 0
   Static As Double delay
   
   ret = 0 'default value no key pressed
      
   If GetJoystick (0, buttons, joyx, joyy) Then
      'no joystick
   Else
      If joyx < -.5 Then 'left
        ret = 4
      ElseIf joyx > .5 Then 'right
         ret = 2
      EndIf
      If joyy < -.5 Then 'up
         ret = 1
      ElseIf joyy > .5 Then 'down
         ret = 3
      EndIf
   EndIf
   
   If ret = 0 Then 'no key pressed, set delay to 0
      lock_ = 0
   EndIf
   
   Select Case lock_ 'delay mode
      Case 0 'immediate execution
         If ret Then 'key pressed
            lock_ = 1 'delay mode
           delay = Timer + 0.3 'delay for 1st keystroke
         EndIf
      Case 1 'key is held down
         If Timer > delay Then 'check if delay is over
            lock_ = 2 'repeat mode
            delay = Timer + 0.07 'delay value for repetition rate
         Else 'delay not over yet
            ret = 0
         EndIf
      Case 2 'repetition mode
         If Timer > delay Then 'check if delay is over
            delay = Timer + 0.07 'set value for next delay loop
         Else 'delay not over yet
            ret = 0
         EndIf
   End Select
   
   Return ret
 
End Function

Please adjust the delay values to your own needs.
paul doe
Posts: 539
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: GetJoystick to control a sprite

Postby paul doe » May 06, 2018 11:52

Hi, Red Rooster. Welcome to the forum.

Since I don't have a joystick, I can't test the solutions proposed by BasicCoder2 and grindstone, so I'll show you one that uses the keyboard. The principle is simple enough for you to be able to code it for a joystick: you simply store the state of the previous input, and react differently depending on the new input. This is the relevant code:

Code: Select all

'' This file contains the constants used with multiKey()
#include once "fbgfx.bi"

'' A simple class to help us deal with the keyboard
type keyboard
   public:
      declare function hold( byval as long ) as boolean
      declare function pressed( byval as long ) as boolean
      declare function released( byval as long ) as boolean
      
   private:
      '' These will store the states of the keys
      m_oldKey( 0 to 255 )   as boolean
      m_newKey( 0 to 255 )   as boolean
end type

function keyboard.hold( byval index as long ) as boolean
   '' Returns whether a key is being held
   return( cbool( multiKey( index ) ) )
end function

function keyboard.pressed( byval index as long ) as boolean
   '' Returns whether a key was pressed
   m_oldKey( index ) = m_newKey( index )
   m_newKey( index ) = cbool( multiKey( index ) )
   
   return( m_oldKey( index ) = false andAlso m_newKey( index ) = true )
end function

function keyboard.released( byval index as long ) as boolean
   '' Returns whether a key was released
   m_oldKey( index ) = m_newKey( index )
   m_newKey( index ) = cbool( multiKey( index ) )
   
   return( m_oldKey( index ) = true andAlso m_newKey( index ) = false )
end function

dim as integer screenWidth = 800, screenHeight = 600
screenRes( screenWidth, screenHeight, 32 )

'' The coordinates of the 'sprite' (it is just a white ball)
dim as integer x = 400, y = 300
'' The radius of the sprite
dim as integer radius = 10
'' The speed of the sprite
dim as integer speed = 10

dim as keyboard key

'' Main loop
do
   '' Get some keypresses
   if( key.pressed( fb.sc_up ) = true ) then
      '' Move the sprite if it's still on bounds
      y -= speed
      
      if( y < 0 ) then
         y = 0
      end if
   end if
      
   if( key.pressed( fb.sc_down ) = true ) then
      '' Move the sprite if it's still on bounds
      y += speed
      
      if( y > screenHeight ) then
         y = screenHeight
      end if   
   end if

   if( key.pressed( fb.sc_left ) = true ) then
      '' Move the sprite if it's still on bounds
      x -= speed
      
      if( x < 0 ) then
         x = 0
      end if   
   end if

   if( key.pressed( fb.sc_right ) = true ) then
      '' Move the sprite if it's still on bounds
      x += speed
      
      if( x > screenWidth ) then
         x = screenWidth
      end if      
   end if

   '' Renders the sprite
   screenLock()
      cls()
      
      circle( x, y ), radius, rgb( 255, 255, 255 ), , , , f
   screenUnlock()
   
   sleep( 1, 1 )
loop until( key.pressed( fb.sc_escape ) = true ) '' Loops until escape key is pressed

As you can see, if you change the 'key.pressed()' call in the update loop for a 'key.hold()' call, it will behave as you described, and if you change it to 'key.released()', the sprite will move only when the key has been released. Hope this helps.
Red Rooster
Posts: 5
Joined: May 05, 2018 22:12

Re: GetJoystick to control a sprite

Postby Red Rooster » May 06, 2018 13:15

Here is my original code:

You control a mouse (grey square), try to catch the cheeses(yellow square) and bring it at mouse's home, try to avoid the virus (blue square) because it takes a live and the toxic zones (light green background) because it will take you 1 from health.

Code: Select all

dim shared tiles(7,516) as integer '0.5Bpp*32p*32p+4B=516B
dim shared lmap(20,14) as integer
dim shared as integer i,j,aux
dim shared as integer xraton,yraton,xqueso,yqueso,xvirus,yvirus,mvirus
dim shared as integer vidas,salud,tanteo,conqueso
dim shared as double told
dim shared k as string
dim shared as boolean salir,chequeo
randomize

sub InicializaTiles()
    line (0,0)-(31,31),4,bf
    line (32,0)-(63,31),2,bf
    line (64,0)-(95,31),10,bf
    line (96,0)-(127,31),14,bf
    line (128,0)-(159,31),11,bf
    line (160,0)-(191,31),7,bf
    line (192,0)-(223,15),14,bf
    line (192,16)-(223,31),7,bf
    get (0,0)-(31,31),tiles(0,0)
    get (32,0)-(63,31),tiles(1,0)
    get (64,0)-(95,31),tiles(2,0)
    get (96,0)-(127,31),tiles(3,0)
    get (128,0)-(159,31),tiles(4,0)
    get (160,0)-(191,31),tiles(5,0)
    get (192,0)-(223,31),tiles(6,0)
    cls
end sub

sub InicializaLmap()
    for i=0 to 20-1
        lmap(i,0)=0
        lmap(i,1)=0
        lmap(i,14-1)=0
    next
    for j=2 to 14-2
        for i=1 to 20-2
            lmap(i,j)=1
        next
    next
    xraton=3:yraton=1
    lmap(xraton,yraton)=1
end sub

sub InicializaVariables()
    salir=false
    xvirus=int(rnd*18)+1
    yvirus=int(rnd*11)+2
    chequeo=false
    while chequeo=false
        xqueso=int(rnd*18)+1
        yqueso=int(rnd*11)+2
        if lmap(xqueso,yqueso)=1 then
            lmap(xqueso,yqueso)=3
            chequeo=true
        end if
    wend
    vidas=3:salud=5:tanteo=0:conqueso=0
    told=timer:mvirus=0
end sub

sub InicializaPantalla()
    for j=0 to 14-1
        for i=0 to 20-1
            put(i*32,j*32),tiles(lmap(i,j),0),pset
        next
    next
    put(xraton*32,yraton*32),tiles(5,0),pset
    draw string (10,32*14+12),"Lives:"+str(vidas)+" Health:"+str(salud)+" Score:"+str(tanteo)
    draw string (536,32*14+12),"Cheeses 2018"
end sub

'Main

screenres 640,480,4
InicializaTiles()
InicializaLmap()
InicializaVariables()
InicializaPantalla()

do
    k=inkey
    if k=chr(255)&"H" and lmap(xraton,yraton-1)<>0 then
        if lmap(xraton,yraton-1)=3 then conqueso=1
        put(xraton*32,yraton*32),tiles(1,0),pset:lmap(xraton,yraton)=1
        yraton=yraton-1
        put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
    end if
    if k=chr(255)&"M" and lmap(xraton+1,yraton)<>0 then
        if lmap(xraton+1,yraton)=3 then conqueso=1
        put(xraton*32,yraton*32),tiles(1,0),pset:lmap(xraton,yraton)=1
        xraton=xraton+1
        put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
    end if
    if k=chr(255)&"P" and lmap(xraton,yraton+1)<>0 then
        if lmap(xraton,yraton+1)=3 then conqueso=1
        put(xraton*32,yraton*32),tiles(1,0),pset:lmap(xraton,yraton)=1
        yraton=yraton+1
        put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
    end if
    if k=chr(255)&"K" and lmap(xraton-1,yraton)<>0 then
        if lmap(xraton-1,yraton)=3 then conqueso=1
        put(xraton*32,yraton*32),tiles(1,0),pset:lmap(xraton,yraton)=1
        xraton=xraton-1
        put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
    end if
    if k=chr(27) then
        salir=true
    end if
    if lmap(xraton,yraton)=2 then
        lmap(xraton,yraton)=1:salud=salud-1
        if salud=0 then
            vidas=vidas-1:salud=5
            if conqueso then
                conqueso=0
                chequeo=false
                while chequeo=false
                    xqueso=int(rnd*18)+1
                    yqueso=int(rnd*11)+2
                    if lmap(xqueso,yqueso)=1 then
                    lmap(xqueso,yqueso)=3
                    chequeo=true
                    end if
                wend
                put(xqueso*32,yqueso*32),tiles(3,0),pset
            end if
            put(xraton*32,yraton*32),tiles(1,0),pset
            xraton=3:yraton=1:put(xraton*32,yraton*32),tiles(5,0),pset
        end if
        line (10,32*14+12)-(536,32*14+12+8),0,bf
        draw string (10,32*14+12),"Lives:"+str(vidas)+" Health:"+str(salud)+" Score:"+str(tanteo)
        draw string (536,32*14+12),"Cheeses 2018"
    end if
    if xraton=xvirus and yraton=yvirus then
        vidas=vidas-1:salud=5
        if conqueso then
                conqueso=0
                chequeo=false
                while chequeo=false
                    xqueso=int(rnd*18)+1
                    yqueso=int(rnd*11)+2
                    if lmap(xqueso,yqueso)=1 then
                    lmap(xqueso,yqueso)=3
                    chequeo=true
                    end if
                wend
                put(xqueso*32,yqueso*32),tiles(3,0),pset
            end if
        xraton=3:yraton=1:put(xraton*32,yraton*32),tiles(5,0),pset
        line (10,32*14+12)-(536,32*14+12+8),0,bf
        draw string (10,32*14+12),"Lives:"+str(vidas)+" Health:"+str(salud)+" Score:"+str(tanteo)
        draw string (536,32*14+12),"Cheeses 2018"
    end if
    if xraton=3 and yraton=1 and conqueso then
        conqueso=0:tanteo=tanteo+1
        lmap(xraton,yraton)=5
        put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
        chequeo=false
        while chequeo=false
            xqueso=int(rnd*18)+1
            yqueso=int(rnd*11)+2
            if lmap(xqueso,yqueso)=1 then
                lmap(xqueso,yqueso)=3
                chequeo=true
            end if
        wend
        put(xqueso*32,yqueso*32),tiles(3,0),pset
        line (10,32*14+12)-(536,32*14+12+8),0,bf
        draw string (10,32*14+12),"Lives:"+str(vidas)+" Health:"+str(salud)+" Score:"+str(tanteo)
        draw string (536,32*14+12),"Cheeses 2018"
    end if
    if timer-told>0.25 then
        chequeo=false
        while chequeo=false
            aux=int(rnd*4)
            if aux=0 then
            if lmap(xvirus,yvirus-1)=1 or lmap(xvirus,yvirus-1)=2 then
                put(xvirus*32,yvirus*32),tiles(lmap(xvirus,yvirus),0),pset':lmap(xvirus,yvirus)=1
                yvirus=yvirus-1
                put(xvirus*32,yvirus*32),tiles(4,0),pset
                chequeo=true:mvirus=mvirus+1
            end if
            end if
            if aux=1 then
            if lmap(xvirus+1,yvirus)=1 or lmap(xvirus+1,yvirus)=2 then
                if mvirus<5 then
                    put(xvirus*32,yvirus*32),tiles(lmap(xvirus,yvirus),0),pset':lmap(xvirus,yvirus)=1
                else
                    put(xvirus*32,yvirus*32),tiles(2,0),pset:lmap(xvirus,yvirus)=2
                    mvirus=0
                end if
                xvirus=xvirus+1
                put(xvirus*32,yvirus*32),tiles(4,0),pset
                chequeo=true:mvirus=mvirus+1
            end if
            end if
            if aux=2 then
            if lmap(xvirus,yvirus+1)=1 or lmap(xvirus,yvirus+1)=2 then
                if mvirus<5 then
                    put(xvirus*32,yvirus*32),tiles(lmap(xvirus,yvirus),0),pset':lmap(xvirus,yvirus)=1
                else
                    put(xvirus*32,yvirus*32),tiles(2,0),pset:lmap(xvirus,yvirus)=2
                    mvirus=0
                end if
                yvirus=yvirus+1
                put(xvirus*32,yvirus*32),tiles(4,0),pset
                chequeo=true:mvirus=mvirus+1
            end if
            end if
            if aux=3 then
            if lmap(xvirus-1,yvirus)=1 or lmap(xvirus-1,yvirus)=2 then
                if mvirus<5 then
                    put(xvirus*32,yvirus*32),tiles(lmap(xvirus,yvirus),0),pset':lmap(xvirus,yvirus)=1
                else
                    put(xvirus*32,yvirus*32),tiles(2,0),pset:lmap(xvirus,yvirus)=2
                    mvirus=0
                end if
                xvirus=xvirus-1
                put(xvirus*32,yvirus*32),tiles(4,0),pset
                chequeo=true:mvirus=mvirus+1
            end if
            end if
        wend
        told=timer
    end if
    if vidas=0 then salir=true
loop until salir
sleep



BasicCoder2 wrote:
Red Rooster wrote:... but the problem is that when I press once one of the direction buttons, up button for example, the sprite moves all to the top of the screen because is like pressing a lot of times this button when I pressed once.

Can you print the relevant code?
This only moves a sprite when a button is pressed.

Code: Select all

Screenres 640,480,32

Dim x As Single
Dim y As Single
Dim shared as integer buttons
Dim result As Integer
Dim a As Integer

Const JoystickID = 0

'This line checks to see if the joystick is ok.

If GetJoystick(JoystickID,buttons,x,y) Then
    Print "Joystick doesn't exist or joystick error."
    Print
    Print "Press any key to continue."
    Sleep
    End
End If

type SPRITE
    as integer x
    as integer y
    as integer w
    as integer h
end type

dim shared as SPRITE s
s.x = 320
s.y = 240
s.w = 50
s.h = 50

sub drawSprite()
    screenlock
    cls
    locate 2,2
    print buttons
    line (s.x,s.y)-(s.x+s.w,s.y+s.h),rgb(255,0,0),bf
    screenunlock
end sub



Do
    drawSprite
    result = GetJoystick(JoystickID,buttons,x,y)
    if buttons = 8 then
        s.y = s.y - 1
        if s.y < 0 then s.y = 0
    end if
    if buttons = 2 then
        s.y = s.y + 1
        if s.y+s.h > 479-s.h then
            s.y = 480-s.h
        end if
    end if
    if buttons = 1 then
        s.x = s.x - 1
        if s.x < 0 then s.x = 0
    end if
    if buttons = 4 then
        s.x = s.x + 1
        if s.x > 640-s.w then
            s.x = 640-s.w
        end if
    end if
   
    sleep 2

Loop until multikey(&H01)

paul doe
Posts: 539
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: GetJoystick to control a sprite

Postby paul doe » May 06, 2018 13:23

@Red Rooster:
Enclose code between [code] and [/code] tags, to be easily copied and pasted for quick run. If you click on the 'Code' button when you write the post, the forum inserts those tags for you. Simply copy the code between those and it looks like this:

[code]
'' Put your code here
[/code]

The code runs fine here, although I can't test it fully (don't have a joystick). The solutions proposed so far didn't help you?

PS: Variables en español? Hacía mucho que no veía código así =D
Red Rooster
Posts: 5
Joined: May 05, 2018 22:12

Re: GetJoystick to control a sprite

Postby Red Rooster » May 06, 2018 13:38

Thanks paul doe, Not at all, I did a modificated code with BasicCoder2 idea, using buttons return of GetJoystick and adding a sleep time at the end of the gameloop (not needed if you use keyboard arrow keys as you notice in the previous version) that Works more or less with stick2 and right fire buttons but not with POVs in the gamepad.

This is the result:

Code: Select all

dim shared tiles(7,516) as integer '0.5Bpp*32p*32p+4B=516B
dim shared lmap(20,14) as integer
dim shared as integer i,j,aux
dim shared as integer xraton,yraton,xqueso,yqueso,xvirus,yvirus,mvirus
dim shared as integer vidas,salud,tanteo,conqueso,buttons
dim shared as double told
dim shared k as string
dim shared as boolean salir,chequeo
dim shared as single joyx,joyy
randomize

sub InicializaTiles()
    line (0,0)-(31,31),4,bf
    line (32,0)-(63,31),2,bf
    line (64,0)-(95,31),10,bf
    line (96,0)-(127,31),14,bf
    line (128,0)-(159,31),11,bf
    line (160,0)-(191,31),7,bf
    line (192,0)-(223,15),14,bf
    line (192,16)-(223,31),7,bf
    get (0,0)-(31,31),tiles(0,0)
    get (32,0)-(63,31),tiles(1,0)
    get (64,0)-(95,31),tiles(2,0)
    get (96,0)-(127,31),tiles(3,0)
    get (128,0)-(159,31),tiles(4,0)
    get (160,0)-(191,31),tiles(5,0)
    get (192,0)-(223,31),tiles(6,0)
    cls
end sub

sub InicializaLmap()
    for i=0 to 20-1
        lmap(i,0)=0
        lmap(i,1)=0
        lmap(i,14-1)=0
    next
    for j=2 to 14-2
        for i=1 to 20-2
            lmap(i,j)=1
        next
    next
    xraton=3:yraton=1
    lmap(xraton,yraton)=1
end sub

sub InicializaVariables()
    salir=false
    xvirus=int(rnd*18)+1
    yvirus=int(rnd*11)+2
    chequeo=false
    while chequeo=false
        xqueso=int(rnd*18)+1
        yqueso=int(rnd*11)+2
        if lmap(xqueso,yqueso)=1 then
            lmap(xqueso,yqueso)=3
            chequeo=true
        end if
    wend
    vidas=3:salud=5:tanteo=0:conqueso=0
    told=timer:mvirus=0
end sub

sub InicializaPantalla()
    for j=0 to 14-1
        for i=0 to 20-1
            put(i*32,j*32),tiles(lmap(i,j),0),pset
        next
    next
    put(xraton*32,yraton*32),tiles(5,0),pset
    draw string (10,32*14+12),"Lives:"+str(vidas)+" Health:"+str(salud)+" Score:"+str(tanteo)
    draw string (536,32*14+12),"Cheeses 2018"
end sub

'Main

screenres 640,480,4
InicializaTiles()
InicializaLmap()
InicializaVariables()
InicializaPantalla()

do
    k=inkey:getjoystick (0, buttons, joyx, joyy)
    if (k=chr(255)&"H" or buttons=1 )and lmap(xraton,yraton-1)<>0 then
        if lmap(xraton,yraton-1)=3 then conqueso=1
        put(xraton*32,yraton*32),tiles(1,0),pset:lmap(xraton,yraton)=1
        yraton=yraton-1
        put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
    end if
    if (k=chr(255)&"M" or buttons=2) and lmap(xraton+1,yraton)<>0 then
        if lmap(xraton+1,yraton)=3 then conqueso=1
        put(xraton*32,yraton*32),tiles(1,0),pset:lmap(xraton,yraton)=1
        xraton=xraton+1
        put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
    end if
    if (k=chr(255)&"P" or buttons=4) and lmap(xraton,yraton+1)<>0 then
        if lmap(xraton,yraton+1)=3 then conqueso=1
        put(xraton*32,yraton*32),tiles(1,0),pset:lmap(xraton,yraton)=1
        yraton=yraton+1
        put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
    end if
    if (k=chr(255)&"K" or buttons=8) and lmap(xraton-1,yraton)<>0 then
        if lmap(xraton-1,yraton)=3 then conqueso=1
        put(xraton*32,yraton*32),tiles(1,0),pset:lmap(xraton,yraton)=1
        xraton=xraton-1
        put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
    end if
    if k=chr(27) then
        salir=true
    end if
    if lmap(xraton,yraton)=2 then
        lmap(xraton,yraton)=1:salud=salud-1
        if salud=0 then
            vidas=vidas-1:salud=5
            if conqueso then
                conqueso=0
                chequeo=false
                while chequeo=false
                    xqueso=int(rnd*18)+1
                    yqueso=int(rnd*11)+2
                    if lmap(xqueso,yqueso)=1 then
                    lmap(xqueso,yqueso)=3
                    chequeo=true
                    end if
                wend
                put(xqueso*32,yqueso*32),tiles(3,0),pset
            end if
            put(xraton*32,yraton*32),tiles(1,0),pset
            xraton=3:yraton=1:put(xraton*32,yraton*32),tiles(5,0),pset
        end if
        line (10,32*14+12)-(536,32*14+12+8),0,bf
        draw string (10,32*14+12),"Lives:"+str(vidas)+" Health:"+str(salud)+" Score:"+str(tanteo)
        draw string (536,32*14+12),"Cheeses 2018"
    end if
    if xraton=xvirus and yraton=yvirus then
        vidas=vidas-1:salud=5
        if conqueso then
                conqueso=0
                chequeo=false
                while chequeo=false
                    xqueso=int(rnd*18)+1
                    yqueso=int(rnd*11)+2
                    if lmap(xqueso,yqueso)=1 then
                    lmap(xqueso,yqueso)=3
                    chequeo=true
                    end if
                wend
                put(xqueso*32,yqueso*32),tiles(3,0),pset
            end if
        xraton=3:yraton=1:put(xraton*32,yraton*32),tiles(5,0),pset
        line (10,32*14+12)-(536,32*14+12+8),0,bf
        draw string (10,32*14+12),"Lives:"+str(vidas)+" Health:"+str(salud)+" Score:"+str(tanteo)
        draw string (536,32*14+12),"Cheeses 2018"
    end if
    if xraton=3 and yraton=1 and conqueso then
        conqueso=0:tanteo=tanteo+1
        lmap(xraton,yraton)=5
        put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
        chequeo=false
        while chequeo=false
            xqueso=int(rnd*18)+1
            yqueso=int(rnd*11)+2
            if lmap(xqueso,yqueso)=1 then
                lmap(xqueso,yqueso)=3
                chequeo=true
            end if
        wend
        put(xqueso*32,yqueso*32),tiles(3,0),pset
        line (10,32*14+12)-(536,32*14+12+8),0,bf
        draw string (10,32*14+12),"Lives:"+str(vidas)+" Health:"+str(salud)+" Score:"+str(tanteo)
        draw string (536,32*14+12),"Cheeses 2018"
    end if
    if timer-told>0.25 then
        chequeo=false
        while chequeo=false
            aux=int(rnd*4)
            if aux=0 then
            if lmap(xvirus,yvirus-1)=1 or lmap(xvirus,yvirus-1)=2 then
                put(xvirus*32,yvirus*32),tiles(lmap(xvirus,yvirus),0),pset':lmap(xvirus,yvirus)=1
                yvirus=yvirus-1
                put(xvirus*32,yvirus*32),tiles(4,0),pset
                chequeo=true:mvirus=mvirus+1
            end if
            end if
            if aux=1 then
            if lmap(xvirus+1,yvirus)=1 or lmap(xvirus+1,yvirus)=2 then
                if mvirus<5 then
                    put(xvirus*32,yvirus*32),tiles(lmap(xvirus,yvirus),0),pset':lmap(xvirus,yvirus)=1
                else
                    put(xvirus*32,yvirus*32),tiles(2,0),pset:lmap(xvirus,yvirus)=2
                    mvirus=0
                end if
                xvirus=xvirus+1
                put(xvirus*32,yvirus*32),tiles(4,0),pset
                chequeo=true:mvirus=mvirus+1
            end if
            end if
            if aux=2 then
            if lmap(xvirus,yvirus+1)=1 or lmap(xvirus,yvirus+1)=2 then
                if mvirus<5 then
                    put(xvirus*32,yvirus*32),tiles(lmap(xvirus,yvirus),0),pset':lmap(xvirus,yvirus)=1
                else
                    put(xvirus*32,yvirus*32),tiles(2,0),pset:lmap(xvirus,yvirus)=2
                    mvirus=0
                end if
                yvirus=yvirus+1
                put(xvirus*32,yvirus*32),tiles(4,0),pset
                chequeo=true:mvirus=mvirus+1
            end if
            end if
            if aux=3 then
            if lmap(xvirus-1,yvirus)=1 or lmap(xvirus-1,yvirus)=2 then
                if mvirus<5 then
                    put(xvirus*32,yvirus*32),tiles(lmap(xvirus,yvirus),0),pset':lmap(xvirus,yvirus)=1
                else
                    put(xvirus*32,yvirus*32),tiles(2,0),pset:lmap(xvirus,yvirus)=2
                    mvirus=0
                end if
                xvirus=xvirus-1
                put(xvirus*32,yvirus*32),tiles(4,0),pset
                chequeo=true:mvirus=mvirus+1
            end if
            end if
        wend
        told=timer
    end if
    if vidas=0 then salir=true
    sleep 120
loop until salir
sleep


paul doe wrote:@Red Rooster:
Enclose code between

Code: Select all

 and
tags, to be easily copied and pasted for quick run. If you click on the 'Code' button when you write the post, the forum inserts those tags for you. Simply copy the code between those and it looks like this:

Code: Select all

'' Put your code here


The code runs fine here, although I can't test it fully (don't have a joystick). The solutions proposed so far didn't help you?

PS: Variables en español? Hacía mucho que no veía código así =D
paul doe
Posts: 539
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: GetJoystick to control a sprite

Postby paul doe » May 06, 2018 13:54

I took the liberty to add the keyboard handler that I showed you to your previous post:

Code: Select all

'' This file contains the constants used with multiKey()
#include once "fbgfx.bi"

'' A simple class to help us deal with the keyboard
type keyboard
   public:
      declare function hold( byval as long ) as boolean
      declare function pressed( byval as long ) as boolean
      declare function released( byval as long ) as boolean
      
   private:
      '' These will store the states of the keys
      m_oldKey( 0 to 255 )   as boolean
      m_newKey( 0 to 255 )   as boolean
end type

function keyboard.hold( byval index as long ) as boolean
   '' Returns whether a key is being held
   return( cbool( multiKey( index ) ) )
end function

function keyboard.pressed( byval index as long ) as boolean
   '' Returns whether a key was pressed
   m_oldKey( index ) = m_newKey( index )
   m_newKey( index ) = cbool( multiKey( index ) )
   
   return( m_oldKey( index ) = false andAlso m_newKey( index ) = true )
end function

function keyboard.released( byval index as long ) as boolean
   '' Returns whether a key was released
   m_oldKey( index ) = m_newKey( index )
   m_newKey( index ) = cbool( multiKey( index ) )
   
   return( m_oldKey( index ) = true andAlso m_newKey( index ) = false )
end function

/'
   Original code starts here
'/
dim shared tiles(7,516) as integer '0.5Bpp*32p*32p+4B=516B
dim shared lmap(20,14) as integer
dim shared as integer i,j,aux
dim shared as integer xraton,yraton,xqueso,yqueso,xvirus,yvirus,mvirus
dim shared as integer vidas,salud,tanteo,conqueso
dim shared as double told
dim shared k as string
dim shared as boolean salir,chequeo
randomize

sub InicializaTiles()
   line (0,0)-(31,31),4,bf
   line (32,0)-(63,31),2,bf
   line (64,0)-(95,31),10,bf
   line (96,0)-(127,31),14,bf
   line (128,0)-(159,31),11,bf
   line (160,0)-(191,31),7,bf
   line (192,0)-(223,15),14,bf
   line (192,16)-(223,31),7,bf
   get (0,0)-(31,31),tiles(0,0)
   get (32,0)-(63,31),tiles(1,0)
   get (64,0)-(95,31),tiles(2,0)
   get (96,0)-(127,31),tiles(3,0)
   get (128,0)-(159,31),tiles(4,0)
   get (160,0)-(191,31),tiles(5,0)
   get (192,0)-(223,31),tiles(6,0)
   cls
end sub

sub InicializaLmap()
   for i=0 to 20-1
      lmap(i,0)=0
      lmap(i,1)=0
      lmap(i,14-1)=0
   next
   
   for j=2 to 14-2
      for i=1 to 20-2
         lmap(i,j)=1
      next
   next
   
   xraton=3:yraton=1
   lmap(xraton,yraton)=1
end sub

sub InicializaVariables()
   salir=false
   xvirus=int(rnd*18)+1
   yvirus=int(rnd*11)+2
   chequeo=false
   
   while chequeo=false
      xqueso=int(rnd*18)+1
      yqueso=int(rnd*11)+2
   
      if lmap(xqueso,yqueso)=1 then
         lmap(xqueso,yqueso)=3
         chequeo=true
      end if
   wend
   
   vidas=3:salud=5:tanteo=0:conqueso=0
   told=timer:mvirus=0
end sub

sub InicializaPantalla()
   for j=0 to 14-1
      for i=0 to 20-1
         put(i*32,j*32),tiles(lmap(i,j),0),pset
      next
   next
   
   put(xraton*32,yraton*32),tiles(5,0),pset
   
   draw string (10,32*14+12),"Lives:"+str(vidas)+" Health:"+str(salud)+" Score:"+str(tanteo)
   draw string (536,32*14+12),"Cheeses 2018"
end sub

'Main

screenres 640,480,4

InicializaTiles()
InicializaLmap()
InicializaVariables()
InicializaPantalla()

dim as keyboard key

do
   'k=inkey
   
   'if k=chr(255)&"H" and lmap(xraton,yraton-1)<>0 then
   '' Up
   if( key.pressed( fb.sc_up ) = true andAlso cbool( lmap(xraton,yraton-1)<>0 ) )then
      if lmap(xraton,yraton-1)=3 then conqueso=1
      put(xraton*32,yraton*32),tiles(1,0),pset:lmap(xraton,yraton)=1
      yraton=yraton-1
      put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
   end if
   
   '' Right
   'if k=chr(255)&"M" and lmap(xraton+1,yraton)<>0 then
   if( key.pressed( fb.sc_right ) = true andAlso cbool( lmap(xraton+1,yraton)<>0 ) ) then
      if lmap(xraton+1,yraton)=3 then conqueso=1
      put(xraton*32,yraton*32),tiles(1,0),pset:lmap(xraton,yraton)=1
      xraton=xraton+1
      put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
   end if
   
   '' Down
   'if k=chr(255)&"P" and lmap(xraton,yraton+1)<>0 then
   if( key.pressed( fb.sc_down ) = true andAlso cbool( lmap(xraton,yraton+1)<>0 ) ) then
      if lmap(xraton,yraton+1)=3 then conqueso=1
      put(xraton*32,yraton*32),tiles(1,0),pset:lmap(xraton,yraton)=1
      yraton=yraton+1
      put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
   end if
   
   '' Left
   'if k=chr(255)&"K" and lmap(xraton-1,yraton)<>0 then
   if( key.pressed( fb.sc_left ) = true andAlso cbool( lmap(xraton-1,yraton)<>0 ) )then
      if lmap(xraton-1,yraton)=3 then conqueso=1
      put(xraton*32,yraton*32),tiles(1,0),pset:lmap(xraton,yraton)=1
      xraton=xraton-1
      put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
   end if
   
   '' Esc
   if( key.pressed( fb.sc_escape ) = true ) then
      salir=true
   end if
   
   if lmap(xraton,yraton)=2 then
      lmap(xraton,yraton)=1:salud=salud-1
      
      if salud=0 then
         vidas=vidas-1:salud=5
      
         if conqueso then
            conqueso=0
            chequeo=false
      
            while chequeo=false
               xqueso=int(rnd*18)+1
               yqueso=int(rnd*11)+2
               
               if lmap(xqueso,yqueso)=1 then
                  lmap(xqueso,yqueso)=3
                  chequeo=true
               end if
            wend
      
            put(xqueso*32,yqueso*32),tiles(3,0),pset
         end if
   
         put(xraton*32,yraton*32),tiles(1,0),pset
         xraton=3:yraton=1:put(xraton*32,yraton*32),tiles(5,0),pset
      end if
      
      line (10,32*14+12)-(536,32*14+12+8),0,bf
      draw string (10,32*14+12),"Lives:"+str(vidas)+" Health:"+str(salud)+" Score:"+str(tanteo)
      draw string (536,32*14+12),"Cheeses 2018"
   end if
   
   if xraton=xvirus and yraton=yvirus then
      vidas=vidas-1:salud=5
      
      if conqueso then
         conqueso=0
         chequeo=false
         
         while chequeo=false
            xqueso=int(rnd*18)+1
            yqueso=int(rnd*11)+2
            
            if lmap(xqueso,yqueso)=1 then
               lmap(xqueso,yqueso)=3
               chequeo=true
            end if
         wend
         
         put(xqueso*32,yqueso*32),tiles(3,0),pset
      end if
   
      xraton=3:yraton=1:put(xraton*32,yraton*32),tiles(5,0),pset
      line (10,32*14+12)-(536,32*14+12+8),0,bf
      draw string (10,32*14+12),"Lives:"+str(vidas)+" Health:"+str(salud)+" Score:"+str(tanteo)
      draw string (536,32*14+12),"Cheeses 2018"
   end if
   
   if xraton=3 and yraton=1 and conqueso then
      conqueso=0:tanteo=tanteo+1
      lmap(xraton,yraton)=5
      put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
      chequeo=false
   
      while chequeo=false
         xqueso=int(rnd*18)+1
         yqueso=int(rnd*11)+2
   
         if lmap(xqueso,yqueso)=1 then
            lmap(xqueso,yqueso)=3
            chequeo=true
         end if
      wend
      
      put(xqueso*32,yqueso*32),tiles(3,0),pset
      line (10,32*14+12)-(536,32*14+12+8),0,bf
      draw string (10,32*14+12),"Lives:"+str(vidas)+" Health:"+str(salud)+" Score:"+str(tanteo)
      draw string (536,32*14+12),"Cheeses 2018"
   end if
   
   if timer-told>0.25 then
      chequeo=false
      
      while chequeo=false
         aux=int(rnd*4)
         
         if aux=0 then
            if lmap(xvirus,yvirus-1)=1 or lmap(xvirus,yvirus-1)=2 then
               put(xvirus*32,yvirus*32),tiles(lmap(xvirus,yvirus),0),pset':lmap(xvirus,yvirus)=1
               yvirus=yvirus-1
               put(xvirus*32,yvirus*32),tiles(4,0),pset
               chequeo=true:mvirus=mvirus+1
            end if
         end if
            
         if aux=1 then
            if lmap(xvirus+1,yvirus)=1 or lmap(xvirus+1,yvirus)=2 then
               if mvirus<5 then
                  put(xvirus*32,yvirus*32),tiles(lmap(xvirus,yvirus),0),pset':lmap(xvirus,yvirus)=1
               else
                  put(xvirus*32,yvirus*32),tiles(2,0),pset:lmap(xvirus,yvirus)=2
                  mvirus=0
               end if
               
               xvirus=xvirus+1
               put(xvirus*32,yvirus*32),tiles(4,0),pset
               chequeo=true:mvirus=mvirus+1
            end if
         end if
      
         if aux=2 then
            if lmap(xvirus,yvirus+1)=1 or lmap(xvirus,yvirus+1)=2 then
               if mvirus<5 then
                  put(xvirus*32,yvirus*32),tiles(lmap(xvirus,yvirus),0),pset':lmap(xvirus,yvirus)=1
               else
                  put(xvirus*32,yvirus*32),tiles(2,0),pset:lmap(xvirus,yvirus)=2
                  mvirus=0
               end if
               
               yvirus=yvirus+1
               put(xvirus*32,yvirus*32),tiles(4,0),pset
               chequeo=true:mvirus=mvirus+1
            end if
         end if
      
         if aux=3 then
            if lmap(xvirus-1,yvirus)=1 or lmap(xvirus-1,yvirus)=2 then
               if mvirus<5 then
                  put(xvirus*32,yvirus*32),tiles(lmap(xvirus,yvirus),0),pset':lmap(xvirus,yvirus)=1
               else
                  put(xvirus*32,yvirus*32),tiles(2,0),pset:lmap(xvirus,yvirus)=2
                  mvirus=0
               end if
               
               xvirus=xvirus-1
               put(xvirus*32,yvirus*32),tiles(4,0),pset
               chequeo=true:mvirus=mvirus+1
            end if
         end if
      wend
      
      told=timer
   end if
   
   if vidas=0 then salir=true
   
   sleep( 1, 1 )
loop until salir

'sleep

This is what you want, but with the joystick buttons, right? It shouldn't be too difficult for you to implement that to work with a joystick, or to use grindstone's solution...

Red Rooster wrote:...adding a sleep time at the end of the gameloop (not needed if you use keyboard arrow keys as you notice in the previous version)...

The 'sleep( 1, 1 )' call inside the main loop is mandatory if you don't want the code to hog all cpu resources, and to keep your app running smoothly. Always include it inside some endless loop when you're running under a multitasking OS.
Red Rooster
Posts: 5
Joined: May 05, 2018 22:12

Re: GetJoystick to control a sprite

Postby Red Rooster » May 06, 2018 16:35

Perfect, many thanks grindstone, that's exactly what I need, goes well for directional pad and stick1 at the same time. Thanks again.

grindstone wrote:You need a repetition delay for the direction buttons. This is the function I use in my own programs to do that:

Code: Select all

Function joysubst () As Integer
   
   Dim As Single joyx, joyy
   Dim As Integer buttons, ret
   Static As Integer lock_ = 0
   Static As Double delay
   
   ret = 0 'default value no key pressed
      
   If GetJoystick (0, buttons, joyx, joyy) Then
      'no joystick
   Else
      If joyx < -.5 Then 'left
        ret = 4
      ElseIf joyx > .5 Then 'right
         ret = 2
      EndIf
      If joyy < -.5 Then 'up
         ret = 1
      ElseIf joyy > .5 Then 'down
         ret = 3
      EndIf
   EndIf
   
   If ret = 0 Then 'no key pressed, set delay to 0
      lock_ = 0
   EndIf
   
   Select Case lock_ 'delay mode
      Case 0 'immediate execution
         If ret Then 'key pressed
            lock_ = 1 'delay mode
           delay = Timer + 0.3 'delay for 1st keystroke
         EndIf
      Case 1 'key is held down
         If Timer > delay Then 'check if delay is over
            lock_ = 2 'repeat mode
            delay = Timer + 0.07 'delay value for repetition rate
         Else 'delay not over yet
            ret = 0
         EndIf
      Case 2 'repetition mode
         If Timer > delay Then 'check if delay is over
            delay = Timer + 0.07 'set value for next delay loop
         Else 'delay not over yet
            ret = 0
         EndIf
   End Select
   
   Return ret
 
End Function

Please adjust the delay values to your own needs.
Red Rooster
Posts: 5
Joined: May 05, 2018 22:12

Re: GetJoystick to control a sprite

Postby Red Rooster » May 06, 2018 16:38

Thanks paul doe, I will use this keyboard handler sometime for sure. For this finally I decided to include grindstone's solution, that's exactly what I need. So many thanks, un saludo.

paul doe wrote:I took the liberty to add the keyboard handler that I showed you to your previous post:

Code: Select all

'' This file contains the constants used with multiKey()
#include once "fbgfx.bi"

'' A simple class to help us deal with the keyboard
type keyboard
   public:
      declare function hold( byval as long ) as boolean
      declare function pressed( byval as long ) as boolean
      declare function released( byval as long ) as boolean
      
   private:
      '' These will store the states of the keys
      m_oldKey( 0 to 255 )   as boolean
      m_newKey( 0 to 255 )   as boolean
end type

function keyboard.hold( byval index as long ) as boolean
   '' Returns whether a key is being held
   return( cbool( multiKey( index ) ) )
end function

function keyboard.pressed( byval index as long ) as boolean
   '' Returns whether a key was pressed
   m_oldKey( index ) = m_newKey( index )
   m_newKey( index ) = cbool( multiKey( index ) )
   
   return( m_oldKey( index ) = false andAlso m_newKey( index ) = true )
end function

function keyboard.released( byval index as long ) as boolean
   '' Returns whether a key was released
   m_oldKey( index ) = m_newKey( index )
   m_newKey( index ) = cbool( multiKey( index ) )
   
   return( m_oldKey( index ) = true andAlso m_newKey( index ) = false )
end function

/'
   Original code starts here
'/
dim shared tiles(7,516) as integer '0.5Bpp*32p*32p+4B=516B
dim shared lmap(20,14) as integer
dim shared as integer i,j,aux
dim shared as integer xraton,yraton,xqueso,yqueso,xvirus,yvirus,mvirus
dim shared as integer vidas,salud,tanteo,conqueso
dim shared as double told
dim shared k as string
dim shared as boolean salir,chequeo
randomize

sub InicializaTiles()
   line (0,0)-(31,31),4,bf
   line (32,0)-(63,31),2,bf
   line (64,0)-(95,31),10,bf
   line (96,0)-(127,31),14,bf
   line (128,0)-(159,31),11,bf
   line (160,0)-(191,31),7,bf
   line (192,0)-(223,15),14,bf
   line (192,16)-(223,31),7,bf
   get (0,0)-(31,31),tiles(0,0)
   get (32,0)-(63,31),tiles(1,0)
   get (64,0)-(95,31),tiles(2,0)
   get (96,0)-(127,31),tiles(3,0)
   get (128,0)-(159,31),tiles(4,0)
   get (160,0)-(191,31),tiles(5,0)
   get (192,0)-(223,31),tiles(6,0)
   cls
end sub

sub InicializaLmap()
   for i=0 to 20-1
      lmap(i,0)=0
      lmap(i,1)=0
      lmap(i,14-1)=0
   next
   
   for j=2 to 14-2
      for i=1 to 20-2
         lmap(i,j)=1
      next
   next
   
   xraton=3:yraton=1
   lmap(xraton,yraton)=1
end sub

sub InicializaVariables()
   salir=false
   xvirus=int(rnd*18)+1
   yvirus=int(rnd*11)+2
   chequeo=false
   
   while chequeo=false
      xqueso=int(rnd*18)+1
      yqueso=int(rnd*11)+2
   
      if lmap(xqueso,yqueso)=1 then
         lmap(xqueso,yqueso)=3
         chequeo=true
      end if
   wend
   
   vidas=3:salud=5:tanteo=0:conqueso=0
   told=timer:mvirus=0
end sub

sub InicializaPantalla()
   for j=0 to 14-1
      for i=0 to 20-1
         put(i*32,j*32),tiles(lmap(i,j),0),pset
      next
   next
   
   put(xraton*32,yraton*32),tiles(5,0),pset
   
   draw string (10,32*14+12),"Lives:"+str(vidas)+" Health:"+str(salud)+" Score:"+str(tanteo)
   draw string (536,32*14+12),"Cheeses 2018"
end sub

'Main

screenres 640,480,4

InicializaTiles()
InicializaLmap()
InicializaVariables()
InicializaPantalla()

dim as keyboard key

do
   'k=inkey
   
   'if k=chr(255)&"H" and lmap(xraton,yraton-1)<>0 then
   '' Up
   if( key.pressed( fb.sc_up ) = true andAlso cbool( lmap(xraton,yraton-1)<>0 ) )then
      if lmap(xraton,yraton-1)=3 then conqueso=1
      put(xraton*32,yraton*32),tiles(1,0),pset:lmap(xraton,yraton)=1
      yraton=yraton-1
      put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
   end if
   
   '' Right
   'if k=chr(255)&"M" and lmap(xraton+1,yraton)<>0 then
   if( key.pressed( fb.sc_right ) = true andAlso cbool( lmap(xraton+1,yraton)<>0 ) ) then
      if lmap(xraton+1,yraton)=3 then conqueso=1
      put(xraton*32,yraton*32),tiles(1,0),pset:lmap(xraton,yraton)=1
      xraton=xraton+1
      put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
   end if
   
   '' Down
   'if k=chr(255)&"P" and lmap(xraton,yraton+1)<>0 then
   if( key.pressed( fb.sc_down ) = true andAlso cbool( lmap(xraton,yraton+1)<>0 ) ) then
      if lmap(xraton,yraton+1)=3 then conqueso=1
      put(xraton*32,yraton*32),tiles(1,0),pset:lmap(xraton,yraton)=1
      yraton=yraton+1
      put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
   end if
   
   '' Left
   'if k=chr(255)&"K" and lmap(xraton-1,yraton)<>0 then
   if( key.pressed( fb.sc_left ) = true andAlso cbool( lmap(xraton-1,yraton)<>0 ) )then
      if lmap(xraton-1,yraton)=3 then conqueso=1
      put(xraton*32,yraton*32),tiles(1,0),pset:lmap(xraton,yraton)=1
      xraton=xraton-1
      put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
   end if
   
   '' Esc
   if( key.pressed( fb.sc_escape ) = true ) then
      salir=true
   end if
   
   if lmap(xraton,yraton)=2 then
      lmap(xraton,yraton)=1:salud=salud-1
      
      if salud=0 then
         vidas=vidas-1:salud=5
      
         if conqueso then
            conqueso=0
            chequeo=false
      
            while chequeo=false
               xqueso=int(rnd*18)+1
               yqueso=int(rnd*11)+2
               
               if lmap(xqueso,yqueso)=1 then
                  lmap(xqueso,yqueso)=3
                  chequeo=true
               end if
            wend
      
            put(xqueso*32,yqueso*32),tiles(3,0),pset
         end if
   
         put(xraton*32,yraton*32),tiles(1,0),pset
         xraton=3:yraton=1:put(xraton*32,yraton*32),tiles(5,0),pset
      end if
      
      line (10,32*14+12)-(536,32*14+12+8),0,bf
      draw string (10,32*14+12),"Lives:"+str(vidas)+" Health:"+str(salud)+" Score:"+str(tanteo)
      draw string (536,32*14+12),"Cheeses 2018"
   end if
   
   if xraton=xvirus and yraton=yvirus then
      vidas=vidas-1:salud=5
      
      if conqueso then
         conqueso=0
         chequeo=false
         
         while chequeo=false
            xqueso=int(rnd*18)+1
            yqueso=int(rnd*11)+2
            
            if lmap(xqueso,yqueso)=1 then
               lmap(xqueso,yqueso)=3
               chequeo=true
            end if
         wend
         
         put(xqueso*32,yqueso*32),tiles(3,0),pset
      end if
   
      xraton=3:yraton=1:put(xraton*32,yraton*32),tiles(5,0),pset
      line (10,32*14+12)-(536,32*14+12+8),0,bf
      draw string (10,32*14+12),"Lives:"+str(vidas)+" Health:"+str(salud)+" Score:"+str(tanteo)
      draw string (536,32*14+12),"Cheeses 2018"
   end if
   
   if xraton=3 and yraton=1 and conqueso then
      conqueso=0:tanteo=tanteo+1
      lmap(xraton,yraton)=5
      put(xraton*32,yraton*32),tiles(5+conqueso,0),pset
      chequeo=false
   
      while chequeo=false
         xqueso=int(rnd*18)+1
         yqueso=int(rnd*11)+2
   
         if lmap(xqueso,yqueso)=1 then
            lmap(xqueso,yqueso)=3
            chequeo=true
         end if
      wend
      
      put(xqueso*32,yqueso*32),tiles(3,0),pset
      line (10,32*14+12)-(536,32*14+12+8),0,bf
      draw string (10,32*14+12),"Lives:"+str(vidas)+" Health:"+str(salud)+" Score:"+str(tanteo)
      draw string (536,32*14+12),"Cheeses 2018"
   end if
   
   if timer-told>0.25 then
      chequeo=false
      
      while chequeo=false
         aux=int(rnd*4)
         
         if aux=0 then
            if lmap(xvirus,yvirus-1)=1 or lmap(xvirus,yvirus-1)=2 then
               put(xvirus*32,yvirus*32),tiles(lmap(xvirus,yvirus),0),pset':lmap(xvirus,yvirus)=1
               yvirus=yvirus-1
               put(xvirus*32,yvirus*32),tiles(4,0),pset
               chequeo=true:mvirus=mvirus+1
            end if
         end if
            
         if aux=1 then
            if lmap(xvirus+1,yvirus)=1 or lmap(xvirus+1,yvirus)=2 then
               if mvirus<5 then
                  put(xvirus*32,yvirus*32),tiles(lmap(xvirus,yvirus),0),pset':lmap(xvirus,yvirus)=1
               else
                  put(xvirus*32,yvirus*32),tiles(2,0),pset:lmap(xvirus,yvirus)=2
                  mvirus=0
               end if
               
               xvirus=xvirus+1
               put(xvirus*32,yvirus*32),tiles(4,0),pset
               chequeo=true:mvirus=mvirus+1
            end if
         end if
      
         if aux=2 then
            if lmap(xvirus,yvirus+1)=1 or lmap(xvirus,yvirus+1)=2 then
               if mvirus<5 then
                  put(xvirus*32,yvirus*32),tiles(lmap(xvirus,yvirus),0),pset':lmap(xvirus,yvirus)=1
               else
                  put(xvirus*32,yvirus*32),tiles(2,0),pset:lmap(xvirus,yvirus)=2
                  mvirus=0
               end if
               
               yvirus=yvirus+1
               put(xvirus*32,yvirus*32),tiles(4,0),pset
               chequeo=true:mvirus=mvirus+1
            end if
         end if
      
         if aux=3 then
            if lmap(xvirus-1,yvirus)=1 or lmap(xvirus-1,yvirus)=2 then
               if mvirus<5 then
                  put(xvirus*32,yvirus*32),tiles(lmap(xvirus,yvirus),0),pset':lmap(xvirus,yvirus)=1
               else
                  put(xvirus*32,yvirus*32),tiles(2,0),pset:lmap(xvirus,yvirus)=2
                  mvirus=0
               end if
               
               xvirus=xvirus-1
               put(xvirus*32,yvirus*32),tiles(4,0),pset
               chequeo=true:mvirus=mvirus+1
            end if
         end if
      wend
      
      told=timer
   end if
   
   if vidas=0 then salir=true
   
   sleep( 1, 1 )
loop until salir

'sleep

This is what you want, but with the joystick buttons, right? It shouldn't be too difficult for you to implement that to work with a joystick, or to use grindstone's solution...

Red Rooster wrote:...adding a sleep time at the end of the gameloop (not needed if you use keyboard arrow keys as you notice in the previous version)...

The 'sleep( 1, 1 )' call inside the main loop is mandatory if you don't want the code to hog all cpu resources, and to keep your app running smoothly. Always include it inside some endless loop when you're running under a multitasking OS.
paul doe
Posts: 539
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: GetJoystick to control a sprite

Postby paul doe » May 06, 2018 16:46

Red Rooster wrote:Thanks paul doe, I will use this keyboard handler sometime for sure. For this finally I decided to include grindstone's solution, that's exactly what I need. So many thanks, un saludo.

You're welcome. Glad that you found an answer. Nos vemos por ahí =D

Return to “Game Dev”

Who is online

Users browsing this forum: No registered users and 2 guests