======Project: Pong======
A project for Systems Programming by Alex Hughes during the Spring 2013.
This project was begun on 4/10/2013 and took 6 weeks.
=====Objectives=====
State the purpose of this project. What is the point of this project? What do we hope to accomplish by undertaking it?
SDL_PONG For Systems Programming
=====Prerequisites=====
In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:
SDL Tutorials 1-18
=====Background=====
State the idea or purpose of the project. What are you attempting to pursue?
Upon approval, you'll want to fill this section out with more detailed background information. DO NOT JUST PROVIDE A LINK.
Providing any links to original source material, such as from a project page, is a good idea.
You'll want to give a general overview of what is going to be accomplished (for example, if your project is about installing a web server, do a little write-up on web servers. What is it, why do we need one, how does it work, etc.)
=====Scope=====
Give a general overview of your anticipated implementation of the project. Address any areas where you are making upfront assumptions or curtailing potential detail. State the focus you will be taking in implementation.
=====Attributes=====
State and justify the attributes you'd like to receive upon successful approval and completion of this project.
* attribute1: why you feel your pursuit of this project will gain you this attribute
* attribute2: why you feel your pursuit of this project will gain you this attribute
* etc...
=====Procedure=====
The actual steps taken to accomplish the project. Include images, code snippets, command-line excerpts; whatever is useful for intuitively communicating important information for accomplishing the project.
=====Code=====
Upon completion of the project, if there is an applicable collection of created code, place a copy of your finished code within
blocks here.
====ball.cpp====
/* Paddle = Gimp, 10 width, 50 high, save as a bmp in project folrder pong */
/* Ball = Gimp, 20 Width, 20 Height save as a bmp in project folrder pong */
/*http://www.youtube.com/watch?annotation_id=annotation_387415&feature=iv&src_vid=cf0vWJn9zZc&v=Mm7-me6mPuk*/
#include "ball.h"
ball::ball(SDL_Surface* img, int x, int y, int w, int h, int xVel, int yVel)
{
image=img;
box.x=x;
box.y=y;
box.w=w;
box.h=h;
xvel=xVel;
yvel=yVel;
}
ball::~ball()
{
SDL_FreeSurface(image);
}
void ball::show()
{
SDL_BlitSurface(image,NULL,SDL_GetVideoSurface(),&box);
}
void ball::move(SDL_Rect* player1,SDL_Rect* player2)
{
box.x+=xvel;
box.y+=yvel;
if(box.y<=0)
yvel=-yvel;
if(box.y+box.h>=SDL_GetVideoSurface()->clip_rect.h)
yvel=-yvel;
if(collision(&box,player1))
{
//top of paddle collision
if(box.x+3x+player1->w)
yvel=-yvel;
else
xvel=-xvel;
}
if(collision(&box,player2))
{
if(box.x+box.w-3>player2->w)
yvel=-yvel;
else
xvel=-xvel;
}
}
bool ball::collision(SDL_Rect* rec1,SDL_Rect* rec2)
{
if(rec1->y >= rec2->y + rec2->h)
return 0;
if(rec1->x >= rec2->x + rec2->w)
return 0;
if(rec1->y + rec2->h <= rec2->y)
return 0;
if(rec1->x + rec1->w <= rec2->x)
return 0;
return 1;
}
int ball::isOut()
{
if(box.x<=0)
return 1;
if(box.x=SDL_GetVideoSurface()->clip_rect.w)
return 2;
return 0;
}
void ball::setBack(int x,int y, int w, int h, int xVel, int yVel)
{
box.x=x;
box.y=y;
box.w=w;
box.h=h;
xvel=xVel;
yvel=yVel;
}
====paddle.cpp====
#include "paddle.h"
paddle::paddle(SDL_Surface* img, int x, int y, int w,int h, int yVel)
{
box.x=x;
box.y=y;
box.w=w;
box.h=h;
image=img;
yvel=yVel;
point=0;
}
paddle::~paddle()
{
SDL_FreeSurface(image);
}
void paddle::moveUp()
{
if(box.y>0)
box.y-=yvel;
}
void paddle::moveDown()
{
if(box.y+box.hclip_rect.h)
box.y+=yvel;
}
void paddle::show()
{
SDL_BlitSurface(image,NULL, SDL_GetVideoSurface(), &box);
}
SDL_Rect* paddle::getRect()
{
return &box;
}
void paddle::incpoint()
{
point++;
}
void paddle::setBack(int x,int y,int w,int h,int yVel){
box.x=x;
box.y=y;
box.w=w;
box.h=h;
yvel=yVel;
}
int paddle::getPoints()
{
return point;
}
====main.cpp====
#include
#include
#include
#include "ball.h"
#include "paddle.h"
SDL_Surface* load_image(const char* c,Uint32 colorkey=0)
{
SDL_Surface* tmp=SDL_LoadBMP(c);
if(colorkey!=0)
{
SDL_SetColorKey(tmp, SDL_SRCCOLORKEY,colorkey);
}
return tmp;
}
int main()
{
SDL_Surface* screen;
const int width=640;
const int height=480;
const int FPS=30;
screen = SDL_SetVideoMode(width,height,32,SDL_SWSURFACE);
TTF_Font* font;
TTF_Init();
font = TTF_OpenFont("lazy.ttf",20);
SDL_Color color ={0,0,0};
SDL_Event event;
Uint32 start;
bool running=true;
bool arr[4] = {0,0,0,0};
paddle player1(load_image("paddle.bmp"),0,225,10,50,4);
paddle player2(load_image("paddle.bmp"),width-10,225,10,50,4);
ball ball1(load_image("ball.bmp", SDL_MapRGB(screen->format,0x00,0xff,0xff)),320,240,20,20,3,5);
SDL_Surface* text;
while(running)
{
start=SDL_GetTicks();
//handle events
while(SDL_PollEvent(&event)){
switch(event.type)
{
case SDL_QUIT:
running=false;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_UP:
arr[0]=1;
break;
case SDLK_DOWN:
arr[1]=1;
break;
case SDLK_w:
arr[2]=1;
break;
case SDLK_s:
arr[3]=1;
break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.sym)
{
case SDLK_UP:
arr[0]=0;
break;
case SDLK_DOWN:
arr[1]=0;
break;
case SDLK_w:
arr[2]=0;
break;
case SDLK_s:
arr[3]=0;
break;
}
break;
}
}
//logic
if(arr[0])
player2.moveUp();
else if(arr[1])
player2.moveDown();
if (arr[2])
player1.moveUp();
else if (arr[3])
player1.moveDown();
ball1.move(player1.getRect(),player2.getRect());
//scoring by out of screen
switch(ball1.isOut()){
case 1:
player2.incpoint();
player1.setBack(0,255,10,50,3);
player2.setBack(width-10,225,10,50,3);
ball1.setBack(320,240,20,20,3,3);
break;
case 2:
player1.incpoint();
player1.setBack(0,255,10,50,3);
player2.setBack(width-10,225,10,50,3);
ball1.setBack(320,240,20,20,3,3);
break;
}
//render
SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0xff,0xff,0xff));
player1.show();
player2.show();
ball1.show();
//text box for score
char c[5];
SDL_Rect tmp = {10,0};
sprintf(c,"%d", player1.getPoints());
c[4]='\0';
text = TTF_RenderText_Solid(font,c,color);
SDL_BlitSurface(text,NULL, screen,&tmp);
SDL_FreeSurface(text);
tmp.x=width-40;
sprintf(c,"%d", player2.getPoints());
text = TTF_RenderText_Solid(font,c,color);
SDL_BlitSurface(text,NULL,screen,&tmp);
SDL_FreeSurface(text);
SDL_Flip(screen);
//regulate FPS
if(1000/FPS>(SDL_GetTicks()-start))
SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
}
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
}
====ball.h====
#include
#ifndef BALL_H
#define BALL_H
class ball{
int xvel,yvel;
SDL_Surface* image;
SDL_Rect box;
bool collision(SDL_Rect* rec1,SDL_Rect* rec2);
public:
ball(SDL_Surface* img, int x, int y, int w, int h, int xVel, int yVel);
~ball();
void show();
void move(SDL_Rect* player1,SDL_Rect* player2);
//ball out of screen
int isOut();
void setBack(int x,int y, int w, int h, int xVel, int yVel);
};
#endif
====paddle.h====
#include
#ifndef PADDLE_H
#define PADDLE_H
class paddle{
SDL_Rect box;
SDL_Surface* image;
int yvel;
int point;
public:
paddle(SDL_Surface* img, int x, int y, int w, int h, int yVel);
~paddle();
SDL_Rect* getRect();
void moveUp();
void moveDown();
void show();
void incpoint();
void setBack(int x,int y,int w,int h,int yVel);
int getPoints();
};
#endif
=====Execution=====
Again, if there is associated code with the project, and you haven't already indicated how to run it, provide a sample run of your code:
lab46:~/src/cprog$ ./hello
Hello, World!
lab46:~/src/cprog$
====Compilation====
compile in the same directory that contains ball.h, paddle.h, ball.bmp, paddle.bmp, and lazy.ttf
PATH: g++ -o pong main.cpp ball.cpp paddle.cpp -lSDL -lSDL_ttf
====Execution====
PATH: ./pong
=====Reflection=====
Comments/thoughts generated through performing the project, observations made, analysis rendered, conclusions wrought. What did you learn from doing this project?
=====References=====
In performing this project, the following resources were referenced:
* http://www.youtube.com/watch?v=cf0vWJn9zZc
* http://www.youtube.com/watch?annotation_id=annotation_387415&feature=iv&src_vid=cf0vWJn9zZc&v=Mm7-me6mPuk
* http://www.youtube.com/watch?annotation_id=annotation_370665&feature=iv&src_vid=Mm7-me6mPuk&v=ufjCnc0ruKg
* http://www.youtube.com/watch?annotation_id=annotation_541223&feature=iv&src_vid=ufjCnc0ruKg&v=uvQEQ7g4omU
* http://www.youtube.com/watch?annotation_id=annotation_164158&feature=iv&src_vid=uvQEQ7g4omU&v=eNEIDPw_0hk
Generally, state where you got informative and useful information to help you accomplish this project when you originally worked on it (from Google, other wiki documents on the Lab46 wiki, etc.)
====guide====
This allowed me to learn code at a brisk pace for me to learn as much as possible in a short time span.