
-----------------------------------
Eny
Mon Jun 05, 2006 8:54 am

Air Hockey Help!
-----------------------------------
Hi, I need some help with programming an airhockey game.
I already have the graphics for it but I'm a bit confused on how to setup the key movements and collision.

Theres nothing in my turing book I was given that answers my question and my teacher doesn't explain things very well... he leaves out a lot of information.

Any tutorials or tips would be great...

-----------------------------------
Remm
Mon Jun 05, 2006 9:32 am


-----------------------------------
Hello and welcome to Compsci! 

First of all, the best place you could head to would be the Turing Walkthrough -- a collection of basic to advanced tutorials and such made by other programmers. 

Oh yeah - you can look in turing refrance under help in the acual turing program.

-----------------------------------
Eny
Mon Jun 05, 2006 10:14 am


-----------------------------------
Thanks, I'll check that out... however, the help reference just confuses me further. I get half of what they say, hopefully that link will help.

-----------------------------------
Eny
Mon Jun 05, 2006 10:36 am


-----------------------------------
Ok, I'm still a bit confused about how to make collision and the mouse keys work with the puck and 'sticks'(those puck looking things that push the puck).

I have a bit of a learning disability, so could someone dumb it down a bit for me? :oops: Sometimes I need to learn specifics.

-----------------------------------
Dan
Mon Jun 05, 2006 8:33 pm


-----------------------------------
Well there are a few difrent ways to do collision dectionion for this case:

1. The math + if staments way:

This is where you check to see if a point x,y or in this case your puck is past a line. For example you check to see if your puck's x value is greater then 0 and less then the maxX value and that your puck's y value is greater then 0 and less then the maxY value. If this NOT the case the you will whont to chage the direction of the puck.

For the padels you will whont do the same but the line you are checking agisted is moving.

Check out http://www.compsci.ca/v2/viewtopic.php?t=75 for examples and a  tutoral about this.


2. The whatdocolor way: This way is probly the simplest tho not as dynaick or as efsihment.

What you do is check the color of the pixel where the puck is going to be and if it is the color of the padal or wall chage directions.

Check out http://www.compsci.ca/v2/viewtopic.php?t=2381 for more info on whatdotcolor.


The [Turing Walkthrough] has alot of other tutorals that will be help full. Also there is a fourm full of example sorce code. I whould try sreaching for pong as it is a game that is prity much the same as what you are doing.

Also for more help tyring coding somthing, and when you get stuck come back here and post your code and what you are having problems with. Poeleop give alot more helpfull help when you have code posted.

-----------------------------------
Clayton
Mon Jun 05, 2006 8:52 pm


-----------------------------------
key movements are simple, use Input.KeyDown or a combination of hasch and getch to get the value of the key you want to use to move whatever it is you want to move:D also, plz excuse Hacker Dan's horrible spelling, as he has had a problem with it ever since well....forever:D other than that good luck and check through the Turing Walkthrough for specific tutorials on subjects that you are having problems with:D

-----------------------------------
Eny
Tue Jun 06, 2006 9:59 am


-----------------------------------
Thanks a bunch! If I have any more questions I'll do that :)

-----------------------------------
Eny
Tue Jun 06, 2006 10:20 am


-----------------------------------
Well I found a code here for air hockey, and asked my teacher to explain it to me so I understand it. So I took the coding idea and am going to modify it to my original designing..I hope thats not a cheap way to do it  :oops: 


setscreen ("graphics:700,max,position:center;center,nobuttonbar,offscreenonly")
var x, y, button, paddlex, paddley, lastballx, lastbally := 100
var ballx, bally : real := 100
var power := 5
var dx, dy : real := 1
proc drawscreen
    drawfillbox (0, 0, maxx, maxy, grey) % Border
    drawfillbox (20, 20, maxx - 20, maxy - 20, 1) %Background
    drawfillbox (20, 20, maxx - 20, 30, grey) % bottom
    drawfillbox (maxx div 2 - 70, 20, maxx div 2 + 70, 40, black) % net
    drawfillbox (20, maxy - 20, maxx - 20, maxy - 30, grey) % top
    drawfillbox (maxx div 2 - 70, maxy - 20, maxx div 2 + 70, maxy - 40, black) % net
    drawfillbox (20, maxy div 2, maxx - 20, maxy div 2 + 5, grey) %center line
    %ball
    drawfilloval (round (ballx), round (bally), 15, 15, 7)
    %paddle
    drawfilloval (paddlex, paddley, 35, 35, red)
    View.Update
    cls
end drawscreen
proc detection
    if ballx > maxx or ballx < 0 then
        dx *= -1
    end if
    if bally > maxy or bally < 0 then
        dy *= -1
    end if
    if abs (sqrt ((ballx - paddlex) ** 2 + (bally - paddley) ** 2)) < 35 then
        dx := (ballx - paddlex) / 10
        dy := (bally - paddley) / 10
    end if
end detection
loop
    mousewhere (x, y, button)
    if y > maxy div 2 then
        paddley := maxy div 2
    else
        paddley := y
    end if
    if x > maxx then
        paddlex := maxx
    elsif x < 0 then
        paddlex := 0
    else
        paddlex := x
    end if
    ballx += dx
    bally += dy
    detection
    if abs (ballx - lastballx) > power or abs (bally - lastbally) > power then
        lastballx := round (ballx)
        lastbally := round (bally)
        drawscreen
    end if
end loop


Code orgianly by rich cash.

Now if I were to add a score code area for the nets, what would I have to use?

-----------------------------------
Clayton
Tue Jun 06, 2006 11:24 am


-----------------------------------
just create a seperate procedure to add to the score each time the pucks coordinates on the screen go beyond a certain point (you can also use View.WhatDotColor to do this) run the procedure and add to the score :D

-----------------------------------
Eny
Tue Jun 06, 2006 11:25 am


-----------------------------------
:) Okey dokey, I'll try that now.

-----------------------------------
Eny
Wed Jun 07, 2006 8:47 am


-----------------------------------
The perimeters are all wrong, I forget how to check them when you run the program but I'm sure I have some saved code that will do that...

So something like this?

if x = 280 and y = 680 then
        put "score"
    end if

-----------------------------------
Remm
Wed Jun 07, 2006 9:31 am


-----------------------------------
is that code you posted above the detection for the puck going in? if so, its only checking one single pixel :/
Have like 
if x > num and x < num and y > num and y < num then
so you got if its greater than x value, but less than another x value, same with y

-----------------------------------
Eny
Wed Jun 07, 2006 9:33 am


-----------------------------------
Oh! I get it now! Thanks.

-----------------------------------
Eny
Wed Jun 07, 2006 12:38 pm


-----------------------------------
Ok, another problem... I can't get the mouse.where to work with the graphics... is there something I need to put in the code for text?(other than put "")

-----------------------------------
Eny
Thu Jun 08, 2006 8:59 am


-----------------------------------
Ok, nevermind... I fixed it :D

Now I got it reconizing the nets and I'm happy.

I'm just having one more problem... Ok, I got it to say , "SCORE!" everytime it goes into the net but it flashes by too fast and if I put a delay on it the entire screen goes white with the score text...

even if I do it like this:put "Score!" .. it still covers the rest of the screen with white.

-----------------------------------
Dan
Thu Jun 08, 2006 11:33 am


-----------------------------------
You should not mix "put" and grpahics of any kind.

Instented use Font.Draw to display text when your are doing somthing grpahical. See the info on Font.Draw from your turings help refercen (f10).

-----------------------------------
PhOtO!
Thu Jun 08, 2006 3:49 pm


-----------------------------------
that souce code you gave was made by me and rich cash, and u only took out the background! Even though u already said that it's not your code and u're gonna change it, i would appreaciate you to give credit (to rich cash) if u took a code that he posted!

-----------------------------------
Eny
Fri Jun 09, 2006 9:34 am


-----------------------------------
:) Yeah, I said it wasn't mine. I just forgot who posted it... I hope thats ok. If its not I'll redo it, but I mostly wanted it to learn the coding.

I showed my teacher and I even said it wasn't my code, he told me how the coding works so I could learn and said thats how he would do it. Either way, my teacher said it would be something like that.

Is that ok? I wouldn't want to make anyone angry.

-----------------------------------
[Gandalf]
Sat Jun 10, 2006 7:37 pm


-----------------------------------
You should not mix "put" and grpahics of any kind.
While this is generally a good mindset to follow, I've found that it's not always the case.  For example, when I'm testing out some feature of a program or I want to quickly display a statistic to the user, put works just fine.  Take a frames per second counter for example, it is a perfect thing to keep at the top right had corner of the screen in the small put font.  The only thing you must consider when doing this is that your put statement should be behind all other drawings so that the horizontal bar created by the text background colour will not interfere.

-----------------------------------
Eny
Mon Jun 12, 2006 8:45 am


-----------------------------------
Thanks for all your help, guys. My only problem is that it still flashes over the background... but I'll figure it out.
