Here's a little snake-game i made with my little brother to teach him programming. It is quite simple, but i think it turned out quite ok :) It is programmed in a demo version of BlitzPlus so i cannot make an executable. The demo is also limited to about 800 lines of code i think. Download it here to try the game or port it to another language.
SeedRnd(MilliSecs())
; Definitions
Type FoodType
Field x
Field y
End Type
Type HeadType
Field x
Field y
Field direction
End Type
Type TailType
Field x
Field y
Field direction
Field pause
End Type
; Double-buffered window
Graphics 800,600,32,0
SetBuffer BackBuffer()
; Load graphics and rotate snake in 4 directions
Dim gfxhead(4),gfxbody(4),gfxtail(4)
gfxfood=LoadImage("gfx/snake_food.png")
gfxhead(0)=LoadImage("gfx/snake_head.png")
gfxbody(0)=LoadImage("gfx/snake_body.png")
gfxtail(0)=LoadImage("gfx/snake_tail.png")
For a=1 To 3
gfxhead(a)=CopyImage(gfxhead(0))
RotateImage(gfxhead(a),90*a)
MidHandle gfxhead(a)
gfxbody(a) = CopyImage(gfxbody(0))
RotateImage(gfxbody(a), 90*a)
MidHandle gfxbody(a)
gfxtail(a) = CopyImage(gfxtail(0))
RotateImage(gfxtail(a), 90*a)
MidHandle gfxtail(a)
Next
MidHandle gfxfood
MidHandle gfxhead(0)
MidHandle gfxbody(0)
MidHandle gfxtail(0)
; Game-logics variables
Global score
food.FoodType = New FoodType
head.HeadType = New HeadType
tail.TailType = New TailType
; Playfield 50x36
Dim track (50,36)
; Start the game
start(food, head, tail)
Repeat
If KeyDown(200) And head\direction <> 2 Then
head\direction = 0
ElseIf KeyDown(203) And head\direction <> 1 Then
head\direction = 3
ElseIf KeyDown(205) And head\direction <> 3 Then
head\direction = 1
ElseIf KeyDown(208) And head\direction <> 0 Then
head\direction = 2
End If
MoveSnake(head, tail)
; Eat food
If food\x = head\x And food\y = head\y Then
thisScore = Rand(1,10)
tail\pause = tail\pause + thisScore
score = score + thisScore * 10
PlaceFood(food)
End If
; Do graphics
Cls
DrawImage(gfxfood, food\x * 16 +8, food\y * 16 +8)
For x =0 To 49
For y =0 To 35
If track(x,y) >= 0 Then
DrawImage(gfxbody(track(x,y)),x * 16 +8,y * 16 +8)
End If
Next
Next
DrawImage(gfxhead(head\direction), head\x * 16 +8, head\y * 16 +8)
DrawImage(gfxtail(tail\direction), tail\x * 16 +8, tail\y * 16 +8)
Line(0,36 * 16, 799,36 * 16)
Text(10, 580, "Score: " + Str(score))
Flip
; Collision detection (after graphics are drawn so one can actually see the collision)
If track(head\x,head\y) >= 0 Or (head\x = tail\x And head\y = tail\y) Then
Delay(1000)
Cls()
Text(400, 290, "Your score: " + Str(score), 1, 1)
Text(400, 300, "Press return to start", 1, 1)
Flip()
Repeat
Until KeyHit(28) Or KeyDown(1)
; Restart the game
start(food, head, tail)
End If
; Snake speed
Delay(100)
Until KeyDown(1)
; Functions
Function MoveSnake(head.HeadType, tail.TailType)
; Move head
track(head\x,head\y) = head\direction
If head\direction = 0 Then
head\y = head\y-1
If head\y < 0 Then head\y = 35
ElseIf head\direction = 1 Then
head\x = head\x+1
If head\x > 49 Then head\x = 0
ElseIf head\direction = 2 Then
head\y = head\y +1
If head\y > 35 Then head\y = 0
ElseIf head\direction = 3 Then
head\x =head\x-1
If head\x < 0 Then head\x = 49
End If
; Move tail
If tail\pause > 0 Then
tail\pause = tail\pause - 1
Else
If tail\direction = 0 Then
tail\y = tail\y-1
If tail\y < 0 Then tail\y = 35
ElseIf tail\direction = 1 Then
tail\x = tail\x+1
If tail\x > 49 Then tail\x = 0
ElseIf tail\direction = 2 Then
tail\y = tail\y +1
If tail\y > 35 Then tail\y = 0
ElseIf tail\direction = 3 Then
tail\x =tail\x-1
If tail\x < 0 Then tail\x = 49
End If
tail\direction = track(tail\x, tail\y)
track(tail\x,tail\y) = -1
End If
End Function
Function PlaceSnake(head.HeadType, tail.TailType)
head\x = 25
head\y = 18
head\direction = 0
tail\x = 25
tail\y = 19
tail\direction = 0
tail\pause = 1
End Function
Function PlaceFood(food.FoodType)
Repeat
food\x = Rand(0,49)
food\y = Rand(0,35)
Until track(food\x, food\y) = -1
End Function
Function Start(food.FoodType, head.HeadType, tail.TailType)
score = 0 ; Score is global
For x = 0 To 49
For y = 0 To 35
track(x,y) = -1 ; And track is global
Next
Next
PlaceFood(food)
PlaceSnake(head, tail)
End Function