Page 1 of 1

Snake simulation

Posted: Tue Jul 16, 2024 5:36 pm
by threedslider
Hi

I share you a nice snake simulation :)

Here the code :

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Coded with Purebasic v.6.11 by threedslider 16/07/2024
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Test for Snake :)


InitSprite()
InitKeyboard()

OpenWindow(1, 0,0,800/ DesktopResolutionX(),600/ DesktopResolutionY(),"Snake",  #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(1),0,0,800,600,0,0,0)
SetFrameRate(30)



Repeat
  ExamineKeyboard()
  event = WindowEvent()
  ClearScreen(RGB(0,0,0))
  
  StartDrawing(ScreenOutput())
  For x = 0 To 360
    
    move.f  +  1/10000
    
      snake_x.f =  10* Cos(30*Cos(move-((x/5)))/10) 
      
      snake_y.f =  10 *(move*2-x)-x
     

        Box( snake_x+400, snake_y+x, 5*Cos(x/4)/2, Cos(x/4)*5, RGB(255, 0, 0) )
    
         
  Next
    
  StopDrawing()
  
  
  Delay(1) : FlipBuffers()
  
Until event = #PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)
End

Re: Snake simulation

Posted: Tue Jul 16, 2024 5:43 pm
by moulder61
Yes, very nice. And only 20 lines of code. :)
I'm thinking "Now how can I use that somehow?" ;)

Moulder.

Re: Snake simulation

Posted: Tue Jul 16, 2024 5:43 pm
by jacdelad
Hehe, cool! And also tiny code. :D

Re: Snake simulation

Posted: Tue Jul 16, 2024 6:40 pm
by Fred
Nice but the event loop is wrong. I definitely needs to find a way to detect this as runtime as its very common.

Re: Snake simulation

Posted: Tue Jul 16, 2024 7:55 pm
by infratec
slightly modified:

Code: Select all

EnableExplicit

Define.i Event, Exit, snake_x, snake_y, x
Define.f move


InitSprite()
InitKeyboard()

OpenWindow(0, 0, 0, 800 / DesktopResolutionX(), 600 / DesktopResolutionY(), "Snake",  #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)
SetFrameRate(30)

Repeat
  
  Repeat
    Event = WindowEvent()
    If Event = #PB_Event_CloseWindow
      Exit = #True
    EndIf
  Until Event = 0
  
  ClearScreen(#Black)
  
  If StartDrawing(ScreenOutput())
    For x = 0 To 360
      move + 1/10000
      
      snake_x = 10 * Cos(30 * Cos(move - ((x / 5))) / 10)
      snake_y = 10 * (move * 2 - x) - x
      
      Box(snake_x + 400, snake_y + x, 5 * Cos(x / 4) / 2, Cos(x / 4) * 5, #Red)
    Next
    
    StopDrawing()
  EndIf
  
  FlipBuffers()
  
  ExamineKeyboard()
  If KeyboardReleased(#PB_Key_Escape)
    Exit = #True
  EndIf
  
Until Exit

Re: Snake simulation

Posted: Wed Jul 17, 2024 4:11 pm
by threedslider
Thanks to all :)

@Fred : Nice ! I am looking forward at this.

@infratec : Nice for this code too !

Re: Snake simulation

Posted: Wed Jul 17, 2024 4:55 pm
by Erolcum
Fred wrote: Tue Jul 16, 2024 6:40 pm Nice but the event loop is wrong. I definitely needs to find a way to detect this as runtime as its very common.
What is wrong with the event loop ? :shock: Can you explain for beginners ?

Re: Snake simulation

Posted: Wed Jul 17, 2024 5:12 pm
by infratec
If you work with OpenWindowedScreen, you have to process all available window events before you do the screen stuff.
This is also written in the help.

My modified version does it the correct way.

Re: Snake simulation

Posted: Wed Jul 17, 2024 7:18 pm
by Erolcum
ok I see. I think, there is one more important trick for beginners like me. Regarding the code on first message, if you do not use Delay(1) when WindowEvent() is used then you will get a very high cpu usage in task manager. That’s why, to use WaitWindowEvent() is better generally if applicable. I know it is not possible to use it with this code

Re: Snake simulation

Posted: Wed Jul 17, 2024 7:36 pm
by infratec
A Delay(1) is not needed in a Screen program.

FlipBuffers() waits enough :wink:

And else you can use WaitWindowEvent(1) in a 'normal' programm if it is really needed.

Re: Snake simulation

Posted: Wed Jul 17, 2024 8:10 pm
by Erolcum
I read the help and see the note that you mentioned. I remembered a sentence that I know the Germans love very much, "Have you read the manual ?" :D

Re: Snake simulation

Posted: Wed Jul 17, 2024 8:52 pm
by HeX0R
"RTFM" is not a German invention :D

Re: Snake simulation

Posted: Wed Jul 17, 2024 9:32 pm
by Erolcum
HeX0R wrote: Wed Jul 17, 2024 8:52 pm "RTFM" is not a German invention :D
thanks to you, I learnt a new word, very important and very funny