[2D] Crazy Snake

Advanced game related topics
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: [2D] Crazy Snake

Post 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

➽ Windows 11 64-bit - PB 6.0 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect.
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: [2D] Crazy Snake

Post 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)
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: [2D] Crazy Snake

Post 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.

➽ Windows 11 64-bit - PB 6.0 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: [2D] Crazy Snake

Post by davido »

@falsam,

Brilliant. :D
Now works perfectly on the Mac; the problem has gone away!
Music is fine.
Pause ok, too.
DE AA EB
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: [2D] Crazy Snake

Post by falsam »

davido wrote:Now works perfectly on the Mac; the problem has gone away!
Very good news. Thank You Davido.

➽ Windows 11 64-bit - PB 6.0 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect.
User avatar
Demivec
Addict
Addict
Posts: 4090
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: [2D] Crazy Snake

Post 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)
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: [2D] Crazy Snake

Post 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
User avatar
em_uk
Enthusiast
Enthusiast
Posts: 366
Joined: Sun Aug 08, 2010 3:32 pm
Location: Manchester UK

Re: [2D] Crazy Snake

Post by em_uk »

59!

Very good :) well done.
----

R Tape loading error, 0:1
Post Reply