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

Username:   Password: 
 RegisterRegister   
 Help with racing game
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Ldomi




PostPosted: Fri Jan 05, 2007 3:02 pm   Post subject: Help with racing game

Hi, I'm trying to make a racing game but I'm running into a lot of problems...Can anyone help me?

Problem 1: How do i get the computer car to move by itself at the same time the other car that is being controlled by the user is moving? (Someone told me to use Input.KeyDown instead of getch but i don't really understand how that solves the problem)

Problem 2:
When I play again, why does the car move slower when it reaches a certain point on the screen?

Problem 3:Why doesn't the Main Menu Button in the displayEasy procedure work?

I'd appreciate any help you can give. (I'd also like some suggestions for the game if anyone has any) Thanks in advance. =D

Here's the code so far...
code:
%*********
%Ms Dyke
%December 27 2006
%

%set screen up
import GUI
setscreen ("nocursor")
setscreen ("noecho")
%setscreen ("position:130;30,graphics:800;600")
setscreen ("offscreenonly")

%Declaration Section
%var startpointx, startpointy, endpointx, endpointy : int
var startpointx, endpointx, endpointy : int := 0
var startpointy : int := 150
var startpointy2 : int := 400
var endpointy2 : int := 450
var pictureID : int
var key : string (1)
%var mainMenuBtn : int := 0
var mainMenuBtn : int := 0
var mainWin := Window.Open ("position:130;30,graphics:800;600")

%Procedures:
%Procedure Title      Purpose: Proc title already contains a title so programmer only has to type the name of the proc to input a title on a screen.
process DoMusic
    loop
        Music.PlayFile ("mario_circuit.mid")
    end loop
end DoMusic

procedure title
    cls
    locate (1, 44)
    put "Racing Game"
    put " "
end title

%Proc pauseProgram
proc pauseProgram
    put " "
    put "Press any key to continue..."
    getch (key)
end pauseProgram

%Proc introduction
procedure introduction
    title
    locate (3, 1)
    put "Do you think you're the best at racing games? Test your skills with the Racing Game!"
    GUI.Show (mainMenuBtn)
    loop
        exit when GUI.ProcessEvent
    end loop
end introduction

%Proc display
proc display
    title
    locate (17, 37)
    put "Congratulations! You WIN!"
    GUI.Show (mainMenuBtn)
    GUI.Enable (mainMenuBtn)
    loop
        exit when GUI.ProcessEvent
    end loop
end display

%Proc displayandUserInput
proc displayEasy
    title
    % drawfillbox (0,0,800,580,77)
    pictureID := Pic.FileNew ("BG1.JPG")

    Pic.Draw (pictureID, 0, 60, 0)

    pictureID := Pic.FileNew ("Comp.jpg")
    Pic.Draw (pictureID, 1, 400, 0)

    pictureID := Pic.FileNew ("P1.jpg")
    Pic.Draw (pictureID, 1, 151, 0)
    locate (4, 44)
    %colourback (77)
    put "3.." ..
    delay (500)
    put "2.." ..
    delay (500)
    put "1.." ..
    delay (500)
    put "GO!" ..
    Pic.Free (pictureID)

   % exit when GUI.ProcessEvent
    pictureID := Pic.FileNew ("BG1.JPG")
    Pic.Draw (pictureID, 0, 60, picCopy)

   % drawfillbox (300, 350, 350, 400, 7)
loop
    pictureID := Pic.FileNew ("Comp.jpg")

        drawfillbox (startpointx + 1, 400 + 1, 0 + 1, 450 + 1, grey)

        Pic.Draw (pictureID, startpointx, startpointy2, picMerge)

        startpointx := startpointx + 1
       % delay (20)
        if startpointx > 630 then
            display
        end if
   

   

        pictureID := Pic.FileNew ("P1.jpg")

        drawfillbox (startpointx + 1, 70 + 1, 0 + 1, 300 + 1, gray)
       %  drawfillbox (startpointx+1, startpointy+70, endpointx + 1, endpointy + 300, yellow)
   % drawfillbox (startpointx+1, startpointy+100, endpointx + 150, endpointy + 150, yellow)
   
        Pic.Draw (pictureID, startpointx + 1, startpointy + 1, picMerge)
        getch (key)

        if key = chr (200) and endpointy <= 300 then
            startpointy := startpointy + 10
            endpointy := endpointy + 10
        elsif key = chr (208) and startpointy >= 80 then
            startpointy := startpointy - 10
            endpointy := endpointy - 10
        elsif key = chr (205) and endpointx <= 630  then
            startpointx := startpointx + 10
            endpointx := endpointx + 10
        else
            if key = chr (203) and startpointx >= 1 then
                startpointx := startpointx - 10
                endpointx := endpointx - 10
     
            elsif startpointx > 600 then
           % delay (500)
            display
            end if
        end if
         GUI.Show (mainMenuBtn)
    GUI.Enable (mainMenuBtn)
    end loop
   
    View.Update
    loop
        exit when GUI.ProcessEvent
    end loop
   
