News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

First game : Shoot the target

Started by Silvh, January 08, 2008, 10:25:04 AM

Previous topic - Next topic

Silvh

Ah, i got bored today after reading some tutorials. And I came up with the following simple game : Shoot the target!

The objective of the game is to shoot the little targets that appear on the screen when you clicked another one. Pretty simple yes, but there is also a time limit! I limited the time to 90 seconds(Yes, the time is really 90 seconds, this system is not based on Frames Per Second. but on the windows time).

Anyhow, I attached a screenshot and the download can he found Here

I know it's nothing big or fancy, But I'm quite proud of it as I only own PlayBasic for a day ;D

Updated
I just added some sounds to it, makes it less boring. And reduced the play time to 90 seconds.

Also, If you fail to click on the target now. You will lose points... Though your points can't go any lower then 0 ;)

Source Code
Main.pba
; PROJECT : Main
; AUTHOR  : Silvester
; CREATED : 8-1-2008
; EDITED  : 8-1-2008
; ---------------------------------------------------------------------
#Include "inifiles.pba"
#Include "Time&Date.pba"

;Set FPS to max 60
SetFPS 60

;Set the windows cursor off
Mouse Off

;Setup Global variables.
Global Score
Global PlayerName$
Global Aim_Img = 1
Global Frame_Img = 2
Global Target_Img = 3
Global Debug
Global TargetX
Global TargetY
Global DoX
Global DotY
Global State
Global Time=90
Global OldSecond
Global Clicked

;Read Game Settings
Debug = GetIniInteger("Config.ini","Debug","Debugmode",0)

;Load Media
LoadImage "Media Assets\Images\Aimer.png", Aim_Img
LoadImage "Media Assets\Images\Frame.png", Frame_Img
LoadImage "Media Assets\Images\Target.png", Target_Img
LoadSound "Media Assets\Sound\Sword.wav",2
LoadSound "Media Assets\Sound\Miss.wav",3
LoadMusic "Media Assets\Sound\Music_001.mp3",1
;Set oldsecond to current
Oldsecond=Currentsecond()

;Play Music
PlayMusic 1

;Main Loop
Do
;Clear old Data from screen
Cls 0

;Draw Target on screen
Draw_Target(TargetX,TargetY,Target_Img,1)

;Draw Screen GFX
Draw_GFX(Aim_Img,Frame_Img,Target_Img,Score,Time)

;Generate new Target Position
If TargetX=0 And TargetY=0
TargetX=Rnd(672)
TargetY=Rnd(472)
EndIf

;Debug Info
If Debug=1
Draw_Debug_Info(DotX,DotY)
If LeftMouseButton()=1
DotX=MouseX()
DotY=MouseY()
Dot MouseX(),MouseY()
EndIf
If SpaceKey()=1 Then Time=Time-1
EndIf

;Handle Mouseclick
If Clicked = 0
If LeftMouseButton()=1
If MouseX()<TargetX+128 And MouseX()>TargetX+64 And MouseY()<TargetY+128 And MouseY()>TargetY+64
PlaySound 2
Score=Score+1
TargetX=0
TargetY=0
Else
PlaySound 3
Score=Score-1
EndIf
EndIf
If Clicked = 0 Then Clicked = 1
EndIf

;MouseClick off
If LeftMouseButton()=0 Then Clicked = 0

;Set anything on Score below 0 to 0
If Score<0 Then Score = 0

;Handle Time label
If Oldsecond<>CurrentSecond()
Time=Time-1
OldSecond = Currentsecond()
   EndIf

;If timer reaches 0
If Time=0
StopMusic 1
Cls 0
Text GetScreenWidth()/2-50,GetScreenHeight()/2,"Game Over!"
Text GetScreenWidth()/2-90,GetScreenHeight()/2+20,"Press a Key to Exit "
Text 0,0,Str$(Score)+" Points earned!"

If FileExist("Score.txt")=0
MakeFile "Score.txt",0
Else
DeleteFile "Score.txt"
MakeFile "Score.txt",0
EndIf

OpenFile "Score.txt",1
String$ = Str$(Score)
WriteString 1,String$
CloseFile 1

Sync
WaitKey
End
EndIf

;Draw new items on screen
Sync
Loop


Func_Draw.pba
; PROJECT : Main
; CREATED : 8-1-2008
; EDITED  : 8-1-2008
; ---------------------------------------------------------------------
Function Draw_GFX(Aim_Img,Frame_Img,Target_Img,Score,Time)
Draw_Aim_Img(Aim_Img,1)
Draw_Frame_Img(Frame_Img,1)
Text 0,0,"Score : "+Str$(Score)
Text 0,20,"Time Left : "+Str$(Time)+" Seconds"
EndFunction

Function Draw_Aim_Img(ImgNumber,Transp_Flag)
Pos_X=MouseX()-32
Pos_Y=MouseY()-32

DrawImage ImgNumber,Pos_X,Pos_Y,Transp_Flag

EndFunction

Function Draw_Frame_Img(Imgnumber,Transp_Flag)
DrawImage Imgnumber,0,0,Transp_Flag
EndFunction

Function Draw_Target(X,Y,Img,Transp_Flag)
If X>0 And Y>0
DrawImage Img,X+64,Y+64,Transp_Flag
EndIf
EndFunction


Function Draw_Debug_Info(X,Y)
Text 35,35,"Screen FPS : "+Str$(FPS())
Text 35,55,"Mouse X : "+Str$(MouseX())
Text 35,75,"Mouse Y : "+Str$(MouseY())
Text 35,95,"Debug Dot X : "+Str$(X)
Text 35,115,"Debug Dot Y : "+Str$(Y)
Dot X,Y
EndFunction

"All we have to do, is decide what to do. With the code that is given to us"

Silvh

Updated the game, and the above post.
"All we have to do, is decide what to do. With the code that is given to us"

iloveTALLwoman


Silvh

Actually, I don't. But it's the best I can do.
"All we have to do, is decide what to do. With the code that is given to us"