Snake simulation

Share your advanced PureBasic knowledge/code with the community.
threedslider
Enthusiast
Enthusiast
Posts: 397
Joined: Sat Feb 12, 2022 7:15 pm

Snake simulation

Post 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
User avatar
moulder61
Enthusiast
Enthusiast
Posts: 193
Joined: Sun Sep 19, 2021 6:16 pm
Location: U.K.

Re: Snake simulation

Post by moulder61 »

Yes, very nice. And only 20 lines of code. :)
I'm thinking "Now how can I use that somehow?" ;)

Moulder.
"If it ain't broke, fix it until it is!

This message is brought to you thanks to SenselessComments.com

My PB stuff for Linux: "https://u.pcloud.link/publink/show?code ... z3MR0T3jyV
User avatar
jacdelad
Addict
Addict
Posts: 2010
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Snake simulation

Post by jacdelad »

Hehe, cool! And also tiny code. :D
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Fred
Administrator
Administrator
Posts: 18220
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Snake simulation

Post 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.
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Snake simulation

Post 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
threedslider
Enthusiast
Enthusiast
Posts: 397
Joined: Sat Feb 12, 2022 7:15 pm

Re: Snake simulation

Post by threedslider »

Thanks to all :)

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

@infratec : Nice for this code too !
User avatar
Erolcum
User
User
Posts: 51
Joined: Fri Jun 07, 2024 10:45 am
Location: Turkiye
Contact:

Re: Snake simulation

Post 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 ?
You may visit my new Purebasic blog here..
:arrow: https://erolcum-github-io.translate.goo ... r_pto=wapp
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Snake simulation

Post 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.
User avatar
Erolcum
User
User
Posts: 51
Joined: Fri Jun 07, 2024 10:45 am
Location: Turkiye
Contact:

Re: Snake simulation

Post 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
You may visit my new Purebasic blog here..
:arrow: https://erolcum-github-io.translate.goo ... r_pto=wapp
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Snake simulation

Post 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.
User avatar
Erolcum
User
User
Posts: 51
Joined: Fri Jun 07, 2024 10:45 am
Location: Turkiye
Contact:

Re: Snake simulation

Post 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
You may visit my new Purebasic blog here..
:arrow: https://erolcum-github-io.translate.goo ... r_pto=wapp
User avatar
HeX0R
Addict
Addict
Posts: 1204
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Snake simulation

Post by HeX0R »

"RTFM" is not a German invention :D
User avatar
Erolcum
User
User
Posts: 51
Joined: Fri Jun 07, 2024 10:45 am
Location: Turkiye
Contact:

Re: Snake simulation

Post 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
You may visit my new Purebasic blog here..
:arrow: https://erolcum-github-io.translate.goo ... r_pto=wapp
Post Reply