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

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




PostPosted: Fri Mar 10, 2006 1:47 pm   Post subject: Escape game

This is a remake of the Dodge game where you move the red box around trying to avoid the blue boxes.
code:

%Remake of the escape game found at (among others) http://uk.download.yahoo.com/ne/fu/dodge.html
%By Chris Brown

View.Set ("graphics:450;450;offscreenonly;nobuttonbar")

const FRAMERATE : int := 2
const SPEEDCHANGE : int := 10
const font : int := Font.New("arial:12:bold")
%Mouse vars
var mx, my, mb : int

var DelayTime : int := 25

var timeElapsed : int := 0

var bestTime : int := 0

type playerType :
    record
        X : int
        Y : int
        Size : int
    end record
var player : playerType
player.Size := 40

type enemyType :
    record
        X : int
        Y : int
        XSize : int
        YSize : int
        currentXDir : int
        currentYDir : int
        minAllowedDist : int
    end record
var enemies : array 1 .. 4 of enemyType

procedure assignValues
player.X := -player.Size
player.Y := -player.Size
    for i : 1 .. 4
        enemies (i).XSize := 4 * (Rand.Int (8, 20))
        enemies (i).YSize := 4 * (Rand.Int (8, 20))
        enemies (i).X := Rand.Int (enemies (i).YSize div 2 + 5, maxx - enemies (i).XSize div 2 - 5)
        enemies (i).Y := Rand.Int (enemies (i).YSize div 2 + 5, maxy - enemies (i).YSize div 2 - 5)
        enemies (i).currentXDir := Rand.Int (-5, 5)
        enemies (i).currentYDir := Rand.Int (-5, 5)
        enemies (i).minAllowedDist := round (sqrt (((enemies (i).XSize div 2 + player.Size div 2) ** 2) + ((enemies (i).YSize div 2 + player.Size div 2) ** 2)))
    end for
end assignValues

procedure draw
    cls
    %Draw the background
    Draw.FillBox (0, 0, maxx, maxy, black)
    Draw.FillBox (50, 50, maxx - 50, maxy - 50, white)
     %Draw player box
    Draw.FillBox (player.X - player.Size div 2, player.Y - player.Size div 2, player.X + player.Size div 2, player.Y + player.Size div 2, red)
        %Draw the enemies
    for i : 1 .. 4
        Draw.FillBox (enemies (i).X - enemies (i).XSize div 2, enemies (i).Y - enemies (i).YSize div 2, enemies (i).X + enemies (i).XSize div 2, enemies (i).Y + enemies (i).YSize div 2, blue)
    end for   
    Font.Draw("Best Time: ", 0, 0, font, white)
    Font.Draw(realstr(bestTime/1000, 2), 90, 0, font, white)
    Font.Draw(realstr(timeElapsed /1000, 2), maxx - 50, 0, font, white)
    View.Update
end draw

procedure mouse
    mousewhere (mx, my, mb)
end mouse

procedure getPlayerLoc
    if mb = 1 then
        %Center the box at mouse point
        player.X := mx
        player.Y := my
    end if
end getPlayerLoc

procedure moveEnemies
    for i : 1 .. 4
        %Enemy location changes in FRAMERATE increment
        enemies (i).X += enemies (i).currentXDir * FRAMERATE
        enemies (i).Y += enemies (i).currentYDir * FRAMERATE
        if enemies (i).X - enemies(i).XSize div 2 < 0 or enemies (i).X+ enemies(i).YSize div 2> maxx then
            %If enemy hits side of screen
            enemies (i).currentXDir := -1 * enemies (i).currentXDir
            %Then enemy's direction changes as it should
            end if
        if enemies (i).Y - enemies(i).YSize div 2 < 0 or enemies (i).Y + enemies(i).YSize div 2 > maxy then
        %If enemy hits top or bottom of screen   
        enemies (i).currentYDir := -1 * enemies (i).currentYDir
            %Then enemy's direction changes as it should
        end if
    end for
end moveEnemies

function checkHit : boolean
    if whatdotcolor (player.X + player.Size div 2, player.Y + player.Size div 2) = blue or whatdotcolor (player.X - player.Size div 2, player.Y + player.Size div 2) = blue or whatdotcolor (player.X + player.Size div 2, player.Y - player.Size div 2) = blue or whatdotcolor (player.X - player.Size div 2, player.Y - player.Size div 2) = blue then
        result true
    elsif player.X - player.Size div 2 < 50 or player.X + player.Size div 2> maxx - 50 or player.Y - player.Size div 2< 50 or player.Y + player.Size div 2 > maxy - 50 then
        result true
    else
        result false
    end if
end checkHit

assignValues
loop
Font.Draw ("You lasted: ", 0, maxy - 14, font, white)
        Font.Draw (realstr(timeElapsed / 1000, 2), 100, maxy - 14, font, white)
        View.Update
        delay(1000)
DelayTime := 30
timeElapsed := 0
    assignValues
    loop
        mouse
        getPlayerLoc
        draw
        if mb = 1 then
            exit
        end if
    end loop

    loop
        mouse
        getPlayerLoc
        moveEnemies
        draw
        if checkHit then
            exit
        end if
        delay (DelayTime)
        timeElapsed += DelayTime
        if timeElapsed mod 5000 = 0 then
        DelayTime -= SPEEDCHANGE   
        end if
    end loop   
    if timeElapsed > bestTime then
        bestTime := timeElapsed
    end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
TokenHerbz




PostPosted: Fri Mar 10, 2006 4:16 pm   Post subject: (No subject)

Thats cool, But may i make a few suggestions?

I noticed a few times when i start i auto lose, cause the blue box spawns on me. Also, One round i played all the boxes seemed to not move well, one was still, other 3 where in one direction only, thus allowed me to get a high time, which actually didn't add to my high score, but have me a -time.

Other than that, i like it, i like how the blocked move faster over time.
[Gandalf]




PostPosted: Fri Mar 10, 2006 8:18 pm   Post subject: (No subject)

Nice game, and quite good code compared to what I have come to expect. Some suggestions on improving your code:

1 - Your types could become vars. For example:
code:
type enemyType :
...
var enemies : array 1 .. 4 of enemyType

Could become:
code:
var enemies : array 1 .. 4 of
...

2 - Your FRAMERATE variable does not seem to reflect the game's frame rate (frames per second), you should rename it to something more proper.
3 - No need for a "mouse" procedure when you only have one line inside it which reflects what is being done quite well.
4 - Statements like:
code:
        if mb = 1 then
            exit
        end if

Could become:
code:
exit when mb = 1

5 - Use Time.DelaySinceLast() instead of delay(). This ensures that your game runs more or less the same fast on all computers.
6 - Your code: timeElapsed += DelayTime is faulty, since it does not take into account how long it took the computer to process all the code in between. This is related to point 5. What you should have instead is something to the effect of:
code:
startTime := Time.Elapsed
%%%run the program here%%%
currentPlayTime := Time.Elapsed - startTime

7 - Why does DelayTime not follow your other naming convention? It should be delayTime.

I think I will leave it at that. Good job on the code and resulting game, now keep trying to improve. Smile
MysticVegeta




PostPosted: Sun Mar 12, 2006 4:11 pm   Post subject: (No subject)

Good game! Here is how to easily beat it and cheat in it

1> Press the button once to start the game and let it go
2> If blue boxes come near, click on the area of th emap where they arent, you are automatically transported there Razz

Also, check out Math.Distance for colliision, it wil save you 1 whole fcn and those messy ifs inside it Smile
cool dude




PostPosted: Sun Mar 12, 2006 5:17 pm   Post subject: (No subject)

MysticVegeta wrote:
Good game! Here is how to easily beat it and cheat in it

1> Press the button once to start the game and let it go
2> If blue boxes come near, click on the area of th emap where they arent, you are automatically transported there Razz

Also, check out Math.Distance for colliision, it wil save you 1 whole fcn and those messy ifs inside it Smile


thats not really cheating because u will lose very quickly. i tried a couple times doing that and i think its easier to just drag it but thats my opinion. anyways its a good game could be more efficient but good start Smile
MysticVegeta




PostPosted: Sun Mar 12, 2006 9:39 pm   Post subject: (No subject)

I dont think without doing that you can get 100+ can you?
person




PostPosted: Mon Mar 13, 2006 11:03 am   Post subject: (No subject)

MysticVegeta wrote:
I dont think without doing that you can get 100+ can you?


Don't the boxes accelerate, and like after 30 seconds or so, it gets to an insane speed?
cool dude




PostPosted: Mon Mar 13, 2006 11:40 am   Post subject: (No subject)

person wrote:
MysticVegeta wrote:
I dont think without doing that you can get 100+ can you?


Don't the boxes accelerate, and like after 30 seconds or so, it gets to an insane speed?


thats exactly my point. no matter how much i tried that cheat i can't get 100+ because it accelerates. plus i was better at the game when i could actually drag the boxes.
Sponsor
Sponsor
Sponsor
sponsor
MysticVegeta




PostPosted: Mon Mar 13, 2006 2:40 pm   Post subject: (No subject)

What I meant was I could easily get 100+ without dragging and clicking. You just have to locate empty spaces and when the boxes come near you, just click there, how hard is that? Confused
person




PostPosted: Mon Mar 13, 2006 2:46 pm   Post subject: (No subject)

MysticVegeta wrote:
What I meant was I could easily get 100+ without dragging and clicking. You just have to locate empty spaces and when the boxes come near you, just click there, how hard is that?


No offense, but have you tried getting to 100?

PS: Read your post again.
MysticVegeta




PostPosted: Mon Mar 13, 2006 6:42 pm   Post subject: (No subject)

Why else would I say that?
cool dude




PostPosted: Mon Mar 13, 2006 11:16 pm   Post subject: (No subject)

in my opinion its harder but thats just my opinion. if it matters so much its an easy fix. oh and u could have changed the code to say that. lol. if u didn't thats really good because i couldn't get up so high. i'm bad at these kinda games Sad
MysticVegeta




PostPosted: Tue Mar 14, 2006 6:38 pm   Post subject: (No subject)

Why would I change the code? I am not that big a loser Razz
cool dude




PostPosted: Wed Mar 15, 2006 11:24 am   Post subject: (No subject)

i was just joking! Smile but ya thats really good!
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  [ 14 Posts ]
Jump to:   


Style:  
Search: