freebasic.net Forum Index
FreeBASIC's Official Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log inLog in

Short FB Game
Goto page 1, 2  Next
 
Post new topic   Reply to topic    freebasic.net Forum Index -> Projects
View previous topic :: View next topic  
Author Message
Deleter
Master
PostPosted: Sep 06, 2005 21:40    Post subject: Short FB Game Reply with quote

Ok I got bored last night, so after 2 hours of programming and 5 of debugging :P, here is the result.

Game Description wrote:
You(the green circle) are the hungry Tera-mamoliny, largest organism of the Bitbytenibble world. You must eat the dynamicaly electrode infused megachow(blue circles) in order to survive. However, the kilotwerps(red circles), are selfish little buggers whom--when they take time out from there random dancing moves--want nothing more than to irritate and then eat you. Luckily, you have the peta-blast on your side(left mouse button). One discharge of this power packed punch and they'll be sorry they ever messed with you. :D You progress to the next level once you have eaten the required amount of mega-chow.


Enjoy!!! :)

Code:
Declare Sub StartGame(level As Integer)
Declare Sub UpdatePlayer()
Declare Sub Display()
Declare Sub SpawnChaser(num As Integer)
Declare Sub UpdateChaser()
Declare Sub SpawnResource(num As Integer)
Declare Sub BlastEmToHell()
Declare Sub LoseGame()
Declare Sub WaitForKeyRelease()
Declare Function Collision (x1 As Integer,y1 As Integer,r1 As Integer,x2 As Integer,y2 As Integer,r2 As Integer) As Byte
Declare Function Distance (x1 As Integer, y1  As Integer, x2  As Integer, y2  As Integer)  As Integer
Declare Function Angle(x1 As Integer,y1 As Integer,x2 As Integer,y2 As Integer) As Integer

Type ChaserType
    x               As Integer
    y               As Integer
    lastUpdate      As Double
    lastDamage      As Double
End Type

Type ResourceType
    x               As Integer
    y               As Integer
End Type

Type GameType
    health          As Integer
    button          As Byte
    lastButton      As Double
    lastX           As Integer
    lastY           As Integer
    currentPage     As Byte
    level           As Uinteger
    chaserSpeed     As Integer
    chaserAmount    As Integer
    resourceAmount  As Integer
    resourceNeeded  As Integer
    resourceHave    As Integer
    resourceRate    As Integer
    lastResource    As Double
    difficulty      As Integer
End Type

Const False = 0
Const True = Not(False)
Const Used = 4
Const r2d = .01745329252

Dim Shared    As GameType      game
Dim Shared    As ResourceType  resource(9)
Dim Shared    As ChaserType    chaser(499)

Dim           As Integer       tempfps, _
                               fps, _
                               tempX, _
                               tempY, _
                               tempButtons, _
                               tempAngle, _
                               tempDistance
                       
Dim           As Double        fpsTimer
Dim           As String        tempDifficulty

SCREENRES 640,480,32,2
Randomize Timer
Input "Difficulty? (1=easy, higher = harder)"; tempDifficulty
game.difficulty = Int(Val(tempDifficulty) * 10)
WaitForKeyRelease()
game.resourceRate = 100 / game.difficulty
StartGame(1)
SCREENSET currentPage, -(currentPage-1)
SETMOUSE  ,,0 'hide the cursor
fpsTimer = Timer

Do
    UpdatePlayer()
    UpdateChaser()
    Cls
    Display()
   
    'LOCATE 3,1: PRINT "FPS: "+str$(fps)     
       
    currentPage = -(currentPage-1)
    SCREENSET currentPage, -(currentPage-1)
    'tempfps += 1
    'IF TIMER - fpsTimer >= 1 THEN
    '    fps = tempfps
    '    tempfps=0
    '    fpsTimer = TIMER       
    'END IF
   
Loop Until MULTIKEY(1)
End

Sub StartGame(level As Integer)
    Dim As String CKEY
    Dim As Integer tempButtons, tempX, tempY
    SCREENSET 0,0
    Cls
    If level <> 1 Then
        Print "Level"; level - 1; " Finished!"
        Print "Press Any Key To Continue to Level (or any mouse button ;) )"; level       
    Else
        Print "Press Any Key To Start Game! (or any mouse button ;) )"
        game.resourceHave = Int(500/game.difficulty)
    End If
    Do 'wait for keypress
        For tempCount = 0 To 127
            If MULTIKEY(tempCount) = -1 Then Exit Do
        Next
        GETMOUSE tempX,tempY,,tempButtons
        If tempButtons > 0 Then Exit Do
        CKEY = Inkey$
    Loop Until CKEY = Chr$(255) + "X"
    WaitForKeyRelease()
    game.level = level
    game.chaserAmount = game.level * game.difficulty
    If game.chaserAmount > 500 Then game.chaserAmount = 500
    game.chaserspeed = 5 * game.difficulty + game.level * 10
    game.resourceAmount = 10 - game.level \ 5
    If game.resourceAmount < 1 Then game.resourceAmount = 1
    game.resourceNeeded += (5 * game.difficulty + game.level * 2.5 * game.difficulty)
    SpawnChaser(game.chaserAmount)
    SpawnResource(game.resourceAmount)
End Sub

Sub UpdatePlayer()
    Dim As Integer tempCount, tempX, tempY, tempButtons
   
    GETMOUSE tempX, tempY, ,tempButtons   
   
    game.button = False
    If (tempButtons And 1) And Not(tempX = -1) Then
        game.button = True
    Else
        game.button = False
    End If
   
    For tempCount = 0 To game.resourceAmount - 1
        If Collision(tempX, tempY, 10, resource(tempCount).x, resource(tempCount).y, 5) = True Then
            If (Timer - game.lastResource) >= 1 Then
                game.resourceHave += game.resourceRate
                game.lastResource = Timer
            Elseif (Timer - game.lastResource) * game.resourceRate >= .5 Then
                game.resourceHave += (Timer - game.lastResource) * game.resourceRate
                game.lastResource = Timer
            End If
        End If
    Next
    If game.resourceHave >= game.resourceNeeded Then StartGame(game.level + 1)
    If game.button = True Then 'if left mouse
        If (Timer - game.lastButton) >= game.difficulty\2-1 Then
            BlastEmToHell() 'launch weapon
        End If
    End If
    For tempCount = 0 To game.chaserAmount - 1
        If Collision(tempX, tempY, 10, chaser(tempCount).x, chaser(tempCount).y, 6) = True Then
            Circle(0,0),144,rgb(255,0,255)
            If (Timer - chaser(tempCount).lastDamage) > 1 Then
                game.resourceHave -= game.resourceRate * 2
                chaser(tempCount).lastDamage = Timer
            Elseif (Timer - chaser(tempCount).lastDamage) * game.resourceRate * 2 > .5 Then
                game.resourceHave -= game.resourceRate * 2 * (Timer - chaser(tempCount).lastDamage)
                chaser(tempCount).lastDamage = Timer
            End If
        End If   
    Next
    If game.resourceHave <= 0 Then LoseGame()   
End Sub

Sub Display()
    Dim As Integer tempCount, tempX, tempY, tempButtons, tempColor
   
    For tempCount = 0 To game.resourceAmount - 1
        Circle(resource(tempCount).x,resource(tempCount).y), 5, RGB(0,0,192)
    Next
   
    For tempCount = 0 To game.chaserAmount - 1
        If chaser(tempCount).lastUpdate < Timer Then
            Circle(chaser(tempCount).x,chaser(tempCount).y), 6, RGB(255,32,1)
        Else
            If (Int(Timer*10)) And 1 _
            Then Circle(chaser(tempCount).x,chaser(tempCount).y), 6, RGB(128,16,1)
        End If       
    Next
   
    If (Timer - game.lastButton) < (2.2 / game.difficulty) Then
        For tempX = 120 To (((Timer - game.lastButton) / (22 / game.difficulty))*1200) Step -10
            tempColor = ((Timer - game.lastButton) / (22 / game.difficulty)) * 255
            tempColor = RGB(255,240-tempX*2, 0)'RGB(tempX*2,240-tempX*2, 0)
            Circle(game.lastX, game.lastY), tempX, tempColor
        Next
    End If
   
    GETMOUSE TempX, TempY, , TempButtons
    Circle(tempX, tempY), 10, RGB(0,255,0)
   
    Locate 1,52:Print "Peta-blast"
    tempColor = (Timer - game.lastButton)/(game.difficulty\2-1) * 255
    If tempColor >= 255 Then
        tempColor = RGB(255,255,0)
        Color RGB(255,0,0)
        If Int(Timer) And 1 Then Locate 2,54: Print "READY!"
        Color RGB(224,224,224)
    Else
        tempColor = RGB(255,tempColor,0)
        Locate 2,52:
        Color RGB(0,0,255)
        If (((Int(Timer*5)Shl 29)Shr 29)+4)\2 = 0 Then
            Print "CHARGING."
        Elseif (((Int(Timer*5)Shl 29)Shr 29)+4)\2 = 1 Then
            Print "CHARGING.. "
        Elseif (((Int(Timer*5)Shl 29)Shr 29)+4)\2 = 2 Then
            Print "CHARGING..."
        Else
            Print "CHARGING   "
        End If
        Color RGB(224,224,224)
    End If
    Line(500, 0)-(500+140*(Timer - game.lastButton)/(game.difficulty\2-1),7),tempColor,BF
    Locate 1,1
    If (game.resourceHave/game.resourceNeeded)< .1 Then
        tempColor = RGB(255,0,0)
        Color tempColor
        If Int(Timer * 2) And 1 Then Print "MegaChow:";game.resourceHave; " out of:";game.resourceNeeded
    Else
        Print "MegaChow:";game.resourceHave; " out of:";game.resourceNeeded
    End If
   
End Sub

Sub BlastEmToHell()
    Dim As Integer tempCount, tempX, tempY, tempButtons, tempAngle
    If game.resourceHave <= game.resourceRate * 5 Then Exit Sub
    game.resourceHave -= game.resourceRate * 5 'it costs a ton for this
    GETMOUSE tempX, tempY,,tempButtons
    game.lastX = tempX
    game.lastY = tempY
    For tempCount = 0 To game.chaserAmount - 1
        If Collision(chaser(tempCount).x, chaser(tempCount).y, 6, tempX, tempY, 120 ) Then
            tempAngle = Angle(tempX,tempY,chaser(tempCount).x, chaser(tempCount).y)
            chaser(tempCount).x = Cos(tempAngle * r2d) * 110 + chaser(tempCount).x
            If chaser(tempCount).x < 0 Then
                chaser(tempCount).x += 640
            Elseif chaser(tempCount).x > 639 Then
                chaser(tempCount).x -= 640
            End If
            chaser(tempCount).y = Sin(tempAngle * r2d) * 110 + chaser(tempCount).y
            If chaser(tempCount).y < 0 Then
                chaser(tempCount).y += 480
            Elseif chaser(tempCount).x > 479 Then
                chaser(tempCount).x -= 480
            End If
            chaser(tempCount).lastUpdate = 23/game.difficulty
            If chaser(tempCount).lastUpdate < .5 Then chaser(tempCount).lastUpdate = .5
            chaser(tempCount).lastUpdate += Timer
        End If
    Next
    game.lastButton = Timer
End Sub

Sub SpawnChaser(num As Integer)
    Dim As Integer tempCount, tempX, tempY, tempButtons
    For tempCount = 0 To num - 1
        SETCOORD:
        chaser(tempCount).x = Int(Rnd * 640)
        chaser(tempCount).y = Int(Rnd * 480)
        chaser(tempCount).lastUpdate = Timer
        GETMOUSE tempX, tempY,,tempButtons
        If Distance(tempX, tempY, chaser(tempCount).x, chaser(tempCount).y) < 80 Then Goto SETCOORD
    Next
End Sub

Sub UpdateChaser()
    Dim As Integer tempAngle, tempCount,tempNum
    Dim As Integer tempX, tempY, tempButtons
    Dim As Integer tempLowestIndex, tempLowestValue
    GETMOUSE tempX, tempY,,tempButtons
    tempLowestValue = 2000
    tempLowestIndex = -1
    For tempCount = 0 To game.resourceAmount - 1
        tempDistance = Distance(tempX, tempY, resource(tempCount).x, resource(tempCount).y)
        If tempDistance < tempLowestValue Then
            tempLowestIndex = tempCount
            tempLowestValue = tempDistance
        End If
    Next
    For tempCount = 0 To game.chaserAmount - 1
        If ((Timer - chaser(tempCount).lastUpdate) * game.chaserSpeed) >= 1 Then
            GETMOUSE tempX, tempY,,tempButtons
            tempDistance = Distance(chaser(tempCount).x,chaser(tempCount).y, tempX, tempY)
            tempNum = 10 + tempDistance\25
            If tempDistance < 150 Or Int(Rnd * tempNum)=0 Then
                tempDistance = Distance(resource(tempLowestIndex).x, resource(tempLowestIndex).y, chaser(tempCount).x, chaser(tempCount).y)
                If tempDistance > tempLowestValue Then
                    tempX = resource(tempLowestIndex).x
                    tempY = resource(tempLowestIndex).y
                End If
                tempAngle = Angle(chaser(tempCount).x,chaser(tempCount).y,tempX,tempY)
            Else
                tempAngle = Int(Rnd * 360)
            End If
            chaser(tempCount).lastUpdate = (Timer - chaser(tempCount).lastUpdate)
            If chaser(tempCount).lastUpdate > 1 Then chaser(tempCount).lastUpdate = 1
            chaser(tempCount).x += Cos(tempAngle * r2d) * (game.chaserSpeed * chaser(tempCount).lastUpdate) * (Int(Rnd * 50)+75)/100
            chaser(tempCount).y += Sin(tempAngle * r2d) * (game.chaserSpeed * chaser(tempCount).lastUpdate) * (Int(Rnd * 50)+75)/100
            If chaser(tempCount).x < 0 Then chaser(tempCount).x = 0
            If chaser(tempCount).y < 0 Then chaser(tempCount).y = 0
            If chaser(tempCount).x > 639 Then chaser(tempCount).x = 639
            If chaser(tempCount).y > 479 Then chaser(tempCount).y = 479
            chaser(tempCount).lastUpdate = Timer
        End If   
    Next
   
End Sub

Sub SpawnResource(num As Integer)
    For tempCount = 0 To num - 1
        resource(tempCount).x = Int(Rnd * 640)
        resource(tempCount).y = Int(Rnd * 480)
    Next
End Sub

Sub LoseGame()
    SCREENSET 0,0
    Cls
    Dim As Integer tempCount, tempKey
    Print "YOU HAVE FAILED TO SURVIVE. YOUR CARCASS IS NOW DINNER FOR THE KILOTWERPS."
    Do
        For tempCount = 0 To 127
            If MULTIKEY(tempCount) Then
                Exit Do
            End If           
        Next
    Loop   
    End
END Sub

Sub WaitForKeyRelease()
    Dim As Integer tempCount, tempKey
    Dim As Integer tempButtons, tempX, tempY
    Do
        tempKey = 0
        For tempCount = 0 To 127
            If MULTIKEY(tempCount) Then
                tempKey +=1
                Exit For
            End If           
        Next
        GETMOUSE tempX,tempY,,tempButtons
        If tempButtons > 0 Then tempKey += 1
        If tempKey = 0 Then Exit Sub
    Loop
End Sub

Function Collision (x1 As Integer,y1 As Integer,r1 As Integer,x2 As Integer,y2 As Integer,r2 As Integer) As Byte
    Dim As Integer tempDistance
    tempDistance = Distance(x1,y1,x2,y2)
    If tempDistance <= (r1+r2) Then Return True
    Return False
End Function

Function Distance (x1 As Integer, y1  As Integer, x2  As Integer, y2  As Integer)  As Integer
    Return Int(Sqr( ( x1 - x2 ) ^ 2 + ( y1 - y2 ) ^ 2 ))
End Function
'x1 and y1 are your starting coords, x2 and y2 your dest coords
Function Angle(x1 As Integer,y1 As Integer,x2 As Integer,y2 As Integer) As Integer
    Dim As Integer tempAngle, tempY, tempX
    tempX = x1 - x2
    tempY = y1 - y2
    tempAngle = ( Atn ( tempY / tempX ) ) / r2d
    If tempX < 0 Xor tempY < 0 Then tempAngle = tempAngle + 180
    If tempY >= 0 Then tempAngle = tempAngle + 180
    Return tempAngle
End Function 


(sorry for anyone who browses this forum and qbn...)


Last edited by Deleter on Sep 06, 2005 23:55; edited 1 time in total
 
Back to top
View user's profile
rdc
Master
PostPosted: Sep 06, 2005 21:53    Post subject: Reply with quote

That is very cool.
 
Back to top
View user's profile Send e-mail Visit poster's website
Deleter
Master
PostPosted: Sep 06, 2005 22:02    Post subject: Reply with quote

thanks :)
 
Back to top
View user's profile
cha0s
Site Admin
PostPosted: Sep 06, 2005 22:21    Post subject: Reply with quote

very nice, dude :D

hard as @#&$ even on 2 difficulty ;p
 
Back to top
View user's profile Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
urger

PostPosted: Sep 06, 2005 22:58    Post subject: Reply with quote

A really nice game, looks good, and man, those Killotwerps won't let me get past Level 3!
 
Back to top
View user's profile
Soi

PostPosted: Sep 06, 2005 23:03    Post subject: Reply with quote

I died in level 6... first try of the game. Pretty nice.
 
Back to top
View user's profile
Deleter
Master
PostPosted: Sep 06, 2005 23:57    Post subject: Reply with quote

Ok, I modified the code.
Updates:
-There is now a graphical circle effect when you fire the peta-blaster.
-You can press a mouse button instead of a key to begin a level (don't worry, there is no chance of accidentily using a precious peta-blast, the program waits for you to lift your slow finger ;) )
-when a kilotwerp gets hit by a blast, he blinks until he recovers.

Enjoy!

edit: Ok, for people without FB (lol) or with really really slow computers, I made a .rar with 32 bit AND 8 bit source, and exe's for both. Download tera.rar. enjoy!


Last edited by Deleter on Sep 07, 2005 1:00; edited 1 time in total
 
Back to top
View user's profile
rdc
Master
PostPosted: Sep 07, 2005 0:35    Post subject: Reply with quote

Nice additions.
 
Back to top
View user's profile Send e-mail Visit poster's website
E.K.Virtanen
Sr. Member
PostPosted: Sep 07, 2005 11:36    Post subject: Reply with quote

Nice one, with sounds this might be very addictive =) Worked fine with 1.6ghz wincrab and 600mhz linux pc.

Deleter wrote:
edit: Ok, for people without FB (lol) or with really really slow computers, I made a .rar with 32 bit AND 8 bit source, and exe's for both. Download tera.rar. enjoy!

http://www.geocities.com/roope00/Small.tar.gz for linux :D
 
Back to top
View user's profile Send e-mail Visit poster's website MSN Messenger
dumbledore
Sr. Member
PostPosted: Sep 07, 2005 19:14    Post subject: Reply with quote

8 bits? O_o
 
Back to top
View user's profile Visit poster's website
Deleter
Master
PostPosted: Sep 07, 2005 19:14    Post subject: Reply with quote

Thanks for testing and commenting and thanks lurah for the linux exe.

8-bit color...
 
Back to top
View user's profile
dumbledore
Sr. Member
PostPosted: Sep 07, 2005 19:17    Post subject: Reply with quote

oh... i thought you meant to say 16bit executable :D
 
Back to top
View user's profile Visit poster's website
mambazo
Sr. Member
PostPosted: Sep 07, 2005 19:29    Post subject: Reply with quote

this is quite a little addictive game indeedy!
 
Back to top
View user's profile Visit poster's website MSN Messenger
thesanman112
Sr. Member
PostPosted: Sep 10, 2005 5:39    Post subject: very cool Reply with quote

i made a few changes,hehehe but bitmaps are required!!!!

its so addictive and alive !!!!! hahahahha

http://www.fbtk.net/phpBB2/viewtopic.php?p=7240#7240
 
Back to top
View user's profile
Deleter
Master
PostPosted: Sep 10, 2005 15:11    Post subject: Reply with quote

I posted there because I saw it there first. Thanks for modding my game, its cool to see that people like it. :D
 
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    freebasic.net Forum Index -> Projects All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



sf.net phatcode