UnderwareDESIGN

PlayBASIC => Show Case => Topic started by: ATLUS on March 31, 2012, 10:39:11 AM

Title: Snake Game
Post by: ATLUS on March 31, 2012, 10:39:11 AM
Hi guys.

This my first snake game! I spent 24 hours on all =)

and in this topic source code

http://www.underwaredesign.com/forums/index.php?topic=3133.0

http://file.kirovnet.ru/d/612238/snake.rar (http://file.kirovnet.ru/d/612238/snake.rar)
Title: Re: Snake Game
Post by: kevin on April 01, 2012, 08:53:02 AM

Well done.. plays like good old snake..
Title: Re: Snake Game
Post by: ATLUS on April 01, 2012, 09:01:21 AM
Thank you Kevin =)
Title: Re: Snake Game
Post by: kevin on April 01, 2012, 09:33:20 AM
   Here's a bit of intro screen replacement for you,  it just adds a fade from and to black around the title screen.  Make sure you load the title picture as an FX image.

ie

 loadFXimage "gfx/titlescreen.png",5

[pbcode]


  loadFXimage "gfx/titlescreen.png",5


  title_screen(5)

   
function Title_Screen(TitleScreenIMage)

      Spr=NewSprite(0,0,TitleScreenIMage)      
      SpriteDrawMode Spr,2
      
      Mode=0

     ; Time Fade should take in milliseconds
      Duration =500
      
     ; when the just effect is to complete and move to the next mode      
      StartTime=Timer()
      EndTime=StartTime+Duration
                                             
      do

         CurrentTime=Timer()
         TimePast=CurrentTime-STartTime         

         Select Mode

               ; ---------------------------
               ; FADE IN
               ; ---------------------------
               case 0
               
                  if CurrentTime<EndTime
                     Level#=TimePast*(255.0/Duration)
                  else
                  
                     level#=255
                     Mode++
                  endif

               ; ---------------------------
               ; Wait for Key press
               ; ---------------------------
               case 1

                  if scancode()<>0
                     StartTime=CurrentTime
                     EndTime=CurrentTime+Duration
                      Mode =2

                  endif


               ; ---------------------------
               ; FADE OUT
               ; ---------------------------
               case 2
               
                  if CurrentTime<EndTime
                     Level#=255.0-(TimePast*(255.0/Duration))
                  else
                     mode++
                  endif

               ; ---------------------------
               ; Fade Complete, so exit do loop
               ; ---------------------------
               case 3
                     exitdo
         
         EndSelect

         C=LEvel#
         c=argb(255,c,c,c)
         spritetint Spr,C
         drawsprite spr

         sync
      loop

      ; kill the temp sprite used in the intro
      deletesprite spr

endfunction

[/pbcode]
 
Title: Re: Snake Game
Post by: ATLUS on April 01, 2012, 09:59:51 AM
this is the very thing that I wanted to add, Thank you Kevin =)
Title: Re: Snake Game
Post by: kevin on April 01, 2012, 11:18:10 AM
 Looking at the code there's a few bits of code that can be tweaked up a bit.

For example, the code draws the screen edges

[pbcode]
   // draw walls
   for c=0 to 800 step 40
         drawimage 3,c,0,1
   next c

   for c=0 to 800 step 40
         drawimage 3,c,560,1
   next c
[/code]

  Can just be done in one loop


// draw walls
for c=0 to 800 step 40
drawimage 3,c,0,1
drawimage 3,c,560,1
next c



  What you could do, is draw the environmental stuff (boarders/backdrop colour) to a screen size image, then simply draw the image each frame.


  In some other places, there's loops that are polling array values that never change inside the loop.   While not such a huge issue in this type of game, this can blow out to be very expensive in larger projects.  

[code]
         
   for m=5 to 90 step 1
      if(player(1).xpos=player(m).xpos and player(1).ypos=player(m).ypos and player(m).fl = true)
         eact=true
         playsound 2
      endif
   next m
[/pbcode]

  So the above loop like this can be tweaked up a bit like this..

[pbcode]
       CurrentXpos=player(1).xpos
       CurrentYpos=player(1).ypos

       ; we don't need the step here FOR loops, loop by positive one by default.    
   for m=5 to 90
            ; check if this thing is active
            if player(m).fl = true
              ; if so, then check for the match
      if  ( CurrentXpos=player(m).xpos and  CurrentYpos=player(m).ypos)
         eact=true
         playsound 2
      endif
            endif
   next m
[/pbcode]

 You can find all sorts of such tips in  A Crash Course In Optimization  (http://www.underwaredesign.com/forums/index.php?topic=2548.0) tutorial


Title: Re: Snake Game
Post by: ATLUS on April 01, 2012, 11:44:39 AM
nice useful improvements, Thank you Kevin =)

I see some improvements too

x=rndrange(1,19)
x += 40-mod(x,40)
y=rndrange(40,520)
y += 40-mod(y,40)

tweaked up

x=rndrange(1,19)
x *= 40
y=rndrange(1,14)
y *= 40


if direct = 1
tempx=player(1).xpos
tempy=player(1).ypos
score += 5
player(1).xpos -= tile
playsound 3
for i=2 to n
tempx1 = player(i).xpos
tempy1 = player(i).ypos

player(i).xpos = tempx
player(i).ypos = tempy

tempx = tempx1
tempy = tempy1
next i
time = timer()
endif


tweaked up

if direct = 1
player(0).xpos=player(1).xpos
player(0).ypos=player(1).ypos
score += 5
player(1).xpos -= tile
playsound 3
for i=2 to n
tempx1 = player(i).xpos
tempy1 = player(i).ypos

player(i).xpos = player(0).xpos
player(i).ypos = player(0).ypos

player(0).xpos = tempx1
player(0).ypos = tempy1
next i
time = timer()
endif
Title: Re: Snake Game
Post by: kevin on April 01, 2012, 12:25:59 PM

When the snake moves, do you actually need more than one copy loop for the tail ?  Looks like you just place that code inside a function and then call that each time.  

 something like,



Function Copy_Tail_Down(n)
for i=2 to n
tempx1 = player(i).xpos
tempy1 = player(i).ypos

player(i).xpos = player(0).xpos
player(i).ypos = player(0).ypos

player(0).xpos = tempx1
player(0).ypos = tempy1
next i
EndFunction





Title: Re: Snake Game
Post by: ATLUS on April 01, 2012, 01:05:56 PM
I add all new improvements, Thank you so much Kevin! =)
Title: Re: Snake Game
Post by: ATLUS on April 03, 2012, 05:40:30 AM
Some code update and add new title.
Title: Re: Snake Game
Post by: micky4fun on May 18, 2012, 05:02:15 PM
Hi All

nice one ATLUS , i got upto 38 , its get quite fast and i keep getting myself trapped in my own tail ,

mick
Title: Re: Snake Game
Post by: Sigtrygg on May 19, 2012, 03:20:59 PM
Hello Atlus!

Exiting game! Finaly it was very fast  :)

I scored 4405!

Greetings by

Sigtrygg
Title: Re: Snake Game
Post by: ATLUS on May 19, 2012, 04:31:40 PM
Hi Guys, Thank you for playing ^__^ My record 3120 =]


Sigtrygg nice score!

poor forum score :D board:

1.Sigtrygg  4405
2.micky4fun 3800
3.ATLUS 3120
Title: Re: Snake Game
Post by: monkeybot on May 19, 2012, 06:55:11 PM
5455
it was going really fast,i panicked!
Title: Re: Snake Game
Post by: ATLUS on May 20, 2012, 02:39:54 AM
Huh big scrore good job monkeybot =)

poor forum score Cheesy board:
1.monkeybot 5455
2.Sigtrygg 4405
3.micky4fun 3800
4.ATLUS 3120

Thank You for playing!