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

Username:   Password: 
 RegisterRegister   
 I am having issues in making a very basic game of pong
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Banks999




PostPosted: Mon Jan 12, 2015 12:12 am   Post subject: I am having issues in making a very basic game of pong

What is it you are trying to achieve?
I am trying to make a very basic game of pong.


What is the problem you are having?
The ball will phase through the player's paddle if the paddle touches the top of the screen and the same will happen with the AI's paddle if it touches the bottom of the screen. I also need to slow down the AI slightly without slowing down the entire game.


Describe what you have tried to solve this problem
I have looked at a couple of tutorials, peoples post, and peoples pong code.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:






Please specify what version of Turing you are using
I am using the latest official release of Turing (Turing 4.1.1).

I am in grade 10 taking a programming tech course, and I have been given my final project. My project requires me to make a very basic game, so I choose pong. Due to me only having about 4 months of basic experience, I lacked much of the required knowledge for pong. this site has helped me with most of the problems I have come across so far. My current problems are listed above and, although I have looked at posts,tutorials,code, I either can not properly understand the information due to my lack of programming knowledge, or I can not find what I am looking for. Keep in mind that my game is not done yet since more things have to be implemented, so the list of problems I have will most likely change. Also, any help that would more guide me to the correct answer instead of giving me the answer is preferred due to me wanting to learn how to be better at programming. Finally, I am sorry if this is not the best way to set up this post, I am kind of new to forums in a whole.

I appreciate any help I get and I thank you in advance.



Pong AI Test.t
 Description:
Started as an AI test file, but became my main file.

Download
 Filename:  Pong AI Test.t
 Filesize:  2.97 KB
 Downloaded:  149 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Mon Jan 12, 2015 10:49 am   Post subject: RE:I am having issues in making a very basic game of pong

code:
if y1 >=maxy or y2 >=maxy then %This will prevent the paddle from going past the maximum upper veiw point
    y1:=maxy
    y2:=maxy-75

end if


This is the code that is causing the problem. Can you figure out why? Hint: You could solve this by completely eliminating the y2 variable (and the AIy2 variable) from your code and replacing it with a height variable instead.
Banks999




PostPosted: Mon Jan 12, 2015 5:43 pm   Post subject: Re: I am having issues in making a very basic game of pong

Thanks Smile. I was able to fix the paddles, so now the ball will not go through the paddles, though I don't know if I used what you would call height variables. I also am yet to figure out how to delay the AI without the entire game being slowed down. I also found another bug where if the ball hits a certain spot in the paddle, it will make the ball spaz out and either launch it towards the opposite side or launch it towards your own, causing the ball to spawn back in the middle. If you could point me in the right direction towards fixing these problems, I would greatly appreciate it.


Pong.t
 Description:
Updated version with a better name

Download
 Filename:  Pong.t
 Filesize:  3.05 KB
 Downloaded:  133 Time(s)

Insectoid




PostPosted: Mon Jan 12, 2015 6:43 pm   Post subject: RE:I am having issues in making a very basic game of pong

Can you copy & paste your code here so I don't have to download it? The easier it is for me to help, the more likely I am to.
Banks999




PostPosted: Mon Jan 12, 2015 7:16 pm   Post subject: Re: I am having issues in making a very basic game of pong

ok, here it is


var x, y, x2, y2 : int %The x and y cordinants for the bottom left and top right corners of the first/players paddle
x := 1
y := 150
%x2:=10
%y2:=225
var Ballx, Bally : int %The x and y cordinats of the ball
Ballx := 315
Bally := 200
var Changex, Changey : int := 5 %Allows ball to move and is also the speed of the ball, higher the number, the faster the ball and causes the ball to move in a different pattern
var Paddle1 : array char of boolean %The first paddle/players paddle
var AIx, AIy, AIx2, AIy2 : int %The cordinates for the bottom left and the top right corners of the secon [layer's/AI's paddle
AIx := 630
AIy := 150
%AIx2:=639
%AIy2:=225
var RandomSpawn : int %Causes the ball to switch it's pattern when respawning in the centre

proc BallChaser %AI paddle goes after the ball
AIy := Bally - 50


end BallChaser

loop

View.Set ("offscreenonly") %To help get rid of flickering

%1st Player's Paddle

Input.KeyDown (Paddle1) %Allows player 1 to use arrow keys for paddle movement/actions


if Paddle1 (KEY_UP_ARROW) and y < maxy then %If player 1 presses the up arrow key then the paddle will go up
y := y + 5

elsif Paddle1 (KEY_DOWN_ARROW) and y > 0 then %If player 1 presses the down arrow key then the paddle will go down
y := y - 5

end if

if y + 75 >= maxy then %This will prevent the paddle from going past the maximum upper veiw point
y := maxy - 75
%y2:=maxy-75

end if

if y <= 0 then %Will prevent paddle from going past maximum lower veiw point
y := 0
%y2:=0+75

end if

%Player 2's/AI's paddle

BallChaser

if AIy + 75 >= maxy then %This will prevent the AI from leaving the top screen
AIy := maxy - 75
%AIy2:=maxy-75
elsif AIy <= 0 then %This will prevent the AI from leaving the bottom screen
AIy := 0
%AIy2:=0+75

end if

%Ball

Ballx += Changex %Allows ball to move horizontally
Bally += Changey %Allows ball to move vertically
randint (RandomSpawn, 3, 7)


if Ballx > x - 5 and Ballx < x + 10 and Bally > y and Bally < y + 75 then %Allows the ball to bounce off the player 1's paddle
Changex := -Changex
elsif Ballx > AIx - 5 and Ballx < AIx + 10 and Bally > AIy and Bally < AIy + 75 then %Allows ball to bounce off player 2's/AI's paddle
Changex := -Changex
elsif Bally < 0 or Bally > maxy then %Allows ball to bounce off the top and bottom of the screen
Changey := -Changey
elsif Ballx < 0 then
Ballx := 315
Bally := 200
Changey := RandomSpawn
elsif Ballx > maxx then
Ballx := 315
Bally := 200
Changey := RandomSpawn

end if

%Other

Draw.FillOval (Ballx, Bally, 5, 5, black) %Ball

drawfillbox (x, y, x + 9, y + 75, black) %Player 1's paddle

drawfillbox (AIx, AIy, AIx + 9, AIy + 75, black) %Player 2's/AI's paddle

delay (12) %Increasing this number will slow down everything, decreasing this number will speed up everything

View.Update

cls

end loop
Insectoid




PostPosted: Mon Jan 12, 2015 8:51 pm   Post subject: RE:I am having issues in making a very basic game of pong

The ball is going inside the paddle. This happens when the ball moves at an angle through the side of the paddle. By the time the collision is detected, the ball is already behind the paddle. When it detects a collision, the ball switches direction, but it doesn't move far enough to get back outside the paddle. So another collision is detected and the ball switches direction again. It's still inside the paddle so another collision happens and again, it switches direction. It keeps doing this until it pops out the side. This is what causes the 'spazzing'.

There's a couple of ways to solve this.

-Prevent the ball from ever going inside the paddle with a more robust collision detection
-Move the ball somewhere else if it goes inside the paddle (if you pick a good spot, nobody will ever know it went inside)
-Tweak your 'bounce' code to only move the ball right if it hits the left paddle and left if it hits the right paddle.

Some of these solutions are better than others, some are easier, and some change the gameplay slightly. Pick the one that suits you best or figure out your own fix.
Banks999




PostPosted: Tue Jan 13, 2015 7:11 pm   Post subject: Re: I am having issues in making a very basic game of pong

Thanks Smile. I have been working on fixing up my ball code by making a better collision detection, but currently have been unsuccessful. This does not mean I give up though, I am going to keep at it until I get it right. Once again, thanks. I do not know how this karma or bits thing works, but I am going to give you some.
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  [ 7 Posts ]
Jump to:   


Style:  
Search: