10.03.2019 - Round 1 results of our "A Love Letter For FreeBASIC" game dev competition have been published. Please be sure to check the results thread: http://games.freebasic.net/forum/index.php?topic=629.0. Don't forget that the competition is continuing with a round 2, lasting till 29th of April, 300 USD first prize. Stay tuned!

Author Topic: Multi Player Snake Game Made in 30 Minutes!  (Read 8793 times)

KristopherWindsor

  • Forum Sage
  • *****
  • Posts: 363
  • The Thirsty Smiley
    • View Profile
    • Reddit/r/pics
    • Email
Multi Player Snake Game Made in 30 Minutes!
« on: February 26, 2008, 03:28:25 AM »
This game is simple, but maybe someone else can enhance it. ;-)
In this multi player version of snake, the two snakes are controlled by the joystick. (I only have one good joystick, so I configured the two analog pads to each control a snake; you will probably need to reconfigure the controls, anyway. :P)
The snakes can go over their own trails, but not the trails of the other players.
This game can easily be expanded to support 3 or 4 players if you have the hardware. ;-)

Code: [Select]
' Snake! v1.0
' (C) 2008 Innova and Kristopher Windsor

Const screenx = 800, screeny = 600, player_max = 2

Type player_type
  As Single x, y
  As Double angle
  As Single Ptr jx, jy
  As Uinteger c, score
End Type

Dim Shared As Single x1, x2, x3, x4
Dim Shared As player_type player(1 To player_max)

Screenres screenx, screeny, 32
Randomize Timer

Sub setup
  For i As Integer = 1 To player_max
    With player(i)
      .x = (i - .5) * screenx / player_max
      .y = screeny / 2
      .angle = Atn(1) * 2
      If .c = 0 Then
        'game has not started
        .c = Rgb(Rnd * 256, Rnd * 256, Rnd * 256)
        Select Case i
        Case 1
          .jx = @x1
          .jy = @x2
        Case 2
          .jx = @x4
          .jy = @x3
        End Select
      End If
    End With
  Next i
End Sub

setup

Do
  Getjoystick(0,, x1, x2, x3, x4)
 
  For i As Integer = 1 To player_max
    With player(i)
      If Abs(*.jy) + Abs(*.jx) > .08 Then .angle = Atan2(*.jy, *.jx)
      .x += Cos(.angle)
      .y += Sin(.angle)
      If .x < 0 Then .x += screenx
      If .x > screenx - 1 Then .x -= screenx
      If .y < 0 Then .y += screeny
      If .y > screeny - 1 Then .y -= screeny
      For i2 As Integer = 1 To player_max
        If i <> i2 Then
          If Point(.x, .y) = player(i2).c Then
            Circle (.x, .y), 8, &HFFFFFFFF
            player(i2).score += 1
            Print "Player " & i2 & " now has " & player(i2).score & " points"
            setup
            Sleep
            Cls
          End If
        End If
      Next i2
    End With
  Next i
 
  For i As Integer = 1 To player_max
    With player(i)
      Pset (.x, .y), .c
    End With
  Next i
 
  Sleep 10
Loop Until Inkey = Chr(27)

Print "Final Score:"
For i As Integer = 1 To player_max
  With player(i)
    Print "Player " & i & ": " & .score
  End With
Next i
Sleep
« Last Edit: February 26, 2008, 03:33:17 AM by KristopherWindsor »

Lachie Dazdarian

  • Double dipper
  • Administrator
  • Forum Sage
  • *****
  • Posts: 1308
    • Yahoo Instant Messenger - lachie13
    • View Profile
    • The Maker Of Stuff
    • Email
Re: Multi Player Snake Game Made in 30 Minutes!
« Reply #1 on: February 26, 2008, 04:34:51 PM »
I don't have a joystick so....but I liked the apperance of snakes.

Anyway, you know my general opinion about this sort of quick programs.

I would be intrigued if you made a snakes AI.
« Last Edit: February 26, 2008, 04:36:38 PM by Lachie Dazdarian »
"Things like Basic and Free Basic provide much-needed therapy and a return to sanity and a correct appreciation of people. The arrogant folk really hate a word like 'Basic' - fine, and good riddance." ~ pragmatist

KristopherWindsor

  • Forum Sage
  • *****
  • Posts: 363
  • The Thirsty Smiley
    • View Profile
    • Reddit/r/pics
    • Email
Re: Multi Player Snake Game Made in 30 Minutes!
« Reply #2 on: February 26, 2008, 07:01:26 PM »
I would be intrigued if you made a snakes AI.

It's much more fun with two real players. ;-)
And besides, I don't have a good strategy (to give to the AI) yet. :P