Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Little game i made
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Clayton




PostPosted: Fri Aug 11, 2006 3:14 pm   Post subject: Little game i made

heres a little game i made, basically there are a bunch of balls floating around, your job is to click on them to make them disappear before time runs out, the object of the game is to get as high of a score as you can, to make more balls press enter on the keyboard, but beware, if you dont clear all of the balls before time runs out you lose, any suggestions would be appreciated Very Happy

EDIT: UPDATED CODE

Turing:

class balls
    import Draw, Mouse
    export initialize, move, draw, draw_, check_clicked
    var size, x, y, colur : int
    var x_vel, y_vel : int
    var draw_ : boolean := true

    proc initialize (x_, y_, size_, color_, xVel, yVel : int)
        x := x_
        y := y_
        size := size_
        x_vel := xVel
        y_vel := yVel
        if color_ >= 1 and color_ <= maxcolor then
            colur := color_
        else
            colur := brightred
        end if
    end initialize

    proc move
        x += x_vel
        y += y_vel
        if x + size >= maxx then
            x := maxx - size
            x_vel *= -1
        end if
        if x - size <= 0 then
            x := size
            x_vel *= -1
        end if
        if y + size >= maxy then
            y := maxy - size
            y_vel *= -1
        end if
        if y - size <= 0 then
            y := size
            y_vel *= -1
        end if
    end move

    procedure check_clicked (mx, my, mb : int)
        %check to see if the ball has been clicked
        if mx > x - size and mx < x + size and my > y - size and my < y + size and mb >= 1 then
            draw_ := false
        else
            draw_ := true
        end if
    end check_clicked

    proc draw
        if draw_ then
            Draw.FillOval (x, y, size, size, colur)
        end if
    end draw
end balls
%-----------------------------------------------------------------------------------------------
View.Set ("offscreenonly,graphics")

type bouncy :
    record
        instance : ^balls
        points_taken : boolean
    end record

var ball : flexible array 1 .. 0 of bouncy
var elements_to_be_removed : flexible array 1 .. 0 of int
var mouse_x, mouse_y, mouse_buttons, oldbutton : int := 0
var points, ball_amount : int := 0
var keys : array char of boolean
const ball_color : int := brightblue
const min_size : int := 10
const max_size : int := 20
const timer : int := 60000 %60 seconds (60 * 1000 ;) )
var tim : int := Time.Elapsed
var tim2 : int := Time.Elapsed - timer
%----------------------------------------------------------------------------------------------
procedure lose
    var font : int := Font.New ("times new roman:20:bold")
    Draw.Cls
    Font.Draw ("You Lose!!", maxx div 2 - 100, maxy - 25, font, brightred)
    locatexy (maxx div 2 - 100, maxy - 50)
    put "You created ", ball_amount, " balls, however you had ", upper (ball), " ball(s) left"
end lose

procedure win
    var font : int := Font.New ("times new roman:20:bold")
    Draw.Cls
    Font.Draw ("You Win!!", maxx div 2 - 150, maxy - 25, font, brightblue)
    locatexy (maxx div 2 - 150, maxy - 50)
    put "You created ", ball_amount, " balls and clicked all of them in 60 seconds"
    locatexy (maxx div 2 - 150, maxy - 75)
    put "giving you a score of ", points, " points, Congratulations."
end win

procedure check_click
    Mouse.Where (mouse_x, mouse_y, mouse_buttons)
    if mouse_buttons = 1 and oldbutton = 0 then
        for i : 1 .. upper (ball)
            oldbutton := 1
            ball (i).instance -> check_clicked (mouse_x, mouse_y, mouse_buttons)
        end for
    elsif mouse_buttons = 0 and oldbutton = 1 then
        oldbutton := 0
    end if
end check_click

procedure delete_ball (object_to_be_deleted : int)
    var temp : bouncy
    for lp : object_to_be_deleted .. upper (ball) - 1
        %for loop pushes the ball to be deleted to top of array to be deleted
        temp := ball (lp)
        ball (lp) := ball (lp + 1)
        ball (lp + 1) := temp
    end for
    free balls, ball (upper (ball)).instance
    new ball, upper (ball) - 1
end delete_ball

procedure new_ball (x, y, size, colur : int)
    new ball, upper (ball) + 1
    new balls, ball (upper (ball)).instance
    ball (upper (ball)).points_taken := false
    ball (upper (ball)).instance -> initialize (x, y, size, colur, Rand.Int (1, 7), Rand.Int (1, 7))
    ball_amount += 1
end new_ball
%-----------------------------------------------------------------------------------------------
%Main Program
new_ball (maxx div 2, maxy div 2, 20, ball_color)

loop
    Draw.Cls
    tim2 := Time.Elapsed - timer
    put "Balls: ", upper (ball), "                 Points: ", points, "                 Time: ", round ((tim - tim2) / 1000)
    put "Button: ", mouse_buttons, "        Old Button: ", oldbutton
    Input.KeyDown (keys)
    %create more balls by holding down the enter key
    if keys (KEY_ENTER) then
        new_ball (Rand.Int (max_size, maxx - max_size), Rand.Int (max_size, maxy - max_size), Rand.Int (min_size, max_size), ball_color)
        Input.Flush
    end if
    Time.DelaySinceLast (30)
    check_click
    new elements_to_be_removed, 0     %start an empty array
    for j : 1 .. upper (ball)
        %move and draw the ball
        ball (j).instance -> move
        ball (j).instance -> draw
        %the if not ball (j).instance -> draw_ is if the balls have been drawn and clicked on
        if not ball (j).instance -> draw_ and not ball (j).points_taken then
            %do this to stop points to keep being gained every iteration of the loop
            ball (j).points_taken := true
            %used for the delete_ball procedure because it screws up if i have delete_ball(j) in here
            new elements_to_be_removed, upper (elements_to_be_removed) + 1
            elements_to_be_removed (upper (elements_to_be_removed)) := j     %removes ball j in next for loop
            points += 10
        end if
    end for
    for i : 1 .. upper (elements_to_be_removed)
        delete_ball (elements_to_be_removed (i))
    end for
    %timer
    exit when tim - tim2 <= 0
    View.Update
end loop
if upper (ball) >= 1 then
    lose
else
    win
end if



ENJOY
Sponsor
Sponsor
Sponsor
sponsor
TokenHerbz




PostPosted: Sat Aug 12, 2006 7:20 am   Post subject: (No subject)

Hmm, Have it so you cant hold down the click button and shake the mouse around to clear them all.

Also, maybe have it in a level sequence.

Level one = 1 ball, Each progressing level can have +2 balls?

good tho
Clayton




PostPosted: Sun Aug 13, 2006 11:29 am   Post subject: (No subject)

ok so what could you do so that you cant just hold the mouse down? ive been trying to use the Mouse. commands but they arent getting me anywhere (specifically Mouse.ButtonMoved, and Mouse.ButtonChoose), as for the level thing thats what i was planning on doing, this is just a kind of basic engine for the game, im still kind of thinking about where to go with it from here:D
NikG




PostPosted: Mon Aug 14, 2006 1:48 pm   Post subject: (No subject)

SuperFreak82 wrote:
ok so what could you do so that you cant just hold the mouse down?
Add a new variable called mouse_buttons_2 or something, write "mouse_buttons_2 := mouse_buttons" right before your Mouse.Where procedure, and everywhere you're checking for whether the mouse is being clicked, just use "if mouse_buttons = 1 and mouse_buttons_2 = 0 then"
Ultrahex




PostPosted: Mon Aug 14, 2006 3:01 pm   Post subject: (No subject)

Ok, some people are gonna probably yell at me for saying this but anyhow...

You Should check to see when they click and when they do click run collision detection, its probably more effective then checking everytime.

If you would like me to make a coded example ask i will whip it up as fast as i can.

also are you removing the balls from the flexible array ? like after they have clicked on them or are you still running them through your loop?

the easiest way to remove the ball is do a main loop with quit like so...
(if this is confusing ill try to explain just give me a message)

code:

var ball : flexible array 1 .. 0 of boolean %Declaring the Flexible Array

%Create Random Information For Flexible Array
new ball, upper (ball) + 1
ball (upper (ball)) := true
new ball, upper (ball) + 1
ball (upper (ball)) := false
new ball, upper (ball) + 1
ball (upper (ball)) := true
new ball, upper (ball) + 1
ball (upper (ball)) := true
new ball, upper (ball) + 1
ball (upper (ball)) := false
new ball, upper (ball) + 1
ball (upper (ball)) := false
new ball, upper (ball) + 1
ball (upper (ball)) := false
new ball, upper (ball) + 1
ball (upper (ball)) := true


var currentBall : int := 1 %Declare The Start of Array
var maxBall : int := upper (ball) %Get Top Of Array
loop
    if (currentBall > maxBall) then     %If The Upcoming check is out of range
        exit %Exit Out Of Loop
    end if
    if (not ball (currentBall)) then     % if is false then [Add and Remove The not, you will notice that it works]
        ball (currentBall) := ball (upper (ball)) %Put the Top Ball In Its Location
        new ball, upper (ball) - 1     %Decrease Array Size (Deletes Duplicate Ball)
        maxBall -= 1     %Decrease Loop By 1 so it wont be array out of bounds
    else %this is added so that it will check the ball it moved to the one you overwrotes position (checks same number twice in reality)
        currentBall += 1     %add 1 to currentBall (To Go Check Next Ball In Loop since current number was satisified)
    end if
end loop

for i : 1 .. upper (ball)
    put "", ball (i)
end for


^ that code is untested btw, but it should work... ive written it many times lets just say
Ultrahex




PostPosted: Mon Aug 14, 2006 3:24 pm   Post subject: (No subject)

Ok, i got lazy and actually got around to writing a click point routine also, so when you click it adds up the clicks inside the area specified...

at the same time i noticed your collision detection is for squares instead of circles so i decided to help you there also, the first one is using your detection (i drew the square to prove the point)

code:

var circleClickCount : int := 0
var prevmb : int := 0
var r : int := 50 %radius
var cx, cy : int := 150
var mx, my, mb : int
loop
    Mouse.Where (mx, my, mb)
    Text.Locate (1, 1)
    put "button Status: ", mb, " at: ", mx, ",", my
    put "circle has been clicked ", circleClickCount, " times"
    drawfillbox (100, 100, 200, 200, brightred)
    drawfilloval (cx, cy, r, r, red)
    if (prevmb = 0) and (mb = 1) then
        if (mx > cx - r) and (mx < cx + r) and (my > cy - r) and (my < cy + r) then
            circleClickCount += 1
        end if
    end if
    prevmb := mb
    delay (50)
end loop


And here is it with proper collision detection

code:

var circleClickCount : int := 0
var prevmb : int := 0
var r : int := 50 %radius
var cx, cy : int := 150
var mx, my, mb : int
loop
    Mouse.Where (mx, my, mb)
    Text.Locate (1, 1)
    put "button Status: ", mb, " at: ", mx, ",", my
    put "circle has been clicked ", circleClickCount, " times"
    drawfilloval (cx, cy, r, r, red)
    if (prevmb = 0) and (mb = 1) then
        if (Math.Distance (150, 150, mx, my) < r) then
            circleClickCount += 1
        end if
    end if
    prevmb := mb
    delay (50)
end loop


Take what you will from it. any questions ask right ahead!
pj_ladd12




PostPosted: Sat Aug 19, 2006 4:43 pm   Post subject: (No subject)

i click it at 56 seconds and then i have to wait a whole minute fer the next ball
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: