Computer Science Canada

Insanely basic target shooter

Author:  chrisbrown [ Fri Jun 09, 2006 7:36 pm ]
Post subject:  Insanely basic target shooter

Just a quick time waster I put together in about fifteen minutes. For those who have absolutely nothing better to do. And by the way, Cervantes, wtd and the rest of the code gurus, I realize that this is very rough but I don't have time to fix it up.

code:
%By Methodoxx
%Shoot the targets infinitely.
%Point value is related to speed of target

var winID : int := Window.Open ("position:center,center;graphics:800;450;offscreenonly")

%Predraw scope---------------------------------
%Scope casing
Draw.FillOval (55, 55, 55, 55, black)
Draw.FillOval (55, 55, 50, 50, white)

%Thick crosshair threads
Draw.ThickLine (55 - 50, 55, 55 - 35, 55, 3, black)
Draw.ThickLine (55 + 50, 55, 55 + 35, 55, 3, black)
Draw.ThickLine (55, 55 - 50, 55, 55 - 35, 3, black)
Draw.ThickLine (55, 55 + 50, 55, 55 + 35, 3, black)

%Thin threads
Draw.Line (55 - 35, 55, 55 + 35, 55, black)
Draw.Line (55, 55 - 35, 55, 55 + 35, black)

%Ticks
for i : -30 .. 30 by 6
    Draw.Line (55 - 3, 55 + i, 55 + 3, 55 + i, black)
    Draw.Line (55 + i, 55 - 3, 55 + i, 55 + 3, black)
end for
var xhair : int := Pic.New (0, 0, 110, 110)
cls
%--------------------------------------------------

var X, Y, _X, _Y : array 1 .. 10 of real

%Predraw targets
var colourC : int
for decreasing i : 5 .. 1
    if i mod 2 = 0 then
        colourC := brightred
    else
        colourC := white
    end if
    Draw.FillOval (20, 20, 4 * i, 4 * i, colourC)
end for
var target : int := Pic.New (0, 0, 40, 40)

fcn drawReticule (x, y, short : int) : boolean
    var rad : int
    rad := round (sqrt (abs (x - (X (short))) ** 2 + abs (y - (Y (short))) ** 2)) div 2
    var hit : boolean := false
    Pic.Draw (xhair, x - 55, y - 55, picMerge)
    for i : 25 .. 385 by 90
        if rad < 10 then
            Draw.FillArc (x, y, rad, rad, i, i + 40, brightgreen)
            hit := true
        else
            Draw.Arc (x, y, rad, rad, i, i + 40, brightred)
        end if
    end for
    result hit
end drawReticule

proc drawTargets
    for i : 1 .. 10
        Pic.Draw (target, round (X (i)) - 20, round (Y (i)) - 20, picMerge)
    end for
end drawTargets

var font : int := Font.New ("arialblack;bold:12")
proc drawScore (score : int)
    Font.Draw ("Score = " + intstr (score), 0, 0, font, blue)
    Font.Draw ("Level = " + intstr ((Time.Elapsed div 10000) + 1), maxx - 70, 0, font, blue)
end drawScore

proc drawAcc (taken, made : int)
    if made > 0 then
        Font.Draw ("Accuracy = " + realstr (made / taken * 100, 2) + "%", maxx div 2, 0, font, blue)
    else
        Font.Draw ("Accuracy = 0%", maxx div 2, 0, font, blue)
    end if
end drawAcc

proc moveTargets
    for i : 1 .. 10
        X (i) += _X (i)
        Y (i) += _Y (i)
        if X (i) < 0 or X (i) > maxx then
            _X (i) *= -1
        end if
        if Y (i) < 0 or Y (i) > maxy then
            _Y (i) *= -1
        end if
    end for
end moveTargets

proc newTarget (i, level : int)
    X (i) := maxx * Rand.Int (0, 1)
    _X (i) := Rand.Real * ((Time.Elapsed div 10000) + 1) ** .5
    Y (i) := Rand.Int (1, maxy)
    _Y (i) := Rand.Real * ((Time.Elapsed div 10000) + 1) ** .5
end newTarget

fcn shortestTarget (x, y : int) : int
    var shortest : int := 1
    for i : 2 .. 10
        if sqrt (abs (x - X (i)) ** 2 + abs (y - Y (i)) ** 2) < sqrt (abs (x - X (shortest)) ** 2 + abs (y - Y (shortest)) ** 2) then
            shortest := i
        end if
    end for
    result shortest
end shortestTarget

var mx, my, mb : int    %Mouse location
var shortest : int      %Index of nearest target
var score, tempScore : int := 0         %Score
var lastHit, lastShot : int := 0        %For multiplier
var shotsTaken, shotsMade : int := 0    %for accuracy

for i : 1 .. 10
    newTarget (i, 1)
end for

loop
    Mouse.Where (mx, my, mb)

    moveTargets
    shortest := shortestTarget (mx, my)

    %Draw procedures
    drawTargets

    if drawReticule (mx, my, shortest) and mb = 1 and Time.Elapsed - lastShot > 400 then
        tempScore += 10 * abs (round (_X (shortest))) * ((Time.Elapsed div 10000) + 1)
        newTarget (shortest, ((Time.Elapsed div 10000) + 1))
        lastHit := Time.Elapsed
        lastShot := Time.Elapsed
        shotsMade += 1
        shotsTaken += 1
    elsif mb = 1 and Time.Elapsed - lastShot > 400 then
        shotsTaken += 1
        lastShot := Time.Elapsed
    end if

    if Time.Elapsed - lastHit > 1000 then
        score += tempScore
        tempScore := 0
    end if

    drawScore (score)
    drawAcc (shotsTaken, shotsMade)
    View.Update
    cls
end loop


[Edit]
Quick fixes: slower; now click anywhere on the target instead of the middle

Author:  notleetprogrammer [ Fri Jun 09, 2006 8:05 pm ]
Post subject: 

very cool game...but WAY too fucking hard

Author:  Clayton [ Fri Jun 09, 2006 8:10 pm ]
Post subject: 

try to make it so that you dont have to hit the target in the very middle, otherwise you can barely hit a single target Sad , this is a very good example of internal graphics with Turing that look good, this should be an example to everyone who keeps whining on how to do external graphics, they arent necessary Smile

Author:  upthescale [ Fri Jun 09, 2006 9:14 pm ]
Post subject: 

Good job man, 15 minutes is pretty danmg good, but tmaybe that is why u have some things to work on...

Speed, maybe decrease the speed, and make les targets
Like wut the above post said by SuperFreak "try to make it so that you dont have to hit the target in the very middle" u shud make it so u can hit anywhere in the circle.

other than that good job

Author:  [Gandalf] [ Sat Jun 10, 2006 5:12 pm ]
Post subject: 

Your game will run as fast as possible on every computer. This means that on my Athlon64 the targets are zooming by at speeds impossible to hit. Insert:
code:
Time.DelaySinceLast(15)
Between the View.Update and the cls so that the program plays more or less equally fast on every computer.

Other than that, on a purely application-based level, nice crosshair! Smile

Author:  Jimbo 420 [ Mon Jun 12, 2006 11:19 am ]
Post subject:  Sick Game

Thats a sick game i played fer like 15 mins haha could put more into it thought such as a time limit or a highscore file ect but other that that nice game works great

Author:  TheRedOne [ Thu Jun 15, 2006 11:15 am ]
Post subject: 

good game man. but as said before put a highscore table on there and maybe a time limit. still good. Very Happy

Author:  viperfan7 [ Fri Sep 29, 2006 3:12 pm ]
Post subject: 

someone should make it so the targets are pictures, so you can shoot Sadams head if you want

Author:  Jekate [ Fri Sep 29, 2006 8:21 pm ]
Post subject: 

Cool, + bits!

Author:  Clayton [ Tue Oct 03, 2006 7:37 am ]
Post subject: 

viperfan7 wrote:
someone should make it so the targets are pictures, so you can shoot Sadams head if you want


that was an inappropriate comment, and im sure you will have offended someone here, please refrain from posting such comments on the board (not to mention you revived a topic almost 2 months old...)

Author:  razrdude [ Mon Oct 30, 2006 10:04 pm ]
Post subject: 

Hey im using a similar concept for a game, its a help thread in turing help. However I dont understand how you did the collision part. Like how would I write when the scope , ship and mouse button are the same, i hope u kno what i mean.

Heres my code so far. I gotta add so that the ships get shot

code:
%User Interface
import GUI
%Setcreen creates a window, enters graphic mode, changes the window to 1000
%x600 and turns of the cursor
setscreen ("graphics:1000;600,nocursor")

%This variable is an integers and allow the scope to be drawn anywhere
var x, y, button : int
var mspace1 : int

var spaceship : int
spaceship := Pic.FileNew ("shipf.bmp")
var background : int
background := Pic.FileNew ("background.bmp")
var explosion : int
explosion := Pic.FileNew ("explosion.bmp")

var spacex, spacey : int

spacex := 450
spacey := 300
% this loop is used to randomly generate direction of movement for the space ship
procedure draw (x, y : int)

    View.Set ("offscreenonly")
    randint (mspace1, 1, 15)
    if mspace1 = 1 then
        spacey := (spacey + 50)
    else
        if mspace1 = 2 then
            spacex := (spacex - 50)
        else
            if mspace1 = 3 then
                spacey := (spacey - 50)
            else
                if mspace1 = 4 then
                    spacex := (spacex + 50)

                end if
            end if
        end if
    end if

    Pic.Draw (spaceship, spacex, spacey, picXor)

    Pic.Draw (background, 0, 0, picMerge)


    %Scope casting
    Draw.FillOval (x, y, 55, 55, black)
    Draw.FillOval (x, y, 50, 50, white)

    %Thick crosshair threads
    Draw.ThickLine (x - 50, y, x - 35, y, 3, black)
    Draw.ThickLine (x + 50, y, x + 35, y, 3, black)
    Draw.ThickLine (x, y - 50, x, y - 35, 3, black)
    Draw.ThickLine (x, y + 50, x, y + 35, 3, black)

    %Thin threads
    Draw.Line (x - 20, y, x + 20, y, red)
    Draw.Line (x, y - 20, x, y + 20, red)
    delay (20)
    View.Update
    cls
end draw


loop
    Mouse.Where (x, y, button)
    draw (x, y)
end loop

Author:  Dannyd534 [ Mon Nov 13, 2006 7:07 pm ]
Post subject: 

hey man i love ur game....its cool how u got the accuracy meter at the bottom, its a nice touch


: