Computer Science Canada

Collison with paddle from pong

Author:  JaxonB [ Fri Feb 26, 2021 12:24 pm ]
Post subject:  Collison with paddle from pong

What is it you are trying to achieve?
A ball bouncing off paddle

What is the problem you are having?
I'm having an issue with my code and it wont detect my collision


Describe what you have tried to solve this problem
Asked around with friends


Post any relevant code


setscreen ("nocursor")


%Varibles

%Title Varibles
var title : int := Font.New ("Bit9x9:90")
var start, eXit : int := Font.New ("Bit9x9:15")
var startorleave : string
var madeby : int := Font.New ("Bit9x9:10")
var startbutton : string := "Start"
var x_pos : int
var y_pos : int



%Main Game Varibles
var x, y : int := 5
var chars : array char of boolean
var x_dir : int
var radius : int := 5
randint (x_dir, -1, 1)
var height1, height2, height3, height4 : int
height1 := 150
height2 := 250
height3 := 150
height4 := 250
x_pos := 320
y_pos := 200
var wall1, wall2, wall3, wall4 : int
wall1 := 0
wall2 := 640
var player1score, player2score : int := 0


%Title Screen
loop

drawfill (1, 1, black, blue)
Font.Draw ("PONG", 170, 300, title, white)
Font.Draw ("Press any Key to start", 150, 250, start, white)
Font.Draw ("Made By: Jaxon Beairsto", 190, 20, madeby, white)
View.Update
Input.Pause
cls
exit
end loop

%Main Code
setscreen ("offscreenonly")
loop

%Draws the game

%Paddles
drawfillbox (0, height1, 5, height2, black)
drawfillbox (640, height3, 635, height4, black)

%Walls
drawfillbox (wall2, 399, 0, 400, white)
drawfillbox (wall1, 0, 640, 0, white)

drawfilloval (x_pos, 300, radius, radius, black)

drawfillbox (320, 0, 320, 400, black)
View.Update ()
delay (10)
cls
drawfilloval (x_pos, 300, radius, radius, white)

drawfillbox (640, 399, 0, 400, white)
drawfillbox (0, 0, 640, 0, white)
drawfillbox (0, height1, 5, height2, black)
drawfillbox (640, height3, 634, height4, black)
drawfillbox (320, 0, 320, 400, black)
locate (1, 5)
put "Player 1: ", player1score
locate (1, 47)
put "Player 2: ", player2score

%Indentifys the keys
Input.KeyDown (chars)
if chars ('w') then
height1 := height1 + 1
height2 := height2 + 1
elsif chars ('s') then
height1 := height1 - 1
height2 := height2 - 1
end if
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
height3 := height3 + 1
height4 := height4 + 1
elsif chars (KEY_DOWN_ARROW) then
height3 := height3 - 1
height4 := height4 - 1
end if
%Ball movements
x_pos := x_pos + x_dir
if x_pos > maxx then
x_dir := -1
end if

%What the ball bounces off of
if whatdotcolour (x_pos, x_dir) = black then
end if


%Score
if x_pos = 640 then
x_pos := 320
player1score := player1score + 1
end if
if x_pos = 0 then
x_pos := 320
player2score := player2score + 1
end if
end loop








Turing:






Please specify what version of Turing you are using
Turing4-1-1a\Turing 4.1.

Author:  Insectoid [ Sat Feb 27, 2021 6:06 pm ]
Post subject:  RE:Collison with paddle from pong

Whatdotcolor() takes an (x,y) coordinate. Is x_dir a y coordinate?

Author:  scholarlytutor [ Sun Feb 28, 2021 8:54 pm ]
Post subject:  RE:Collison with paddle from pong

Has anyone written a tutorial for collision detection? It's not something I've personally learned, but it's definitely something I expect a lot of students will ask about.

I'm going to direct you to this video by Ogany Surpreme on YouTube. He walks you through making a game of pong in Turing:

https://www.youtube.com/watch?v=6BsRZgQdhBg

Once you learn how collision detection works, try to see if you can make your game unique in some way, like upside-down pong, or even 4 player pong.

Good luck!

Author:  Insectoid [ Tue Mar 02, 2021 12:33 pm ]
Post subject:  RE:Collison with paddle from pong

There are lots of collision detection tutorials on this site. I think it's the most common tutorial we have.

Author:  scholarlytutor [ Tue Mar 02, 2021 1:16 pm ]
Post subject:  RE:Collison with paddle from pong

Insectoid: That's great. You guys have been around here a lot longer than I have.

JaxonB: So besides the video, you can also go to Turing > Turing tutorial and type in "collision" into the search box. Here's one example I found from there:

http://compsci.ca/v3/viewtopic.php?t=75&highlight=collision


: