
-----------------------------------
Gooie
Fri Nov 02, 2007 10:28 pm

Game Speed Trouble?!?!
-----------------------------------
I am just starting out with Turing, and wanted to do a simple game, so of course Pong came to mind. I worked on it on and off for a bout a month, and figured out collision. But, when I got it done. I can't seem to fine tune the speed. Because depending on the speed of the computer the game is faster or slower. How would I go about changing that? Here's the source. Also any idea how to make my code more efficient, or better? I'll consider all sugestions. 

%--------------------Pong V1.0--------------------%
%-This software is licensed under the CC-GNU GPL.-%
%-------------------Mackie Drew-------------------%
%--------------------09/21/2007-------------------%

% Graphic Modes
View.Set ("offscreenonly,nobuttonbar,title:Pong")

% Define Variables
var key : array char of boolean
var right_x, right_y, left_x, left_y, ball_x, ball_y, speed, ball_dir, font, left_points, right_points : int

% Variable Assignments
left_x := 0
left_y := 170
right_x := 629
right_y := 170
ball_x := Rand.Int (100, 540)
ball_y := Rand.Int (100, 240)
speed := 1
left_points := 0
right_points := 0
ball_dir := Rand.Int (0, 3)
font := Font.New ("serif:36")

% Main Game Loop
loop

    % Draw Objects
    Draw.FillOval (ball_x, ball_y, 4, 4, black)
    Draw.FillBox (right_x, right_y, right_x + 10, right_y + 60, black)
    Draw.FillBox (left_x, left_y, left_x + 10, left_y + 60, black)
    Draw.Line (0, maxy - 18, maxx, maxy - 18, 1)
    locate (1, 2)
    put left_points
    locate (1, 79)
    put right_points
    
    /*
     Direction Map
     0 = Right Up
     1 = Right Down
     2 = Left Down
     3 = Left Up
     */

    % Ball Movement
    if ball_dir = 0 then % Right Up

        ball_x += speed
        ball_y += speed

        if ball_y > right_y and ball_y < right_y + 60 and ball_x + 5 = maxx - 10 and (ball_dir = 0 or ball_dir = 1) then
            ball_dir := 3
        elsif ball_y = maxy - 24 then
            ball_dir := 1
        elsif ball_x = maxx then
            ball_dir := 3
        end if

    elsif ball_dir = 1 then % Right Down

        ball_x += speed
        ball_y -= speed

        if ball_y > right_y and ball_y < right_y + 60 and ball_x + 5 = maxx - 10 and (ball_dir = 0 or ball_dir = 1) then
            ball_dir := 2

        elsif ball_y = 0 then
            ball_dir := 0

        elsif ball_x = maxx then
            ball_dir := 2
        end if

    elsif ball_dir = 2 then % Left Down

        ball_x -= speed
        ball_y -= speed

        if ball_y > left_y and ball_y < left_y + 60 and ball_x = 10 and (ball_dir = 2 or ball_dir = 3) then
            ball_dir := 1
        elsif ball_x = 0 then
            ball_dir := 1
        elsif ball_y = 0 then
            ball_dir := 3
        end if

    elsif ball_dir = 3 then % Left Up

        ball_x -= speed
        ball_y += speed

        if ball_y = maxy - 24 then
            ball_dir := 2

        elsif ball_x = 0 then
            ball_dir := 0

        elsif ball_y > left_y and ball_y < left_y + 60 and ball_x = 10 and (ball_dir = 2 or ball_dir = 3) then
            ball_dir := 0
        end if

    end if

    % Point System
    if ball_x = 0 then
        right_points += 1
    end if

    if ball_x = maxx then
        left_points += 1
    end if

    % Ask For Keys
    Input.KeyDown (key)

    % Left Paddle Movement Keys
    if key (chr (119)) and left_y < maxy - 80 then     % if key W

        left_y += speed

    elsif key (chr (115)) and left_y > 0 then    % if key S

        left_y -= speed

    end if

    % Right Paddle Movement Keys
    if key (KEY_UP_ARROW) and right_y < maxy - 80 then   % if key up arrow

        right_y += speed

    elsif key (KEY_DOWN_ARROW) and right_y > 0 then     % if key down arrow

        right_y -= speed

    end if

    %Update Graphics
    View.Update
    Draw.Cls
    delay (4)
    exit when right_points = 12 or left_points = 12
end loop

if right_points = 12 then
    Draw.Text ("RIGHT PLAYER WINS!", 70, 190, font, black)
elsif left_points = 12 then
    Draw.Text ("LEFT PLAYER WINS!", 100, 190, font, black)
end if
[/quote]

-----------------------------------
CodeMonkey2000
Fri Nov 02, 2007 10:34 pm

RE:Game Speed Trouble?!?!
-----------------------------------
This code is crying for a collide function. But I doubt you know about functions. About the speed try replacing Draw.Cls with Time.DelaySinceLast(round(1000/65)) for a smooth 65 frames per second.

-----------------------------------
Gooie
Fri Nov 02, 2007 10:49 pm

Re: RE:Game Speed Trouble?!?!
-----------------------------------
This code is crying for a collide function. But I doubt you know about functions. About the speed try replacing Draw.Cls with Time.DelaySinceLast(round(1000/65)) for a smooth 65 frames per second.

Awesome! Thanks! Time.DelaySinceLast(round(1000/65)) fixed the problem. Now, how would I go about using these collide functions? I really need a simpler way to make collision.

-----------------------------------
CodeMonkey2000
Sat Nov 03, 2007 12:05 am

RE:Game Speed Trouble?!?!
-----------------------------------
You have to create your own collide function. Look up functions on the turing walkthrough. You already know how to look for collisions, so apply it in a function.