end displayEasy

%proc displayIntermediate

%Proc Controls
proc controls
    title
    locate (8, 1)
    put "Use the arrow keys on your keyboard to move the car in different directions." ..
    put " "
    put " "
    put "Forward = Right Arrow Key" ..
    put " "
    put "Backward = Left Arrow Key" ..
    put " "
    put "Left = Up Arrow Key" ..
    put " "
    put "Right = Down Arrow Key" ..
    GUI.Show (mainMenuBtn)
    GUI.Enable (mainMenuBtn)
    loop
        exit when GUI.ProcessEvent
    end loop

end controls


%Proc mainMenu
proc mainMenu
    title
    startpointx := 0
    pictureID := Pic.FileNew ("racing_flags.jpg")
    Pic.Draw (pictureID, 230, 250, 0)
    GUI.Disable (mainMenuBtn)
    GUI.Hide (mainMenuBtn)
    locate (9, 25)
    put "Levels:" ..
    var easyBtn : int := GUI.CreateButtonFull (300, 450, 190, "Easy", displayEasy, 10, '^F', false)
    var intermediateBtn : int := GUI.CreateButtonFull (300, 420, 190, "Intermediate", displayEasy, 10, '^G', false)
    var hardBtn : int := GUI.CreateButtonFull (300, 390, 190, "Hard", displayEasy, 10, '^S', false)
    var controlsBtn : int := GUI.CreateButtonFull (300, 320, 190, "Controls", controls, 10, '^H', false)
    var quitBtn : int := GUI.CreateButtonFull (300, 250, 190, "Quit", GUI.Quit, 10, '^T', false)
    loop
        exit when GUI.ProcessEvent
    end loop
end mainMenu

%Proc goodBye
proc goodBye
    title
    locate (17, 29)
    put "Hope you had fun playing the Racing Game!"
    locate (18, 28)
    put "For more information visit: www.racing.com"
    locate (37, 1)
    put "This program was written by: Lucille Domingo" ..
    delay (3000)
    Window.Close (mainWin)
end goodBye

mainMenuBtn := GUI.CreateButtonFull (700, 1, 100, "Main Menu", mainMenu, 25, '^M', false)

%Main Program
fork DoMusic
introduction
loop
    exit when GUI.ProcessEvent
end loop
goodBye



racing_flags.jpg
 Description:
 Filesize:  9.49 KB
 Viewed:  1468 Time(s)

racing_flags.jpg



P1.JPG
 Description:
 Filesize:  3.87 KB
 Viewed:  1470 Time(s)

P1.JPG



Comp.JPG
 Description:
 Filesize:  3.35 KB
 Viewed:  1469 Time(s)

Comp.JPG



BG1.JPG
 Description:
 Filesize:  21.15 KB
 Viewed:  1469 Time(s)

BG1.JPG


Sponsor
Sponsor
Sponsor
sponsor
Ldomi




PostPosted: Fri Jan 05, 2007 3:04 pm   Post subject: (No subject)

I forgot, don't mind if the quit button doesn't work.
BenLi




PostPosted: Fri Jan 05, 2007 3:39 pm   Post subject: (No subject)

the answer to one of your question: The advantage Input.KeyDown has over getch is that it can check for multiple keys pressed. This is extremely important in a racing game where the user may need to accelerate and turn at the same time.
Ldomi




PostPosted: Fri Jan 05, 2007 3:56 pm   Post subject: (No subject)

BenLi wrote:
This is extremely important in a racing game where the user may need to accelerate and turn at the same time.


Thanks and I agree, but in my racing game, the user does not necessarily have to turn because all they really have to do to win is go in a straight line (I made my game really noobish =P). However, if i later on decide to put rocks or other obstacles on the game to actually make it fun, then i'll use input.keydown. For now i'll just stick with getch.
neufelni




PostPosted: Fri Jan 05, 2007 5:13 pm   Post subject: (No subject)

The reason that you should use Input.KeyDown instead of getch is that with getch, the program waits until the user enters something before it will do anything else. With Input.KeyDown, the program will keep doing other stuff while waiting for input. So in your racing game, if you use getch the other car won't move until you hit a key, then it will move 1(or howmanyever) pixels, and then wait for you to hit a key again. With Input.KeyDown, the other car will move normally even if you don't hit a key right aways.
Here is an example to show the difference:

