Page 3 of 3

Re: [2D] Crazy Snake

Posted: Mon Jul 27, 2015 2:53 pm
by falsam
Vera wrote:I'm thinking about a further idea where the snake doesn't stop at the screen-sides but re-enters again on the opposite side.
Todo list
Vera wrote:switching the key-commands on one level [right turns left etc.]
I did it. The game was unplayable. May be at the beginning of the game. Todo list.
Vera wrote: Is it be possible to free examinekeyboard under certain conditions and recontinue it afterwards?
I'm not sure that's possible with PureBasic. BUT
Vera wrote: feature request for a 'pause'.
YES ....

New version for CrazySnake.
Adding a pause: Press the spacebar to bring the game to pause (or continue).

Add : Pause when the game lost focus.

The first message code and update
:idea: http://www.purebasic.fr/english/viewtop ... 16&t=62548

and the GitHub
:idea: https://github.com/falsam/CrazySnake

Re: [2D] Crazy Snake

Posted: Tue Jul 28, 2015 9:09 am
by Vera
Hi falsam,
that 'Pause' is really good to have :-)
falsam wrote:Contrairement à ce que dit la doc, ExamineKeyboard() renvoie bien une valeur.
It seems a lucky feature ;-)
But on Linux ExamineKeyboard() stays valid so that the 'auto-pause' doesn't occure. And like I mentioned above, ExamineKeyboard occupies (nearly) all keys for the OS while the application is running and even with lost focus*.

I already searched the forum for hours yesterday to find a solution how to deactivate a WindowedScreen or its parent window to somehow mute the app to regain all keycommands, but successless until now. Maybe it's only achivable via API.

* Yeah I found out this morning while testing again and checking GetActiveWindow() that the focus of the WindowedScreen is only really lost is the app is minimized (or if I double-click the windowtitle to fold the window to its title only).
In these cases the key-occupation is freed, ExamineKeyboard() returns 0 and 'auto-pause' will be set.

Hey that's good to know for a start 8)

Re: [2D] Crazy Snake

Posted: Tue Jul 28, 2015 2:48 pm
by falsam
The first sound version of Crazy Snake is available.

:arrow: http://s242132022.onlinehome.fr/downloa ... ySnake.zip (Source + Media)

Thank you to everyone who helped me achieve this release. I hope that you will be crazy at the end of the game.

Re: [2D] Crazy Snake

Posted: Tue Jul 28, 2015 10:04 pm
by davido
@falsam,

Brilliant. :D
Now works perfectly on the Mac; the problem has gone away!
Music is fine.
Pause ok, too.

Re: [2D] Crazy Snake

Posted: Wed Jul 29, 2015 11:33 am
by falsam
davido wrote:Now works perfectly on the Mac; the problem has gone away!
Very good news. Thank You Davido.

Re: [2D] Crazy Snake

Posted: Wed Jul 29, 2015 11:39 am
by Demivec
Here are fixes for 2 bugs that were introduced by me.

The first bug makes the game end if the snake enters the square in the top-left corner. It is caused by some leftover and unneeded code. The fix is to remove the code.

The second bug makes the snake detect a collision with itself by allowing the snake to collide with where its tail part used to be before it moved. It is caused by a combination in the way the snake was updated and the detection for collision with itself. It is corrected by moving the collision detection after the update of the snakes length.

Previous code:

Code: Select all

AddElement(Snakes()) ;add a new part for the head at the front of snake
      
      If Snakes()\x = SnakePart\x + 16 * vx And Snakes()\y = SnakePart\y + 16 * vy
        vx * -1
        vy * -1
      EndIf
      Snakes()\x = SnakePart\x + 16 * vx
      Snakes()\y = SnakePart\y + 16 * vy
      
      SnakePart = Snakes() ;save information for new head part
      
      ;-Collide(head, target)
      If SnakePart\x = tx And SnakePart\y = ty
        TargetCreate = #True
        UpdateSquares = #True
        Score + 1
        PlaySound(#Sound_FindTarget, #PB_Sound_MultiChannel, 100)
      EndIf
      
      ;-Collide(head, Body)
      While NextElement(Snakes())
        If SnakePart\x = Snakes()\x And  SnakePart\y = Snakes()\y
          Boom = #True
          Break
        EndIf
      Wend
      
      If UpdateSquares = #False
        ;Remove tail part to keep snake the same length
        LastElement(Snakes())
        DeleteElement(Snakes())
      Else
        ;Adds an element to the body of the snake, it does so by NOT removing the tail part
        UpdateSquares = #False
      EndIf
Replacement code:

Code: Select all

      AddElement(Snakes()) ;add a new part for the head at the front of snake
      
;       If Snakes()\x = SnakePart\x + 16 * vx And Snakes()\y = SnakePart\y + 16 * vy
;         vx * -1
;         vy * -1
;       EndIf
      Snakes()\x = SnakePart\x + 16 * vx
      Snakes()\y = SnakePart\y + 16 * vy
      
      SnakePart = Snakes() ;save information for new head part
      
      ;-Collide(head, target)
      If SnakePart\x = tx And SnakePart\y = ty
        TargetCreate = #True
        UpdateSquares = #True
        Score + 1
        PlaySound(#Sound_FindTarget, #PB_Sound_MultiChannel, 100)
      EndIf
      
      If UpdateSquares = #False
        ;Remove tail part to keep snake the same length
        LastElement(Snakes())
        DeleteElement(Snakes())
      Else
        ;Adds an element to the body of the snake, it does so by NOT removing the tail part
        UpdateSquares = #False
      EndIf
      
      ;-Collide(head, Body)
      FirstElement(Snakes())
      While NextElement(Snakes())
        If SnakePart\x = Snakes()\x And  SnakePart\y = Snakes()\y
          Boom = #True
          Break
        EndIf
      Wend

Another effect may or may not be a bug. It seems like it is to me though. The effect is based on the window coordinates. The window's inner coordinates is used instead of the frame coordinates. When the effect is in play is using these coordinates it nudges the window off the bottom of the screen if 'up' or 'down' is pushed.

Previous code:

Code: Select all

  WX = WindowX(#MainForm, #PB_Window_InnerCoordinate)
  WY = WindowY(#MainForm, #PB_Window_InnerCoordinate)
Replacement code:

Code: Select all

  WX = WindowX(#MainForm, #PB_Window_FrameCoordinate)
  WY= WindowY(#MainForm, #PB_Window_FrameCoordinate)

Re: [2D] Crazy Snake

Posted: Thu Jul 30, 2015 11:22 am
by Vera
You're rocking it falsam ~ Image ~ the sound-version is great

I missed the blue bubles and the 'default' right-left movement beyond the final Case ... so I re-inserted them ;-)

Ar-s' Sound_Logo is a good addition. As it only apears at game-start I tested it as game-end-sound too which is nice.

Thanks Demivec for realising that the first square got buggy, as I had thought my key-down-event was delayed too much for that corner. (I do encounter delayed or failing key-events now and then when there's too much action in a level and think it's due to my 'slow' PC.)

cheers ~ Vera

ps: I much appreciate the code-developements as it gives me a lot to learn from Image

Re: [2D] Crazy Snake

Posted: Tue Nov 24, 2015 1:39 am
by em_uk
59!

Very good :) well done.