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

Username:   Password: 
 RegisterRegister   
 Help me comprehend some (SIMPLE) Pong coding
Index -> Programming, Turing -> Turing Help
Goto page Previous  1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Aange10




PostPosted: Mon Sep 05, 2011 2:59 pm   Post subject: Re: Help me comprehend some (SIMPLE) Pong coding

I've spent half of last night and nearly all day today trying to figure out why my timer isn't working. I've taken what you've said, and took in to account what the guy a couple posts above me said, and I came up with this code.
Turing:

View.Set("offscreenonly, graphics:maxx;maxxy, nocursor")
var mainmenu : boolean % Using this varriable to tell wether or not to pull up the main menu.
var fontTNR : int % A to allow me to make a font style into a variable
var mainmenuexit : string % The varriable i am using to exit the main meny/ skip past it
var oval_x1,oval_y1,oval_xr1,oval_yr1 : int % I made the drawoval syntax as variables
var oval_speed : int %This is to set my oval's speed
var oval_directionx, oval_directiony : int % To set my ovals trajectory direction
var box1_x1, box1_x2, box1_y1, box1_y2 : int
var chars : array char of boolean % Not sure what this is, just know you have to have it to use keys
var continue : string % Get the y/n asking if the player would like to continue
var score : int % Keeps track of the score
var timesincethen, timestart, timenow : real % Trying to reset timer...
oval_x1 := 70      % the x cord for the ball
oval_y1 := 70      % the y cord for the ball
oval_xr1 := 10     % the radius x for the ball
oval_yr1 := 10     % the radius y for the ball
oval_speed := 2    % the speed of the ball pfp (Pixles per frame ;) )
oval_directionx := 1 % Direction of the x cord of the oval (1 = right, -1 = left)
oval_directiony := 1  % direction of the y cord of the oval ( 1 = up, -1 = down)
box1_x1 := 85 % Box x1 cord
box1_x2 := 200 % box x2 cord
box1_y1 := 20 % box y1 cord
box1_y2 := 40 % box y2 cord
mainmenu := true %a varriable to control the main menu
continue := "y" %a varriable to tell wether or not to continue
score := 0
timestart := Time.Elapsed
loop % Main loop
    cls
      Input.KeyDown (chars)
        loop % main menu debugging loop
            if mainmenu = true then
            View.Set("nooffscreenonly, graphics:maxx;maxxy")
                fontTNR := Font.New ("TimeNewRoman:14")
                Font.Draw ("Game O' Paddle Ball!", maxx div 2, maxy div 2, fontTNR, green)
                delay (1000)
                fontTNR := Font.New ("Courier:8")
                Font.Draw ("Use AD or Arrow Keys to move! Press b to begin!", maxx div 2 - 100, maxy div 2 - 20, fontTNR, red)
                score := 0 % Just to prevent people from spamming the debug system to gain points
                get mainmenuexit % It asks the user for input
            View.Set("offscreenonly, graphics:maxx;maxxy, nocursor")
            else
                exit
            end if
            if mainmenuexit = "b" then %When user inputs b, it changes mainmenu to false so that the loop will skip the main menu if statement
                mainmenu := false
            else
                cls
                put "Invalid key."
                View.Update
                delay (2000)
                exit
            end if
        end loop % Ends the main menu de bugging loop
         timenow := Time.Elapsed() % So I can measure the time elapsed from this point
         timesincethen := timenow - timestart
    cls
    if oval_y1 >= 400 then %if y cord of oval goes >=400 then it inverses the direction of the y
        oval_directiony := oval_directiony * -1
    end if
    if oval_x1 >= 630 then %if the x cord of the oval goes >= 630 then it inverses the direction of the x
        oval_directionx := oval_directionx * -1
    end if
    if oval_x1 <= 0 then %  x cord direction inverse
        oval_directionx := oval_directionx * -1
    end if
        loop % Loop to de bug the continue system
            if oval_y1 <= 20 then %  if y cord hits <= 20 it asks if you'd like to continue (reseting score)
                cls
                put "Would you like to continue? y/n: " ..
                get continue   % if you'd like to continue..
            else
                exit
            end if
            if continue = "y" or continue = "n" then
                cls % Clears the screen after you type in a valid answer
                oval_x1 := 328
                oval_y1 := 228
                oval_directionx := 1
                oval_directiony := 1 % Reset the direction & placement of the ball
                score := 0 % Resets the score
                timenow := 0
                exit
            else
                put "Invalid answer."
                delay (2000)
                cls
            end if
        end loop % End the debugging loop
    if continue = "n" then
        put "Good Bye!" %Says goodbye and closes if you chose not to keep going
        delay (2000)
        break
    end if
    oval_x1 += (oval_directionx * oval_speed) % adds the direction *speed to the x cord of the ball
    oval_y1 += (oval_directiony * oval_speed) % " but with the y cord
            if chars (KEY_RIGHT_ARROW) then %Right key move the paddle to the right
                box1_x1 := box1_x1 + 3
                box1_x2 := box1_x2 + 3
            end if
            if chars ('d') then % " but d
                box1_x1 := box1_x1 + 3
                box1_x2 := box1_x2 + 3
            end if
            if chars (KEY_LEFT_ARROW) then % left key move paddle to the left
                box1_x2 := box1_x2 - 3
                box1_x1 := box1_x1 - 3
            end if
            if chars ('a') then % " but a
                box1_x2 := box1_x2 - 3
                box1_x1 := box1_x1 - 3
            end if
        if box1_x1 >= 630 - 115 or box1_x2 >= 630 then %Sets the far x bounds for the paddle
            box1_x1 := box1_x1 - 5
            box1_x2 := box1_x2 - 5
        end if
        if box1_x1 <= 0 or box1_x2 <= 15 + 115 then %Sets the near x bounds for the paddle
            box1_x1 := box1_x1 + 5
            box1_x2 := box1_x2 + 5
        end if
        if oval_x1 <= box1_x2 & oval_y1 <= box1_y2 & oval_x1 >= box1_x1 & oval_y1 >= box1_y1 then %If collision occurs, the ball moves in the oposite direction
            oval_directiony := oval_directiony * -1
            score := score + 1
        end if
    colourback(0) put "Score:", score
        colourback(0) put "Time elapsed:", round(timesincethen / 1000) % How much time has elapsed since exiting the menu
        drawfillbox (box1_x1, box1_y1, box1_x2, box1_y2, red) %Draw the changes to the box
        drawfilloval (oval_x1,oval_y1,oval_xr1,oval_yr1,yellow) %draw the ball
    View.Update % Supposed to fix screen flickering
    delay (5)
end loop


I really enjoy this game and I plan on turning it into a brick breaker game, but it is absolutely bugging me that my timing doesn't work! I just can't figure out why. I've done just about everything I could think of, and iIve searched on here for somebody with the same problem for hours. The example I was giving earlier just doesn't seem to be working. No matter what variable i set to 0 or what variables i subtract from one another the time never restarts. The most I've done is destroy the time all together =( Please help <3
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Mon Sep 05, 2011 3:27 pm   Post subject: RE:Help me comprehend some (SIMPLE) Pong coding

You never need set anything to 0.

Since you seem to be putting in a decent effort I'll help you out in more than hints.

timeSinceReset = Time.Elapsed() - startTime. startTime is taken when you 'reset' the timer. Whenever you want to display the time, use this formula. If you want to reset, do startTime := Time.Elapsed. For the sake of clean code, put startTime := Time.Elapsed at the top of the main loop. You *Do NOT* want to put this in your "If continue = 'y' or continue = 'n'. DO NOT DO THIS. Honestly, that if statement doesn't even have to be there- all that stuff can be put in the top of the loop.

Play around with this if you still don't get it:

code:

var startTime : int
var reset_flag : char
startTime := Time.Elapsed()
loop
    put Time.Elapsed() - startTime
    put "Reset? y/n: "..
    get reset_flag
    if reset_flag = "y" then
        startTime := Time.Elapsed()
    end if
end loop


Note that I don't have a copy of Turing available atm, so I haven't been able to test that code, so I can't guarantee it's error-free.
Aange10




PostPosted: Tue Sep 06, 2011 4:42 pm   Post subject: RE:Help me comprehend some (SIMPLE) Pong coding

Oh my goodness. Thank you so much Insectoid! It works! Ahh. You should have seen the expersion on my face when my timer went from "25" to "0"!!!

Thanks a ton!!

How do I get bits? I'd like to donate <3
Insectoid




PostPosted: Tue Sep 06, 2011 5:45 pm   Post subject: RE:Help me comprehend some (SIMPLE) Pong coding

No need to donate (bits come from posting). Karma's cool though!
Aange10




PostPosted: Tue Sep 06, 2011 6:47 pm   Post subject: RE:Help me comprehend some (SIMPLE) Pong coding

Hehe when I get 8 more posts, I will be allowed to karma, which I shall do!
(and look, this post goes 1 towards that.. 7 left!)

I'm very much enjoying this game, and I absolutely love watching this expand. (If you knew me personally, you'd know that seeing progress is just absolutely fantastic to me.)

I've come to a minor problem (doesn't bother me nearly as bad as the timer) that I'm not sure how to fix. My problem is with the collision of my ball and paddle. My ball will occasionally go into one of the sides of my paddle.

Before I attempted to fix the problem, I decided I needed to actually learn (not follow a formula) what my collision equation was saying. So I drew a box and a square on a piece of paper and found out exactly what my program was saying. Now the reason I am confused and unable to fix this, is because what i found was that the = signs in my formula drew the border of my box, and the > and < signs "filled it in". So with that being said I have no idea how my ball is going inside my paddle. (Goes in one end comes out the other)

So I decided instead of just coming and asking, I would go to your tutorial on how to make a game and re-read the collision section. After re-reading it, I felt that my hypothesis of how my formula worked was true, but I didn't find the answer to my problem. Though I got two links!

After reading the collision section of your tutorial I decided to go back over the Turing Walkthrough and re-read their collision tutorial. I read it and it honestly didn't teach me anything. I've read the thing a bunch of times and I didn't really feel like it told me anything relevant to my problem. However, I noticed it told about a problem very similar to mine. Which happened to be with ball collision.

So long story short, I went and read the advanced guide. To be honest I'm very happy to say I comprehended the majority of what he was saying. I'm not going to lie; I didn't grasp every bit of it, especially considering I only read it once and then skimmed it once more, but I got the gist.

Where all this is leading to, and the reason I am here now is that I still haven't found a solution to my problem. To be honest I doubt my problem will need anything to do with advanced circular collision (though I will do it, if need be). But it still leaves me with one question: What's not right?

Current coding for collision detection (full coding is a couple posts up...)

Turing:

if oval_x1 <= box1_x2 & oval_y1 <= box1_y2 & oval_x1 >= box1_x1 & oval_y1 >= box1_y1 then %If collision occurs, the ball moves in the opposite direction
            oval_directiony := oval_directiony * -1
            score := score + 1
        end if
Aange10




PostPosted: Wed Sep 07, 2011 7:07 pm   Post subject: RE:Help me comprehend some (SIMPLE) Pong coding

Also this is my current coding for a pause. Obviously, I ran into the problem of not being able to use 'p' for both chars statements, so i used v for one. However this is bugging me, and I was wondering if I could get some help =/. I went to the turing help section and searched "pausing" and read all 20 forums that came up but... well I'm still here Sad


Turing:

var gameState : boolean% If gameState true then it is paused
var fontTNR : int % A to allow me to make a font style into a variable
var chars : array char of boolean
gameState := false
procedure pausing
        if gameState = true then
            cls
            fontTNR := Font.New ("Courier:14")
                    Font.Draw ("Screen Paused. Press V to unpause.", maxx div 2 -150, maxy div 2, fontTNR, purple)
                loop
                    Input.KeyDown (chars)
                    if chars ('v') | chars ('V') then
                        gameState := false
                    end if
                    exit when gameState = false
               end loop
               end if
    end pausing
loop
    cls
    Input.KeyDown (chars)   
    if chars ('p') | chars ('P') then
             gameState := true
            end if
    put "It's not paused!"
    put "gameState:", gameState
    pausing
    delay (1000)
end loop
DemonWasp




PostPosted: Wed Sep 07, 2011 7:18 pm   Post subject: RE:Help me comprehend some (SIMPLE) Pong coding

Your first problem is that you should be using or, not a vertical bar.
Aange10




PostPosted: Wed Sep 07, 2011 8:26 pm   Post subject: RE:Help me comprehend some (SIMPLE) Pong coding

What is a verticle bar...?
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Wed Sep 07, 2011 8:30 pm   Post subject: Re: RE:Help me comprehend some (SIMPLE) Pong coding

Turing:

...
    if chars ('p') | chars ('P') then

                   ^ that guy
Aange10




PostPosted: Wed Sep 07, 2011 9:17 pm   Post subject: RE:Help me comprehend some (SIMPLE) Pong coding

Okay, thankyou. I changed them all.... But that didn't fix the problem. What do I need to correct?
DemonWasp




PostPosted: Wed Sep 07, 2011 10:19 pm   Post subject: RE:Help me comprehend some (SIMPLE) Pong coding

Oh! If you were meaning the problem where you can't use 'p' for both pause and unpause, that's because you need to detect the key-down event, not the key-down state.

The state of key-down ( 'p' ) is whether or not that key is down right now.

The event of key-down ( 'p' ) is whether or not that key was just pressed down now. That is, whether 'p' is pressed now, but not on the last iteration.

The most straightforward way to detect this event is to keep the old keys array around and record into a new one on each iteration. Then, if new_keys_down ( 'p' ) and not old_keys_down ( 'p' ), then you know that p was pressed just this instant.

Looks something like this:
Turing:

var paused : boolean := false
var new_keys_down, old_keys_down : array char of boolean

% Need initialized values!
Input.KeyDown ( old_keys_down )

loop
    Input.KeyDown ( new_keys_down )

    if new_keys_down ( 'p' ) and not old_keys_down ( 'p' ) then
        % Either pause or unpause, depending on whether we were already paused:
        paused := not paused  % 'not' here switches true to false and false to true...or paused to unpaused and vice-versa
    end if

    if not paused then
        % game stuff here -- call some methods, whatever.
    end if

    % Do NOT forget this bit, or you'll have some weird bugs
    old_keys_down := new_keys_down
end loop
Aange10




PostPosted: Sat Sep 10, 2011 1:50 pm   Post subject: Re: Help me comprehend some (SIMPLE) Pong coding

Alright, so I've been busy latley, but for the past 3 or 4 days i've been working on making the computer generate bricks. I'm kind of (as you can assume) lost.

My coding so far has been
Turing:

var brick_x1 :array 1 .. 100 of int %Arrays
var brick_x2 :array 1 .. 100 of int
var brick_y1 :array 1 .. 100 of int
var brick_y2 :array 1 .. 100 of int
var brick_color :array 1 .. 100 of int
brick_x2(1) := 629 % far right x
brick_y2(1) := 399 % top
for i : 1 .. 1
brick_x1(i) := Rand.Int(brick_x2(i) - 115, brick_x2(i) - 20) % Random int to make the width of the brick

brick_color(i) := Rand.Int (1,10) % random color for the brick
end for
brick_y1(1) := brick_y2(1) - 20 % width (constant)
for e : 2 .. 10
end for
put "brick_x1:", brick_x1(1)
put "brick_x2:", brick_x2(1)
put "brick_y1:", brick_y1(1)
put "brick_y2:", brick_y2(1)
%for b : 1 .. 2
drawfillbox (brick_x1(1),brick_y1(1),brick_x2(1),brick_y2(1),brick_color(1)) % draw da box, all the (1) will be % when the %for statement is made
%end for

...
Pretty much all i was trying to make it do what generate bricks starting from the top right corner (629, 399) and having the computer generate a width (the x2) & color (brick_color). After it did that, it would generate another brick starting from where the brick behind it had ended.

Obviously I didn't do too well, and I was wondering how I should go about doing this. Is 4 arrays bad? Is multiple fors bad? Do I need a loop?
Zren




PostPosted: Sat Sep 10, 2011 3:23 pm   Post subject: RE:Help me comprehend some (SIMPLE) Pong coding

How would you draw a grid?
How would you draw a bunch of points spaced n pixels apart?

Start with a new program, and try drawing a grid from the bottom left (0,0). After you can, it's easy to translate it to the top.

Tip: Think of it as iterating through each column in a row.

Using a different array for x1, x2 isn't bad, you can't change that until you've learnt how to make your own data types (records/classes).

maxx and maxy are useful variables to find the maximum coordinates.
Aange10




PostPosted: Mon Sep 12, 2011 7:23 pm   Post subject: RE:Help me comprehend some (SIMPLE) Pong coding

Ok but why do i need a grid? I can find any cords i need with my Mouse.Where.. Not to mention i already found where i wanted my brick to start
Zren




PostPosted: Tue Sep 13, 2011 12:26 am   Post subject: RE:Help me comprehend some (SIMPLE) Pong coding

Because the behaviour you were describing is similar to a grid. As your eventual output of bricks would look like it has columns and rows.

It's also very easy to draw a grid.
Turing:


var startX := 0
var startY := maxy div 2
var cellW := 40
var cellH := 10
var w := cellW - 1
var h := cellH - 1
for y : 0 .. 100 by cellH
    for x : 0 .. maxx by cellW
        Draw.Dot (x, y, black) %Starting point per brick
        %Draw.Box (x, y, x + cellW, y + cellH, black) % Overlap!
        %Draw.Box (x, y, x + w, y + h, black) % No more overlap
        Draw.FillBox (startX + x, startY + y, startX + x + w, startY + y + h, Rand.Int (0, maxcolor))
    end for
end for

% Alternatively:
for y : 0 .. 19
    for x : 0 .. 4
        %Draw.Dot (x * cellW, y * cellH, black)
    end for
end for


I might have misinterpreted. I thought you were doing the game breakout where all the bricks are a fixed width. You seem to have some randomness in there. If you know how to make a for loop into a regular loop, then you can do a non fixed increment to the counter.

Turing:

var x := 0
loop
    exit when x > maxx
    Draw.Line(x, 0, x, maxy, black)
    x += Rand.Int (5, 20)
end loop
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 2 of 3  [ 37 Posts ]
Goto page Previous  1, 2, 3  Next
Jump to:   


Style:  
Search: