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

Username:   Password: 
 RegisterRegister   
 Check my code -- I cant get it to run smoothly
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Khrist




PostPosted: Fri Apr 15, 2005 8:10 am   Post subject: Check my code -- I cant get it to run smoothly

ok, ive been playing around with some things and im just learning how to use procedures and processes. I am just making this fun little game where you have 2 players on a team and they will shoot down enemies. I am only in the very primal states of the game but im having problems with the shooting. If i try and shoot, it either erases my guy, shoots off 10 bullets and once, or my guy cannot move until the bullet is gone. If anyone can help me here, im sure its probably something like an organization problem. Anyways heres the code:
code:
View.Set ("graphics:640;400,offscreenonly")


var x, y, x1, y1, pic, dir, bx1, by1, byy, bxx : int
x := 60
y := 390
x1 := x - 5
y1 := y - 5
var pic1, pic2, pic3, pic4, bg, bhp, baddie, dmg, pic5, pic6, pic7, pic8 : int
var db1 : real
baddie := Pic.FileNew ("baddie.jpg")
bhp := 200
bx1 := 300
by1 := 200
dmg := 10
var chars : array char of boolean
bg := Pic.FileNew ("bg01.jpg")
pic1 := Pic.FileNew ("pic0.jpg")
pic2 := Pic.FileNew ("pic90.jpg")
pic3 := Pic.FileNew ("pic180.jpg")
pic4 := Pic.FileNew ("pic270.jpg")
pic5 := Pic.FileNew ("pic45.jpg")
pic6 := Pic.FileNew ("pic135.jpg")
pic7 := Pic.FileNew ("pic225.jpg")
pic8 := Pic.FileNew ("pic315.jpg")
pic := pic1
var maxd : int := 10
var hit : int := 5
var bdmg : int
var hp : int := 100

%%%%%%LOOP%%%%%%
proc vars
    x := x1 + 10
    y := y1 + 10
    bxx := bx1 + 10
    byy := by1 + 10
    db1 := Math.Distance (x, y, bxx, byy)
end vars

proc draw
    Pic.Draw (bg, 0, 0, 0)
    if bhp > 0 then
        Pic.Draw (baddie, bx1, by1, picMerge)
    end if
end draw

proc controls
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) then
        if whatdotcolor (x, y + 11) = 0 then

            pic := pic1
            dir := 1
            y1 += 2

        end if
    end if

    if chars (KEY_RIGHT_ARROW) then
        if whatdotcolor (x + 11, y) = 0 then

            pic := pic2
            dir := 2
            x1 += 2

        end if
    end if
    if chars (KEY_DOWN_ARROW) then
        if whatdotcolor (x, y - 11) = 0 then

            pic := pic3
            dir := 3
            y1 -= 2

        end if
    end if
    if chars (KEY_LEFT_ARROW) then
        if whatdotcolor (x - 11, y) = 0 then

            pic := pic4
            dir := 4
            x1 -= 2

        end if
    end if
end controls

proc bulletd1
    var bd1x : int := x
    var bd1y : int := y
    loop
        Pic.Draw (pic, x1, y1, picMerge)
        drawdot (bd1x, bd1y, black)
        drawdot (bd1x, bd1y - 1, white)
        bd1y += 1

        delay (20)
        View.Update
        if Math.Distance (bd1x, bd1y, bxx, byy) < 20 then
            bhp := bhp - dmg
        end if
        exit when bd1y = (y + 150)
        exit when Math.Distance (bd1x, bd1y, bxx, byy) < 19
    end loop

end bulletd1




/*
 proc ai
 if db1 < 100 then
 bx1 := bx1 + Rand.Int (-3, 3)
 by1 := by1 + Rand.Int (-3, 3)
 delay (100)
 end if
 if db1 < 50 then
 if Rand.Int (1, 5) = 1 then
 drawline(bxx,byy,x,y,red)
 bdmg := Rand.Int(1,5)
 hp := hp - bdmg
 delay(100)
 end if
 %delay(1000)
 end if

 end ai
 */


proc fight

    if chars ('a') and dir = 1 then
        bulletd1
        delay (200)
    end if


end fight


proc update


    Pic.Draw (pic, x1, y1, picMerge)
    View.Update
    delay (10)

    %cls
end update

loop
    vars
    draw
    controls
    %ai
    fight
    update
end loop
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Fri Apr 15, 2005 8:17 am   Post subject: (No subject)

You do realise that if we're to 'check your code', we would actually have to be able to run it to debug it. No, we're not going to sift through it and try read it off...
In other words, this is not going any where w/out those pics you've got as part of it.
But wait! There's more.

How about instead of posting all the code, you post the parts you think are screwing things up. Again, ensure that this code will run w/ the press of F1.

Though just from glancing over it, you might be in need of some cls' here and there...again, I can't tell unless I can run it.
jamonathin




PostPosted: Fri Apr 15, 2005 8:24 am   Post subject: (No subject)

Yes, and please dont post the pictures individually. Put the program and the pictures into a zip/rar file.

But by looking at your program, I suggest your main loop look like this:
code:

loop
cls
   vars
    draw
    controls
    %ai
    fight
Pic.Draw (pic, x1, y1, picMerge)
View.Update
delay(10)
end loop
c0bra54




PostPosted: Wed Apr 20, 2005 12:39 am   Post subject: (No subject)

also, if u are doing a shooter, you will be tempted (sice it is soooo easy) to do a simple delay (x) for your time delay.. but don't!!!

that is the worst, because if your main loop takes longer the execute, it'll still wait the same length.. what you need is (i call it this) FPS limiting code.. or code that sets a max for the FPS of your game, and it will always be that..

the code looks something like this.. put it at the VERY end of your main loop righ before the View.Update, or it won't work right..

code:

const MAX_FRAME_RATE : int := 30
const MIN_MILLISECS_BETWEEN_FRAMES : int := 1000 div MAX_FRAME_RATE




var lastFrameTime : int := 0
var frameTime : int := 0


%proc for it
proc tdelay
    if Time.Elapsed - lastFrameTime < MIN_MILLISECS_BETWEEN_FRAMES then
        delay (MIN_MILLISECS_BETWEEN_FRAMES - (Time.Elapsed - lastFrameTime))
    end if
    lastFrameTime := Time.Elapsed
end tdelay


so your main loop would be

loop
cls
hasdkjhad
asd
asd


tdelay


View.Update


your gonna have to change the max FPS to make sure italways works.. and yeh u probly have some errors in code.. every program always does :)

enjoy.. and post an update, w/ pics, and ready to go vers for some Bits :D
Bacchus




PostPosted: Wed Apr 20, 2005 3:27 pm   Post subject: (No subject)

or just use Time.DeaySiceLast()
Martin




PostPosted: Wed Apr 20, 2005 4:31 pm   Post subject: (No subject)

Please make a more descriptive title for your topic, Khrist.
Cervantes




PostPosted: Wed Apr 20, 2005 4:32 pm   Post subject: (No subject)

jamonathin wrote:
Yes, and please dont post the pictures individually. Put the program and the pictures into a zip/rar file.

But by looking at your program, I suggest your main loop look like this:
code:

loop
cls
   vars
    draw
    controls
    %ai
    fight
Pic.Draw (pic, x1, y1, picMerge)
View.Update
delay(10)
end loop

That's an akward loop structure. First off, you should not have any variable declaration within your loop. Move all that before the loop. Secondly, the cls should go with the drawing, just to keep things organized. the draw procedure should then also go with the drawing (at the end) to keep things organized. Lastly, I like to go with the whole input -> processing -> output approach, so I'd go like this:
code:

vars
loop
  input %(controls)

  ai
  fight

  cls
  draw
  Pic.Draw (pic, x1, y1, picMerge)
  View.Update
  Time.DelaySinceLast (5)
end loop

I'm not sure what ai and fight do though, so perhaps they should go elsewhere. Hopefully you get my idea, though.
c0bra54




PostPosted: Wed Apr 20, 2005 8:01 pm   Post subject: (No subject)

and about Time.DelaySinceLast... well some ppl still have 4.0.4 since they can't get there hands on 4.0.5, plus my Teacher has the old one so Razz
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: