• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
mistervirtue

Code Help - Pong Game

11 posts in this topic

(I misposted this is in general, this is should have been posted in For Beginners)

I know what I want to do, but I am unsure how to do it. I want to have my ComputerPaddle Sprite Update it's location based on the position of my Ball Sprite. Code-wise I am not sure on how to allow my ComputerPaddle class to acess the postion of the ball class.
NOTE: Please don't tell me excatly how to do it, please drop hints/tips. I learn better if I have but together myself, but a point in the right direction is always nice.
---------------------------
Computer Paddle Class
[source lang="csharp"]class ComputerPaddle : BasePaddle
{
//Speed of the paddle
float paddleSpeed = 15;

public ComputerPaddle(Texture2D texture, Rectangle screenBounds, Vector2 direction, Vector2 position)
: base(texture, screenBounds, direction, position)
{
this.texture = texture;
this.screenBounds = screenBounds;
}
public override void Update()
{
//Use the location of the ball to move the paddle up and down
// in the Y direction, toward Y value of the ball
}
}[/source]
------------------------------------------
My Ball Class
[source lang="csharp"] class Ball
{
//The position of the ball
Vector2 position;
//Motion of the ball
Vector2 motion;
//speed of the ball
float ballSpeed = 2;
//Texture For the ball
Texture2D texture;
//Collision rectangle for the ball
Rectangle screenBounds;
//Ball Constructor
public Ball(Texture2D texture, Rectangle screenBounds)
{
this.texture = texture;
this.screenBounds = screenBounds;
}
//Update Method for the ball
public void Update()
{
position += ballSpeed * motion;
CheckForCollisonWithTop();
}
//Any time ball collides with the bounds of the window increase the speed of the ball 1 point.
//Any time ball collides with the bounds of the window change the directoin of the motion in the Y direction
public void CheckForCollisonWithTop()
{
if (position.Y < 0)
{
position.Y = 0;
motion.Y *= -1;
ballSpeed += 1;
}
if (position.Y + texture.Height > screenBounds.Height)
{
position.Y = screenBounds.Height - texture.Height;
motion.Y *= -1;
ballSpeed += 1;
}
}
public bool BehindPaddle()
{
if (position.X > screenBounds.Width || position.X < 0)
{
return true;
}
return false;
}
public void CheckForCollisionWithPaddle(Rectangle paddleLocation)
{
Rectangle ballLocation = new Rectangle(
(int)position.X, (int)position.Y, texture.Width, texture.Height);
if (paddleLocation.Intersects(ballLocation))
{
position.X = paddleLocation.X - texture.Width;
motion.X *= -1;
}
}
public void SetInStartPosition()
{
motion = new Vector2(1, -1);
position.X = (screenBounds.Width - texture.Width) / 2;
position.Y = (screenBounds.Height - texture.Height) / 2;
RestartBallSpeed();
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, Color.White);
}
public void RestartBallSpeed()
{
ballSpeed = 2;
}
}[/source]
-------------------------------------------
Like I said I think there is a way for me to acess the postion of the ball but I don't know how. Any help would be very much appreciated. I think I can do something similar to my CheckForCollisionWithPaddle() Method. I just need a little but of a push start.
0

Share this post


Link to post
Share on other sites
why don't you make something like GameManager class that has both ball and paddle as members and it will handle the collision ?! there are other ways to do it of course :)
0

Share this post


Link to post
Share on other sites
Not an answer, but that much desired hint: search for 'Get and Set values in coding' in your desired search engine.

Regards,

Stitchs.
0

Share this post


Link to post
Share on other sites
Why not just keep two classes, one for Paddle and one for the Ball.

[CODE]Ball.Check_Collision(Player_Paddle.Get_Bounding_Box_Top(), Player_Paddle.Get_Bounding_Box_Bottom(), Player_Paddle.Get_Bounding_Box_Left(), Player_Paddle.Get_Bounding_Box_Right());[/CODE]

The above assumes bounding box collision.

You could also just have a function for collision in your logic code, and pass both the Paddle and Ball into that function to find out if a collision is true. If so, then you have the ball, and game do whatever it's supposed to do after.

There are many ways to do this, but don't try to complicate something that is simple.
0

Share this post


Link to post
Share on other sites
I am not getting get and sets. I Have a Get BallPosition in my ball class but I don't know how to have my ComputerPaddle access it.
------------ MY Get for Ball Position in the ball Class --------------
[source lang="csharp"] public Vector2 GetPosition
{
get
{
return position;
}
}[/source]

Now how do I get my ComputerPaddle Access this value?
0

Share this post


Link to post
Share on other sites
Not sure why you're doing it that way... This is very simple thing to program.

Make a function that returns the value of your Ball's Position.

[CODE]
int Ball::Ball_X_Position()
{
return x;
}
[/CODE]

Then have a function for your Paddle which can read that position.

[CODE]Paddle.Paddle_Function(Ball.Ball_X_Position());[/CODE]

Just have whatever you need done occur after positions have been read.

You don't need to use Get and Set.
1

Share this post


Link to post
Share on other sites
C# uses functions like C++ does. Since C# uses classes, you have functions in those classes.

For example:

[CODE]
using System;

class Ball
{
private int x, y;

// Constructor
public Ball(int Temp_X, int Temp_Y)
{
x = Temp_X;
y = Temp_Y;
}

// Destructor
~Ball(){}

// Get X
public int Get_X()
{
return x;
}
}
[/CODE]

Just a basic example. Edited by Black-Rook
0

Share this post


Link to post
Share on other sites
You make a method which returns the position of object a, then object b has a method which reads the positions of object a and compares itself with those positions, then follows through with the appropriate actions.

I explained how to do this six posts above.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0