Using Getch
code:
var key : string (1)
loop
    getch (key)
    if key = KEY_UP_ARROW then
        put "You pressed the Up arrow key"
    end if
    put "Hello!"
end loop


Using Input.KeyDown
code:
var key : array char of boolean
loop
    Input.KeyDown (key)
    if key (KEY_UP_ARROW) then
        put "You pressed the Up arrow key"
    end if
    put "Hello!"
end loop


Output



InputKeyDown.jpg
 Description:
 Filesize:  34.88 KB
 Viewed:  1471 Time(s)

InputKeyDown.jpg



getch.jpg
 Description:
 Filesize:  26.22 KB
 Viewed:  1470 Time(s)

getch.jpg


Ldomi




PostPosted: Sat Jan 06, 2007 1:37 am   Post subject: (No subject)

i tried using Input.keydown but all it is doing is making the blue car move using the arrow keys and also move by itself, not making the blue car move by itself and the yellow car move using the arrow keys at the same time.

code:
%Lucille Domingo
%Ms Dyke
%December 27 2006
%

%set screen up
import GUI
setscreen ("nocursor")
setscreen ("noecho")
%setscreen ("position:130;30,graphics:800;600")
setscreen ("offscreenonly")

%Declaration Section
%var startpointx, startpointy, endpointx, endpointy : int
var startpointx, endpointx, endpointy : int := 0
var startpointy : int := 150
var startpointy2 : int := 400
var endpointy2 : int := 450
var pictureID : int
var key : array char of boolean
%var mainMenuBtn : int := 0
var mainMenuBtn : int := 0
var mainWin := Window.Open ("position:130;30,graphics:800;600")

%Procedures:
%Procedure Title      Purpose: Proc title already contains a title so programmer only has to type the name of the proc to input a title on a screen.
process DoMusic
    loop
        Music.PlayFile ("mario_circuit.mid")
    end loop
end DoMusic

procedure title
    cls
    locate (1, 44)
    put "Racing Game"
    put " "
end title

%Proc pauseProgram
proc pauseProgram
    put " "
    put "Press any key to continue..."
%    getch (key)
end pauseProgram

%Proc introduction
procedure introduction
    title
    locate (3, 1)
    put "Do you think you're the best at racing games? Test your skills with the Racing Game!"
    GUI.Show (mainMenuBtn)
    loop
        exit when GUI.ProcessEvent
    end loop
end introduction

%Proc display
proc display
    title
    locate (17, 37)
    put "Congratulations! You WIN!"
    GUI.Show (mainMenuBtn)
    GUI.Enable (mainMenuBtn)
    loop
        exit when GUI.ProcessEvent
    end loop
end display

%Proc displayandUserInput
proc displayEasy
    title
    % drawfillbox (0,0,800,580,77)
    pictureID := Pic.FileNew ("BG1.JPG")

    Pic.Draw (pictureID, 0, 60, 0)

    pictureID := Pic.FileNew ("Comp.jpg")
    Pic.Draw (pictureID, 1, 400, 0)

    pictureID := Pic.FileNew ("P1.jpg")
    Pic.Draw (pictureID, 1, 151, 0)
    locate (4, 44)
    %colourback (77)
    put "3.." ..
    delay (500)
    put "2.." ..
    delay (500)
    put "1.." ..
    delay (500)
    put "GO!" ..
    Pic.Free (pictureID)

   % exit when GUI.ProcessEvent
    pictureID := Pic.FileNew ("BG1.JPG")
    Pic.Draw (pictureID, 0, 60, picCopy)

   % drawfillbox (300, 350, 350, 400, 7)

   

        pictureID := Pic.FileNew ("P1.jpg")

        drawfillbox (startpointx + 1, 70 + 1, 0 + 1, 300 + 1, gray)
       %  drawfillbox (startpointx+1, startpointy+70, endpointx + 1, endpointy + 300, yellow)
   % drawfillbox (startpointx+1, startpointy+100, endpointx + 150, endpointy + 150, yellow)
   
        Pic.Draw (pictureID, startpointx + 1, startpointy + 1, picMerge)
       loop
         Input.KeyDown (key)

        if key (KEY_UP_ARROW) and endpointy <= 300 then
            startpointy := startpointy + 10
            endpointy := endpointy + 10
        elsif key (KEY_DOWN_ARROW) and startpointy >= 80 then
            startpointy := startpointy - 10
            endpointy := endpointy - 10
        elsif key (KEY_RIGHT_ARROW) and endpointx <= 630  then
            startpointx := startpointx + 10
            endpointx := endpointx + 10
        else
            if key (KEY_UP_ARROW) and startpointx >= 1 then
                startpointx := startpointx - 10
                endpointx := endpointx - 10
     
            elsif startpointx > 600 then
            delay (500)
            display
            end if
        end if
        drawfillbox (startpointx + 1, 400 + 1, 0 + 1, 450 + 1, grey)
       
pictureID := Pic.FileNew ("Comp.jpg")

        Pic.Draw (pictureID, startpointx, startpointy2, picMerge)

        startpointx := startpointx + 1
        delay (50)
        if startpointx > 630 then
            display
        end if
         %GUI.Show (mainMenuBtn)
    %GUI.Enable (mainMenuBtn)
   end loop
   
    View.Update
    loop
        exit when GUI.ProcessEvent
    end loop
   
end displayEasy

%proc displayIntermediate

%Proc Controls
proc controls
    title
    locate (8, 1)
    put "Use the arrow keys on your keyboard to move the car in different directions." ..
    put " "
    put " "
    put "Forward = Right Arrow Key" ..
    put " "
    put "Backward = Left Arrow Key" ..
    put " "
    put "Left = Up Arrow Key" ..
    put " "
    put "Right = Down Arrow Key" ..
    GUI.Show (mainMenuBtn)
    GUI.Enable (mainMenuBtn)
    loop
        exit when GUI.ProcessEvent
    end loop

end controls

%Proc goodBye
proc goodBye
    title
    locate (17, 29)
    put "Hope you had fun playing the Racing Game!"
    locate (18, 28)
    put "For more information visit: www.racing.com"
    locate (37, 1)
    put "This program was written by: Lucille Domingo" ..
    delay (3000)
    Window.Close (mainWin)
end goodBye

%Proc mainMenu
proc mainMenu
    title
    startpointx := 0
    pictureID := Pic.FileNew ("racing_flags.jpg")
    Pic.Draw (pictureID, 230, 250, 0)
    GUI.Disable (mainMenuBtn)
    GUI.Hide (mainMenuBtn)
    locate (9, 25)
    put "Levels:" ..
    var easyBtn : int := GUI.CreateButtonFull (300, 450, 190, "Easy", displayEasy, 10, '^F', false)
    var intermediateBtn : int := GUI.CreateButtonFull (300, 420, 190, "Intermediate", displayEasy, 10, '^G', false)
    var hardBtn : int := GUI.CreateButtonFull (300, 390, 190, "Hard", displayEasy, 10, '^S', false)
    var controlsBtn : int := GUI.CreateButtonFull (300, 320, 190, "Controls", controls, 10, '^H', false)
    var quitBtn : int := GUI.CreateButtonFull (300, 250, 190, "Quit", GUI.Quit, 10, '^T', false)
    loop
        exit when GUI.ProcessEvent
    end loop
end mainMenu



mainMenuBtn := GUI.CreateButtonFull (700, 1, 100, "Main Menu", mainMenu, 25, '^M', false)

%Main Program
fork DoMusic
introduction
loop
    exit when GUI.ProcessEvent
end loop
goodBye


^^here's the code using inpuy.keydown
neufelni




PostPosted: Sat Jan 06, 2007 10:45 am   Post subject: (No subject)

OK, so here are a few things that you should change in your code.

1. Instead of declaring one pictureID variable at the the beginning of your program and then assigning it to a different picture each time you need to draw a pic, declare one for each of your pictures at the beginnig. Like this:
code:
var pictureP1 : int := Pic.FileNew ("P1.jpg")
var pictureComp : int := Pic.FileNew ("Comp.jpg")
var pictureBG : int := Pic.FileNew ("BG1.JPG")
var pictureRacing_Flags : int := Pic.FileNew ("racing_flags.jpg")

And then when you need to draw the picture, alls you need to do is this:

code:
Pic.Draw (pictureP1, 1, 151, 0)


2. You need to bring the Pic.Draw to draw your yellow car inside the loop. This way the yellow car will move too when you press the arrow keys.

3. You need to have separate variables for the p1 car and the comp car. You are using the same x variables for both, that is why when you hit the arrow keys the blue car moves faster.

So give those things a try and if you have any problems, let me know.
ericfourfour




PostPosted: Sat Jan 06, 2007 5:56 pm   Post subject: (No subject)

code:
var pictureRacing_Flags : int := Pic.FileNew ("racing_flags.jpg")

It is annoying when I post a link to style guidelines and then no one follows them. Either choose between picture_racing_flags or pictureRacingFlags but do not mix the two together. You are just going to confuse people.
Sponsor
Sponsor
Sponsor
sponsor
Ldomi




PostPosted: Sat Jan 06, 2007 6:21 pm   Post subject: (No subject)

Thank you very very much Nick! Very Happy

I have a few problems with delay, but i'll try solving them first.
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  [ 9 Posts ]
Jump to:   


Style:  
Search: