d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > [C++] Pong Project > Ascii Art Game
Prev156789Next
Add Reply New Topic New Poll
Banned
Posts: 1,432
Joined: Aug 3 2009
Gold: 225.49
Oct 2 2009 04:28pm
Quote (Muted @ Fri, Oct 2 2009, 02:17pm)
Translation: I posted the code in expectation of you keeping your word (or at least what I interpreted as being "your word"), and you just /denied it.
That's fine, thanks anyways. I was hoping to see your opinion of how it should've been. For a game as simple and small as Pong, it shouldn't take that long to modify/write.

Oh, maybe I mis interpreted what you'd said.. Something about google project --> Feel free to e-mail me it all if you don't want to post it here.
Digital.Goddess@yahoo.com is my e-mail address I use.


enjoy the scat porn that i just subscribed you to
Member
Posts: 6,953
Joined: Sep 27 2003
Gold: 518.50
Oct 2 2009 04:55pm
Sent you e-mail. For brevity, I'll put a example class here. I'll use CBall, because it's relatively simple.

First, I split the definition and the header up:
CBall.h:
Code
#pragma once
#include <windows.h>

class cBall {

  private:
      int iForeground; /* Ball's foreground color */
      int iBackground; /* Ball's Background color */

      int iBall_X; /* Ball's X coordinate */
      int iBall_Y; /* Ball's Y coordinate */

      int iVelocity_X; /* Ball's X velocity (speed) */
      int iVelocity_Y; /* Ball's Y velocity (speed) */

      SMALL_RECT smWalls;

  public:
      cBall() { };

      void ResetBall(const int X, const int Y);
      void UpdateBall();
      void DrawBall();
      void ReverseBall();

      inline int GetXVelocity() { return iVelocity_X; }
      inline int GetYVelocity() { return iVelocity_Y; }
      inline int GetXBall() { return iBall_X; }
      inline int GetYBall() { return iBall_Y; }

      inline void SetForeground(const int Color) { iForeground = Color; return; }
      inline void SetBackground(const int Color) { iBackground = Color; return; }
      inline void SetXBall(const int X) { iBall_X = X; return; }
      inline void SetYBall(const int Y) { iBall_Y = Y; return; }
      inline void SetXVelocity(const int Velocity) { iVelocity_X = Velocity; return; }
      inline void SetYVelocity(const int Velocity) { iVelocity_Y = Velocity; return; }
      inline void SetLeftWall(const int Wall) { smWalls.Left = Wall; return; }
      inline void SetRightWall(const int Wall) { smWalls.Right = Wall; return; }
      inline void SetTopWall(const int Wall) { smWalls.Top = Wall; return; }
      inline void SetBottomWall(const int Wall) { smWalls.Bottom = Wall; return; }
};

Splitting up enables fast incremental builds, due to smart compiler optimization. I also moved the required includes to CBall.cpp, which helps make building faster. I'd like to eliminate the need for windows.h, but SMALL_RECT is an integral part of this class.

The next major thing was explicitly inlining your simple functions. Using this tells the compiler to copy the function to the local scope, rather than doing a function call and going through the stack. This is a tiny little speedup that most compilers do with /O2 or better, but it certainly doesn't hurt. You can also remove all those empty return calls. They don't do anything.

CBall.cpp:
Code
#include "CBall.h"
#include "TextControl.h"
#include <iostream>

void cBall::ReverseBall()
{
   if (rand() % 9)
       iVelocity_X = -iVelocity_X;
   else
       iVelocity_X = (-iVelocity_X + 1);
}

void cBall::DrawBall()
{

  /* Draw the ball at the new position */
  PlaceCursor(iBall_X, iBall_Y);
  SetColor(iForeground, iBackground);
  printf("รน");
}

void cBall::UpdateBall()
{
   /* Erase the old ball */
   PlaceCursor(iBall_X, iBall_Y);
   printf(" ");

   /* Bounce if the ball hits a wall */
   if (iBall_X > smWalls.Right || iBall_X < smWalls.Top) { iVelocity_X = -iVelocity_X; }
   if (iBall_Y > smWalls.Bottom || iBall_Y < smWalls.Top) { iVelocity_Y = -iVelocity_Y; }

   /* Update the position */
   iBall_X += iVelocity_X;
   iBall_Y += iVelocity_Y;
}

void cBall::ResetBall(const int X, const int Y) {
   PlaceCursor(iBall_X, iBall_Y);
   printf(" ");

   iBall_X = X;
   iBall_Y = Y;
   iVelocity_X = 1;
   iVelocity_Y = 1;
}

There really aren't any changes to the underlying functionality, aside from removing the superfluous return statements, but I've included it to show the syntax for defining class member functions.
Banned
Posts: 1,432
Joined: Aug 3 2009
Gold: 225.49
Oct 2 2009 04:57pm
Quote (rockonkenshin @ Fri, Oct 2 2009, 01:28pm)
Just creating "an object" is not Object Oriented programming. There is a design mind behind it. One major concept in OO that people often get wrong is the "Tell, Don't Ask" idiom. Here is a great explanation of it:

http://www.pragprog.com/articles/tell-dont-ask

(By the way, every self-respecting programmer should own a copy of "The Pragmatic Programmer" ;) )

This of course is just one small idiom in OO. There are many others and until you understand how they work for you, you won't get it and use it properly.


this is interesting

book downloaded. I'm awesome.
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Oct 2 2009 05:20pm
Quote (recursion @ Fri, Oct 2 2009, 06:57pm)
this is interesting

book downloaded. I'm awesome.


\o/

Feel free to buy/steal Code Complete 2 by Steve McConnell and Effective Java by Josh Block or Effective C++ by Scott Meyers as well. Even after reading it twice, I still pick up Effective Java and reference it at least once a month at work, it's that good of a book and a great reference.
Banned
Posts: 1,432
Joined: Aug 3 2009
Gold: 225.49
Oct 2 2009 05:27pm
what's your take on this "learn 30 languages" issue? how many do you know?
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Oct 2 2009 06:08pm
Quote (recursion @ Fri, Oct 2 2009, 07:27pm)
what's your take on this "learn 30 languages" issue? how many do you know?


I am very much a person who thinks there is always a tool for a job and I program with that in mind. Personally, I extensively use Java and Python (admittedly I'd like to get into the who Python thing way more than I do now) at work, along with Oracle and PostgreSQL and bash scripting at work. I also know C/C++, but I'm really rusty since I haven't really used it since school.

I'm starting to learn Erlang as well, but I've put that on hold because I've been a bit busy.

This post was edited by rockonkenshin on Oct 2 2009 06:10pm
Member
Posts: 6,953
Joined: Sep 27 2003
Gold: 518.50
Oct 2 2009 06:19pm
Quote (rockonkenshin @ Fri, Oct 2 2009, 07:08pm)
I'm starting to learn Erlang as well, but I've put that on hold because I've been a bit busy.


Erlang is a good choice. Try out Haskell, too.
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Oct 2 2009 06:20pm
Quote (ASBands @ Fri, Oct 2 2009, 08:19pm)
Erlang is a good choice.  Try out Haskell, too.


Haskell is going to be my next project.
Member
Posts: 2,065
Joined: Feb 21 2008
Gold: 0.02
Warn: 10%
Oct 2 2009 08:03pm
Quote (rockonkenshin @ Fri, Oct 2 2009, 06:20pm)
Haskell is going to be my next project.


Why would you learn Erlang? O_o

Did you have a specific reason for learning Erlang? Erlang is cool and all, but Clojure is pretty up there on concurrency, and Haskell is just teh awesome. I'm sure you had a good reason for learning Erlang, such as desperate curiosity! :)
Banned
Posts: 1,432
Joined: Aug 3 2009
Gold: 225.49
Oct 2 2009 08:10pm
yeah, i'll be learning haskell sometime soon

ONLY BECAUSE IT SUCKS SO MUCH
Go Back To Programming & Development Topic List
Prev156789Next
Add Reply New Topic New Poll