基本信息
源码名称:c++ 双人弹球 小游戏示例源码
源码大小:14.59M
文件格式:.zip
开发语言:C/C++
更新时间:2017-12-30
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 4 元×
微信扫码支付:4 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
双人弹球
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h"
#include <string>
#include <cstdlib>
#include <stdlib.h>
//Screen attributes
const int SCREEN_WIDTH = 720;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The frame rate
const int FRAMES_PER_SECOND = 20;
//The attributes of the square
const int SQUARE_WIDTH = 20;
const int SQUARE_HEIGHT = 20;
const int CATCHER_HEIGHT = 20;
const int AWARD_HEIGHT=30;
const int AWARD_WIDTH=30;
const int PUNISH_HEIGHT=30;
const int PUNISH_WIDTH=30;
//Total particles
//The button states in the sprite sheet
const int CLIP_MOUSEAOVER = 0; //菜单按钮
const int CLIP_MOUSEAOUT = 1;
const int CLIP_MOUSEBOVER = 2;
const int CLIP_MOUSEBOUT= 3;
const int CLIP_MOUSECOVER = 4;
const int CLIP_MOUSECOUT = 5;
const int CLIP_MOUSEDOVER = 6;
const int CLIP_MOUSEDOUT= 7;
const int CLIP_MOUSEEOVER = 8; //难度选择按钮
const int CLIP_MOUSEEOUT = 9;
const int CLIP_MOUSEFOVER = 10;
const int CLIP_MOUSEFOUT= 11;
const int CLIP_MOUSEGOVER = 12;
const int CLIP_MOUSEGOUT = 13;
const int CLIP_MOUSEHOVER = 14; //回合数选择按钮
const int CLIP_MOUSEHOUT = 15;
const int CLIP_MOUSEIOVER = 16;
const int CLIP_MOUSEIOUT= 17;
const int CLIP_MOUSEJOVER = 18;
const int CLIP_MOUSEJOUT = 19;
const int TOTAL_PARTICLES = 20;
int CATCHER_WIDTH_NUM[]= {60,90,120};
int CATCHER1=CATCHER_WIDTH_NUM[1];
int CATCHER2=CATCHER_WIDTH_NUM[1];
int i=1; //挡块长度 数组
int j=1; //j=2为奖励 j=0为惩罚
int difficulty_v;
bool replay = false;
SDL_Surface *message0 = NULL;
SDL_Surface *message10 = NULL;
SDL_Surface *message11 = NULL;
SDL_Surface *message12 = NULL;
SDL_Surface *message13 = NULL;
SDL_Surface *message14 = NULL;
SDL_Surface *message15 = NULL;
SDL_Surface *message20 = NULL;
SDL_Surface *message21 = NULL;
SDL_Surface *message22 = NULL;
SDL_Surface *message23 = NULL;
SDL_Surface *message24 = NULL;
SDL_Surface *message25 = NULL;
SDL_Surface *background = NULL;
SDL_Surface *square = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *catcher10 = NULL;
SDL_Surface *catcher1 = NULL;
SDL_Surface *catcher11 = NULL;
SDL_Surface *catcher20 = NULL;
SDL_Surface *catcher2 = NULL;
SDL_Surface *catcher22 = NULL;
SDL_Surface *win1 = NULL;
SDL_Surface *win2 = NULL;
SDL_Surface *red = NULL;
SDL_Surface *green = NULL;
SDL_Surface *blue = NULL;
SDL_Surface *shimmer = NULL;
SDL_Surface *award = NULL;
SDL_Surface *punish = NULL;
SDL_Surface *buttonSheet = NULL;
SDL_Surface *mbg = NULL;
SDL_Surface *db = NULL;
SDL_Surface *tip = NULL;
SDL_Surface *return1 = NULL;
SDL_Surface *return2 = NULL;
TTF_Font *font = NULL;
Mix_Music *music = NULL;
Mix_Chunk *hit = NULL;
Mix_Chunk *mouse = NULL;
SDL_Color textColor = { 0, 0, 0 };
SDL_Event event;
SDL_Rect clips[ 20 ];
struct pa
{
int a=0,b=0,c=0,d=0;
int d1=0,d2=0,d3=0;
int h1=0,h2=0,h3=0;
}mm;
struct part
{
int x,y;
} part1,part2; //两个挡块的坐标信息
struct score
{
int x=0;
} score1,score2,huihe_num;
struct parttt
{
bool x=false;
bool y=false;
bool z=false;
bool re=false; // 菜单
bool d1=false;
bool d2=false;
bool d3=false;
bool h1=false;
bool h2=false;
bool h3=false;
} to;
struct judge
{
bool x=false;
bool quit=false;
} judge1,judge2,regame,huihe,difficulty,mmenu,data;
struct addv //y速度加速
{
int a=0;
} v1,v2;
struct add_v //x速度加速
{
int x=0;
} _v1,_v2;
struct square_information //方块的位置 速度信息
{
int vx,vy;
int x,y;
int t;
} s_i;
int jueduizhi(int a) //求绝9对值
{
if(a<0)
return -a;
else
return a;
}
struct award_punish //奖励与惩罚
{
int x=0,y=0;
int show=0; //是否显示奖励惩罚
int frame=0; //奖励惩罚
int add1=1; //挡块1的变化
int add2=1; //挡块2的变化
} address;
int random(int x,int y) //生成随机数,随机出现奖励惩罚
{
int a;
for(; a>y||a<x;)
{
a=rand();
if(a<=y&&a>=x)
break;
}
return a;
}
class ButtonA
{
private:
//The attributes of the button
SDL_Rect boxA;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
ButtonA( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class ButtonB
{
private:
//The attributes of the button
SDL_Rect boxB;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
ButtonB( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class ButtonC
{
private:
//The attributes of the button
SDL_Rect boxC;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
ButtonC( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class ButtonD
{
private:
//The attributes of the button
SDL_Rect boxD;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
ButtonD( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class Buttond1
{
private:
//The attributes of the button
SDL_Rect boxd1;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
Buttond1( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class Buttond2
{
private:
//The attributes of the button
SDL_Rect boxd2;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
Buttond2( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class Buttond3
{
private:
//The attributes of the button
SDL_Rect boxd3;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
Buttond3( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class Buttonh1
{
private:
//The attributes of the button
SDL_Rect boxh1;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
Buttonh1( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class Buttonh2
{
private:
//The attributes of the button
SDL_Rect boxh2;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
Buttonh2( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class Buttonh3
{
private:
//The attributes of the button
SDL_Rect boxh3;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
Buttonh3( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class Particle
{
private:
//Offsets
int x, y;
//Current frame of animation
int frame;
//Type of particle
SDL_Surface *type;
public:
//Constructor
Particle( int X, int Y );
//Shows the particle
void show();
//Checks if particle is dead
bool is_dead();
};
class Square
{
private:
SDL_Rect boxs;
//The velocity of the square
int xVel, yVel;
//The particles
Particle *particles[ TOTAL_PARTICLES ];
public:
//Initializes the variables
Square();
//Cleans up particles
~Square();
void Square_hit_events();
//Moves the square
void Square_move();
void show_particles();
//Shows the square on the screen
void Square_show();
};
class Catcher1
{
private:
//The collision box of the catcher
SDL_Rect box1c;
//The velocity of the square
int xVel, yVel;
SDL_Surface *type;
public:
//Initializes the variables
Catcher1();
//Takes key presses and adjusts the square's velocity
void handle_input1();
//Moves the square
void Catcher1_move();
//Shows the square on the screen
void Catcher1_show();
};
class Catcher2
{
private:
//The collision box of the catcher
SDL_Rect box2c;
//The velocity of the square
int xVel, yVel;
SDL_Surface *type;
public:
//Initializes the variables
Catcher2();
//Takes key presses and adjusts the square's velocity
void computer_input2();
//Moves the square
void Catcher2_move();
//Shows the square on the screen
void Catcher2_show();
};
class Catcher3
{
private:
//The collision box of the catcher
SDL_Rect box3c;
//The velocity of the square
int xVel, yVel;
SDL_Surface *type;
public:
//Initializes the variables
Catcher3();
//Takes key presses and adjusts the square's velocity
void handle_input3();
//Moves the square
void Catcher3_move();
//Shows the square on the screen
void Catcher3_show();
};
//The timer
class Timer
{
private:
//The clock time when the timer started
int startTicks;
//The ticks stored when the timer was paused
int pausedTicks;
//The timer status
bool paused;
bool started;
public:
//Initializes variables
Timer();
//The various clock actions
void start();
void stop();
void pause();
void unpause();
//Gets the timer's time
int get_ticks();
//Checks the status of the timer
bool is_started();
bool is_paused();
};
SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old surface
SDL_FreeSurface( loadedImage );
//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 82, 95, 170 ) );
}
void Boss_exist();
//Shows the square on the screen
void Boss_show();
}
//Return the optimized surface
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
if( TTF_Init() == -1 )
{
return false;
}
if( Mix_OpenAudio( MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 512 ) == -1 )
{
return false;
}
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "game", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the square image
font = TTF_OpenFont( "lazy.ttf", 38 );
tip = load_image( "tip.png" );
buttonSheet = load_image( "button.png" );
background = load_image( "background.png" );
square = load_image( "square.png" );
catcher10= load_image( "catcher10.png" );
catcher1= load_image( "catcher1.png" );
catcher11= load_image( "catcher11.png" );
// catcher111= load_image( "catcher111.png" );
catcher20= load_image( "catcher20.png" );
catcher2= load_image( "catcher2.png" );
catcher22= load_image( "catcher22.png" );
// catcher222= load_image( "catcher222.png" );
db = load_image( "db.png" );
mbg = load_image( "mbg.png" );
win1 = load_image( "win1.png" );
win2 = load_image( "win2.png" );
award = load_image( "award.png" );
punish = load_image( "punish.png" );
return1 = load_image( "return1.png" );
return2 = load_image( "return2.png" );
message0 = TTF_RenderText_Solid( font, "..", textColor );
message10 = TTF_RenderText_Solid( font, "0", textColor );
message11 = TTF_RenderText_Solid( font, "1", textColor );
message12 = TTF_RenderText_Solid( font, "2", textColor );
message13 = TTF_RenderText_Solid( font, "3", textColor );
message14 = TTF_RenderText_Solid( font, "4", textColor );
message15 = TTF_RenderText_Solid( font, "5", textColor );
message20 = TTF_RenderText_Solid( font, "0", textColor );
message21 = TTF_RenderText_Solid( font, "1", textColor );
message22 = TTF_RenderText_Solid( font, "2", textColor );
message23 = TTF_RenderText_Solid( font, "3", textColor );
message24 = TTF_RenderText_Solid( font, "4", textColor );
message25 = TTF_RenderText_Solid( font, "5", textColor );
music = Mix_LoadMUS( "backgroundmusic.mp3" );
hit = Mix_LoadWAV( "hit.wav" );
mouse = Mix_LoadWAV( "mouse.wav" );
if( mouse == NULL )
{
return false;
}
if( ( hit == NULL ) )
{
return false;
}
if( ( db == NULL ) )
{
return false;
}
if( ( mbg == NULL ) )
{
return false;
}
if( music == NULL )
{
return false;
}
//If there was a problem in loading the square
if( font == NULL )
{
return false;
}
if( return1 == NULL )
{
return false;
}
if( return2 == NULL )
{
return false;
}
if( tip == NULL )
{
return false;
}
//If there was a problem in loading the square
if( buttonSheet == NULL )
{
return false;
}
if( background == NULL )
{
return false;
}
if( award == NULL )
{
return false;
}
if( punish == NULL )
{
return false;
}
if( square == NULL )
{
return false;
}
if( win1 == NULL )
{
return false;
}
if( win2 == NULL )
{
return false;
}
if( catcher10 == NULL )
{
return false;
}
if( catcher1 == NULL )
{
return false;
}
if( catcher11 == NULL )
{
return false;
}
// if( catcher111 == NULL )
// {
// return false;
// }
if( catcher20 == NULL )
{
return false;
}
if( catcher2 == NULL )
{
return false;
}
if( catcher22 == NULL )
{
return false;
}
// if( catcher222 == NULL )
// {
// return false;
// }
red = load_image( "red.bmp" );
green = load_image( "green.bmp" );
blue = load_image( "blue.bmp" );
shimmer = load_image( "shimmer.bmp" );
//If there was a problem in loading the images
if( ( shimmer == NULL ) || ( blue == NULL ) || ( green == NULL ) || ( red == NULL ) )
{
return false;
}
//Set alpha
SDL_SetAlpha( red, SDL_SRCALPHA | SDL_RLEACCEL, 192 );
SDL_SetAlpha( blue, SDL_SRCALPHA | SDL_RLEACCEL, 192 );
SDL_SetAlpha( green, SDL_SRCALPHA | SDL_RLEACCEL, 192 );
SDL_SetAlpha( shimmer, SDL_SRCALPHA | SDL_RLEACCEL, 192 );
//If everything loaded fine
return true;
}
void clean_up()
{
TTF_CloseFont( font );
SDL_FreeSurface( message0 );
SDL_FreeSurface( message10 );
SDL_FreeSurface( message11 );
SDL_FreeSurface( message12 );
SDL_FreeSurface( message13 );
SDL_FreeSurface( message14 );
SDL_FreeSurface( message15 );
SDL_FreeSurface( message20 );
SDL_FreeSurface( message21 );
SDL_FreeSurface( message22 );
SDL_FreeSurface( message23 );
SDL_FreeSurface( message24 );
SDL_FreeSurface( message25 );
SDL_FreeSurface( return1 );
SDL_FreeSurface( return2 );
SDL_FreeSurface( tip );
SDL_FreeSurface( buttonSheet );
SDL_FreeSurface( background );
SDL_FreeSurface( square );
SDL_FreeSurface( mbg );
SDL_FreeSurface( db );
SDL_FreeSurface( win1 );
SDL_FreeSurface( win2 );
SDL_FreeSurface( catcher10 );
SDL_FreeSurface( catcher1 );
SDL_FreeSurface( catcher11 );
// SDL_FreeSurface( catcher111 );
SDL_FreeSurface( catcher20 );
SDL_FreeSurface( catcher2 );
SDL_FreeSurface( catcher22 );
// SDL_FreeSurface( catcher222 );
SDL_FreeSurface( red );
SDL_FreeSurface( green );
SDL_FreeSurface( blue );
SDL_FreeSurface( shimmer );
SDL_FreeSurface( award );
SDL_FreeSurface( punish );
Mix_FreeMusic( music );
Mix_FreeChunk( hit );
Mix_FreeChunk( mouse );
//Quit SDL
TTF_Quit();
Mix_CloseAudio();
SDL_Quit();
}
Particle::Particle( int X, int Y )
{
//Set offsets
x = X - 5 ( rand() % 25 );
y = Y - 5 ( rand() % 25 );
//Initialize animation
frame = rand() % 5;
//Set type
switch( rand() % 3 )
{
case 0:
type = red;
break;
case 1:
type = green;
break;
case 2:
type = blue;
break;
}
}
void Particle::show()
{
//Show image
apply_surface( x, y, type, screen );
//Show shimmer
if( frame % 2 == 0 )
{
apply_surface( x, y, shimmer, screen );
}
//Animate
frame ;
}
bool Particle::is_dead()
{
if( frame > 10 )
{
return true;
}
return false;
}
Square::Square()
{
//Initialize the offsets
boxs.x=280;
boxs.y=440;
//Initialize the velocity
xVel = 0;
yVel = 0;
//Initialize particles
for( int p = 0; p < TOTAL_PARTICLES; p )
{
particles[ p ] = new Particle( boxs.x, boxs.y );
}
}
Square::~Square()
{
//Delete particles
for( int p = 0; p < TOTAL_PARTICLES; p )
{
delete particles[ p ];
}
}
Catcher1::Catcher1()
{
//Initialize the offsets
box1c.x = 280;
box1c.y = 460;
//Set the square's dimentions
box1c.w = CATCHER1;
box1c.h = CATCHER_HEIGHT;
//Initialize the velocity
xVel = 0;
yVel = 0;
}
void Catcher1::handle_input1()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_LEFT:
xVel -= SCREEN_WIDTH / 20;
break;
case SDLK_RIGHT:
xVel = SCREEN_WIDTH / 20;
break;
case SDLK_UP:
v1.a = SCREEN_WIDTH / 80;
break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_LEFT:
xVel = SCREEN_WIDTH / 20;
break;
case SDLK_RIGHT:
xVel -= SCREEN_WIDTH / 20;
break;
case SDLK_UP:
v1.a -= SCREEN_WIDTH / 80;
break;
}
}
}
void Catcher1::Catcher1_move()
{
//Move the square left or right
box1c.x = xVel;
box1c.y = yVel;
//If the square went too far to the left or right or has collided with the wall
if( ( box1c.x < 0 ) )
{
//Move backbox.y -= yVel;
box1c.x =0;
}
if( ( box1c.x CATCHER1 > SCREEN_WIDTH ) )
{
//Move backbox.y -= yVel;
box1c.x =SCREEN_WIDTH-CATCHER1;
}
part1.y=box1c.y;
part1.x=box1c.x;
_v1.x=xVel/5;
}
void Catcher1::Catcher1_show()
{
if(address.add1==0) type=catcher10;
if(address.add1==1) type=catcher1;
if(address.add1==2) type=catcher11;
apply_surface( box1c.x, box1c.y, type, screen );
}
Catcher2::Catcher2()
{
//Initialize the offsets
box2c.x = 280;
box2c.y = 0;
//Set the square's dimentions
box2c.w = CATCHER2;
box2c.h = CATCHER_HEIGHT;
//Initialize the velocity
xVel = 0;
yVel = 0;
}
void Catcher2::computer_input2()
{
if(s_i.vy<0)
{
if((s_i.x SQUARE_WIDTH/2)-(box2c.x CATCHER2/2)>0)
xVel =SCREEN_WIDTH / difficulty_v;
else if((s_i.x SQUARE_WIDTH/2)-(box2c.x CATCHER2/2)<=0)
xVel =-SCREEN_WIDTH / difficulty_v;
}
}
void Catcher2::Catcher2_move()
{
//Move the square left or right
box2c.x = xVel;
box2c.y = yVel;
//If the square went too far to the left or right or has collided with the wall
if( ( box2c.x < 0 ) )
{
//Move backbox.y -= yVel;
box2c.x =0;
}
if(( box2c.x CATCHER2 > SCREEN_WIDTH ) )
{
//Move backbox.y -= yVel;
box2c.x =SCREEN_WIDTH-CATCHER2;
}
part2.x=box2c.x;
part2.y=box2c.y;
//_v2.x=xVel/5;
}
void Catcher2::Catcher2_show()
{
if(address.add2==0) type=catcher20;
if(address.add2==1) type=catcher2;
if(address.add2==2) type=catcher22;
apply_surface( box2c.x, box2c.y, type, screen );
}
Catcher3::Catcher3()
{
//Initialize the offsets
box3c.x = 280;
box3c.y = 0;
//Set the square's dimentions
box3c.w = CATCHER2;
box3c.h = CATCHER_HEIGHT;
//Initialize the velocity
xVel = 0;
yVel = 0;
}
void Catcher3::handle_input3()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_a:
xVel -= SCREEN_WIDTH / 20;
break;
case SDLK_d:
xVel = SCREEN_WIDTH / 20;
break;
case SDLK_w:
v2.a = SCREEN_WIDTH / 80;
break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_a:
xVel = SCREEN_WIDTH / 20;
break;
case SDLK_d:
xVel -= SCREEN_WIDTH / 20;
break;
case SDLK_w:
v2.a -= SCREEN_WIDTH / 80;
break;
}
}
}
void Catcher3::Catcher3_move()
{
//Move the square left or right
box3c.x = xVel;
box3c.y = yVel;
//If the square went too far to the left or right or has collided with the wall
if( ( box3c.x < 0 ) )
{
//Move backbox.y -= yVel;
box3c.x =0;
}
if(( box3c.x CATCHER2 > SCREEN_WIDTH ) )
{
//Move backbox.y -= yVel;
box3c.x =SCREEN_WIDTH-CATCHER2;
}
part2.x=box3c.x;
part2.y=box3c.y;
_v2.x=xVel/5;
}
void Catcher3::Catcher3_show()
{
if(address.add2==0) type=catcher20;
if(address.add2==1) type=catcher2;
if(address.add2==2) type=catcher22;
apply_surface( box3c.x, box3c.y, type, screen );
}
void Square::Square_hit_events()
{
if( ( boxs.x <= 0 ) || ( boxs.x SQUARE_WIDTH >= SCREEN_WIDTH ) )
xVel=-xVel;
if( boxs.y SQUARE_HEIGHT>=part1.y&&(boxs.x<part1.x CATCHER1&&boxs.x SQUARE_WIDTH>part1.x) )
{
yVel=-yVel;
yVel-=v1.a;
xVel =_v1.x/2;
}
if( boxs.y SQUARE_HEIGHT>=part1.y-35&&boxs.y SQUARE_HEIGHT<=part1.y-10&&(boxs.x<part1.x CATCHER1&&boxs.x SQUARE_WIDTH>part1.x) )
{
if(yVel>=0)
Mix_PlayChannel( -1, hit, 0 );
}
if( (part2.y CATCHER_HEIGHT>=boxs.y)&&(boxs.x<part2.x CATCHER2&&boxs.x SQUARE_WIDTH>part2.x) )
{
yVel=-yVel;
yVel =v2.a;
xVel =_v2.x/2;
}
if( (part2.y CATCHER_HEIGHT 35>=boxs.y&&part2.y CATCHER_HEIGHT 10<=boxs.y)&&(boxs.x<part2.x CATCHER2&&boxs.x SQUARE_WIDTH>part2.x) )
{
if(yVel<=0)
Mix_PlayChannel( -1, hit, 0 );
}
}
void Square::Square_move()
{
boxs.x = xVel;
boxs.y = yVel;
if(boxs.y SQUARE_HEIGHT<0)
judge1.x=true;
if(boxs.y>SCREEN_HEIGHT)
judge2.x=true;
address.frame ;
s_i.vx=xVel;
s_i.vy=yVel;
s_i.x=boxs.x;
s_i.y=boxs.y;
if(boxs.x<address.x AWARD_WIDTH 10&&boxs.x>address.x-10&&boxs.y<address.y AWARD_HEIGHT 10&&boxs.y>address.y-10&&j==2)
{
if(yVel<0)
{
address.add1 ;
i ;
CATCHER1=CATCHER_WIDTH_NUM[i];
if(i>2)
{
address.add1--;
i--;
CATCHER1=CATCHER_WIDTH_NUM[i];
}
}
if(yVel>0)
{
address.add2 ;
i ;
CATCHER2=CATCHER_WIDTH_NUM[i];
if(i>2)
{
address.add2--;
i--;
CATCHER2=CATCHER_WIDTH_NUM[i];
}
}
address.show=0;
address.x=0;
address.y=0;
}
if(boxs.x<address.x AWARD_WIDTH 10&&boxs.x>address.x-10&&boxs.y<address.y AWARD_HEIGHT 10&&boxs.y>address.y-10&&j==0)
{
if(yVel<0)
{
address.add1--;
i--;
CATCHER1=CATCHER_WIDTH_NUM[i];
if(i<0)
{
address.add1 ;
i ;
CATCHER1=CATCHER_WIDTH_NUM[i];
}
}
if(yVel>0)
{
address.add2--;
i--;
CATCHER2=CATCHER_WIDTH_NUM[i];
if(i<0)
{
address.add2 ;
i ;
CATCHER2=CATCHER_WIDTH_NUM[i];
}
}
address.frame=0;
address.show=0;
address.x=0;
address.y=0;
}
}
void Square::show_particles()
{
//Go through particles
for( int p = 0; p < TOTAL_PARTICLES; p )
{
//Delete and replace dead particles
if( particles[ p ]->is_dead() == true )
{
delete particles[ p ];
particles[ p ] = new Particle( boxs.x, boxs.y );
}
}
//Show particles
for( int p = 0; p < TOTAL_PARTICLES; p )
{
particles[ p ]->show();
}
}
void Square::Square_show()
{
apply_surface( 332, 198, message0, screen );
if(score1.x==0)
apply_surface( 343, 230, message10, screen );
if(score1.x==1)
apply_surface( 343, 230, message11, screen );
if(score1.x==2)
apply_surface( 343, 230, message12, screen );
if(score1.x==3)
apply_surface( 343, 230, message13, screen );
if(score1.x==4)
apply_surface( 343, 230, message14, screen );
if(score1.x==5)
apply_surface( 343, 230, message15, screen );
if(score2.x==0)
apply_surface( 343, 190, message20, screen );
if(score2.x==1)
apply_surface( 343, 190, message21, screen );
if(score2.x==2)
apply_surface( 343, 190, message22, screen );
if(score2.x==3)
apply_surface( 343, 190, message23, screen );
if(score2.x==4)
apply_surface( 343, 190, message24, screen );
if(score2.x==5)
apply_surface( 343, 190, message25, screen );
apply_surface( boxs.x, boxs.y, square, screen );
if(address.frame==300)
{
address.show=1;
address.x=random(50,650);
address.y=random(100,360);
j=2;
}
if(address.frame==600)
{
address.show=1;
address.x=random(50,650);
address.y=random(100,360);
j=0;
}
if(address.show==1)
{
if(j==2)
apply_surface(address.x,address.y,award,screen);
if(j==0)
apply_surface(address.x,address.y,punish,screen);
}
//Show the particles
show_particles();
if(judge1.x==true)
{
score1.x ;
regame.quit=true;
}
if(judge2.x==true)
{
score2.x ;
regame.quit=true;
}
if(score1.x==huihe_num.x)
{
Mix_HaltMusic();
apply_surface( 0,0,win1,screen );
}
if(score2.x==huihe_num.x)
{
Mix_HaltMusic();
apply_surface( 0,0,win2,screen );
}
}
Timer::Timer()
{
//Initialize the variables
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
}
void Timer::start()
{
//Start the timer
started = true;
//Unpause the timer
paused = false;
//Get the current clock time
startTicks = SDL_GetTicks();
}
void Timer::stop()
{
//Stop the timer
started = false;
//Unpause the timer
paused = false;
}
void Timer::pause()
{
//If the timer is running and isn't already paused
if( ( started == true ) && ( paused == false ) )
{
//Pause the timer
paused = true;
//Calculate the paused ticks
pausedTicks = SDL_GetTicks() - startTicks;
}
}
void Timer::unpause()
{
//If the timer is paused
if( paused == true )
{
//Unpause the timer
paused = false;
//Reset the starting ticks
startTicks = SDL_GetTicks() - pausedTicks;
//Reset the paused ticks
pausedTicks = 0;
}
}
int Timer::get_ticks()
{
//If the timer is running
if( started == true )
{
//If the timer is paused
if( paused == true )
{
//Return the number of ticks when the timer was paused
return pausedTicks;
}
else
{
//Return the current time minus the start time
return SDL_GetTicks() - startTicks;
}
}
//If the timer isn't running
return 0;
}
bool Timer::is_started()
{
return started;
}
bool Timer::is_paused()
{
return paused;
}
void date_clear()
{
to.x=false;
to.y=false;
to.z=false;
regame.quit=false;
judge1.x=false;
judge2.x=false;
score1.x=0;
score2.x=0;
to.re=false;
to.d1=false;
to.d2=false;
to.d3=false;
to.h1=false;
to.h2=false;
to.h2=false;
}
void date_init()
{
to.x=false;
to.y=false;
to.z=false;
to.re=false;
to.d1=false;
to.d2=false;
to.d3=false;
to.h1=false;
to.h2=false;
to.h2=false;
regame.x=false;
replay=false;
judge1.x=false;
judge2.x=false;
huihe.x=false;
difficulty.x=false;
mmenu.x=false;
mmenu.quit == false;
regame.quit=false;
judge1.quit=false;
judge2.quit=false;
huihe.quit=false;
difficulty.quit=false;
mmenu.quit=false;
score1.x=0;
score2.x=0;
address.x=0;
address.y=0;
address.show=0;
address.frame=0;
address.add1=1;
address.add2=1;
replay=false;
data.x=false;
CATCHER1=CATCHER_WIDTH_NUM[1];
CATCHER2=CATCHER_WIDTH_NUM[1];
i=1;
j=1;
}
void mouse_to_restart()
{
if( event.type == SDL_KEYDOWN )
{
if( event.key.keysym.sym == SDLK_SPACE )
{
if(score1.x==huihe_num.x||score2.x==huihe_num.x)
{
date_init();
data.x=true;
}
}
}
if( event.type == SDL_QUIT )
{
date_clear();
exit(0);
}
}
int game_vs_computer()
{
//The square
Square mySquare;
Catcher1 myCatcher1;
Catcher2 myCatcher2;
//The frame rate regulator
Timer fps;
//While the user hasn't quit
while( regame.quit == false )
{
//Start the frame timer
fps.start();
mySquare.Square_hit_events();
myCatcher2.computer_input2();
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
myCatcher1.handle_input1();
mouse_to_restart();
if( event.type == SDL_QUIT )
{
//Quit the program
regame.quit = true;
clean_up();
exit(0);
}
}
mySquare.Square_move();
myCatcher1.Catcher1_move();
myCatcher2.Catcher2_move();
//Fill the screen white
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
apply_surface( 0, 0, background, screen );
myCatcher1.Catcher1_show();
myCatcher2.Catcher2_show();
mySquare.Square_show();
if(data.x==true)
{
replay=true;
regame.quit=true;
}
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//Cap the frame rate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
}
int game_vs_player()
{
//The square
Square mySquare;
Catcher1 myCatcher1;
Catcher3 myCatcher3;
//The frame rate regulator
Timer fps;
//While the user hasn't quit
while( regame.quit == false )
{
//Start the frame timer
fps.start();
mySquare.Square_hit_events();
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//Handle events for the square
myCatcher1.handle_input1();
myCatcher3.handle_input3();
mouse_to_restart();
if( event.type == SDL_QUIT )
{
//Quit the program
regame.quit = true;
clean_up();
exit(0);
}
}
mySquare.Square_move();
myCatcher1.Catcher1_move();
myCatcher3.Catcher3_move();
apply_surface( 0, 0, background, screen );
myCatcher1.Catcher1_show();
myCatcher3.Catcher3_show();
mySquare.Square_show();
if(data.x==true)
{
replay=true;
regame.quit=true;
}
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//Cap the frame rate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
}
void set_clips()
{
//Clip the sprite sheet
clips[ CLIP_MOUSEAOVER ].x =0;
clips[ CLIP_MOUSEAOVER ].y = 70;
clips[ CLIP_MOUSEAOVER ].w = 300;
clips[ CLIP_MOUSEAOVER ].h = 70;
clips[ CLIP_MOUSEAOUT ].x = 0;
clips[ CLIP_MOUSEAOUT ].y = 0;
clips[ CLIP_MOUSEAOUT ].w = 300;
clips[ CLIP_MOUSEAOUT ].h = 70;
clips[ CLIP_MOUSEBOUT ].x = 300;
clips[ CLIP_MOUSEBOUT ].y = 0;
clips[ CLIP_MOUSEBOUT ].w = 300-1;
clips[ CLIP_MOUSEBOUT ].h = 70;
clips[ CLIP_MOUSEBOVER ].x = 300;
clips[ CLIP_MOUSEBOVER ].y = 70;
clips[ CLIP_MOUSEBOVER ].w = 300-1;
clips[ CLIP_MOUSEBOVER ].h = 70;
clips[ CLIP_MOUSECOVER ].x =600-1;
clips[ CLIP_MOUSECOVER ].y = 70;
clips[ CLIP_MOUSECOVER ].w = 300;
clips[ CLIP_MOUSECOVER ].h = 70;
clips[ CLIP_MOUSECOUT ].x = 600-1;
clips[ CLIP_MOUSECOUT ].y = 0;
clips[ CLIP_MOUSECOUT ].w = 300;
clips[ CLIP_MOUSECOUT ].h = 70;
clips[ CLIP_MOUSEDOUT ].x = 900;
clips[ CLIP_MOUSEDOUT ].y = 0;
clips[ CLIP_MOUSEDOUT ].w = 300-2;
clips[ CLIP_MOUSEDOUT ].h = 70;
clips[ CLIP_MOUSEDOVER ].x = 900;
clips[ CLIP_MOUSEDOVER ].y = 70;
clips[ CLIP_MOUSEDOVER ].w = 300-2;
clips[ CLIP_MOUSEDOVER ].h = 70;
clips[ CLIP_MOUSEEOVER ].x =0;
clips[ CLIP_MOUSEEOVER ].y = 255;
clips[ CLIP_MOUSEEOVER ].w = 230;
clips[ CLIP_MOUSEEOVER ].h = 115;
clips[ CLIP_MOUSEEOUT ].x = 0;
clips[ CLIP_MOUSEEOUT ].y = 140;
clips[ CLIP_MOUSEEOUT ].w = 230;
clips[ CLIP_MOUSEEOUT ].h = 115;
clips[ CLIP_MOUSEFOUT ].x = 230;
clips[ CLIP_MOUSEFOUT ].y = 140;
clips[ CLIP_MOUSEFOUT ].w = 230;
clips[ CLIP_MOUSEFOUT ].h = 115;
clips[ CLIP_MOUSEFOVER ].x = 230;
clips[ CLIP_MOUSEFOVER ].y = 255;
clips[ CLIP_MOUSEFOVER ].w = 230;
clips[ CLIP_MOUSEFOVER ].h = 115;
clips[ CLIP_MOUSEGOVER ].x =460;
clips[ CLIP_MOUSEGOVER ].y = 255;
clips[ CLIP_MOUSEGOVER ].w = 230;
clips[ CLIP_MOUSEGOVER ].h = 115;
clips[ CLIP_MOUSEGOUT ].x = 460;
clips[ CLIP_MOUSEGOUT ].y = 140;
clips[ CLIP_MOUSEGOUT ].w = 230;
clips[ CLIP_MOUSEGOUT ].h = 115;
clips[ CLIP_MOUSEHOVER ].x = 697;
clips[ CLIP_MOUSEHOVER ].y = 183;
clips[ CLIP_MOUSEHOVER ].w = 110;
clips[ CLIP_MOUSEHOVER ].h = 45;
clips[ CLIP_MOUSEHOUT ].x = 697;
clips[ CLIP_MOUSEHOUT ].y = 138;
clips[ CLIP_MOUSEHOUT ].w = 110;
clips[ CLIP_MOUSEHOUT ].h = 45;
clips[ CLIP_MOUSEIOUT ].x = 806;
clips[ CLIP_MOUSEIOUT ].y = 138;
clips[ CLIP_MOUSEIOUT ].w = 110;
clips[ CLIP_MOUSEIOUT ].h = 45;
clips[ CLIP_MOUSEIOVER ].x = 806;
clips[ CLIP_MOUSEIOVER ].y = 183;
clips[ CLIP_MOUSEIOVER ].w = 110;
clips[ CLIP_MOUSEIOVER ].h = 45;
clips[ CLIP_MOUSEJOVER ].x = 916;
clips[ CLIP_MOUSEJOVER ].y = 183;
clips[ CLIP_MOUSEJOVER ].w = 110;
clips[ CLIP_MOUSEJOVER ].h = 45;
clips[ CLIP_MOUSEJOUT ].x = 916;
clips[ CLIP_MOUSEJOUT ].y = 138;
clips[ CLIP_MOUSEJOUT ].w = 110;
clips[ CLIP_MOUSEJOUT ].h = 45;
}
ButtonA::ButtonA( int x, int y, int w, int h )
{
//Set the button's attributes
boxA.x = x;
boxA.y = y;
boxA.w = w;
boxA.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSEAOUT ];
}
ButtonB::ButtonB( int x, int y, int w, int h )
{
//Set the button's attributes
boxB.x = x;
boxB.y = y;
boxB.w = w;
boxB.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSEBOUT ];
}
ButtonC::ButtonC( int x, int y, int w, int h )
{
//Set the button's attributes
boxC.x = x;
boxC.y = y;
boxC.w = w;
boxC.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSECOUT ];
}
ButtonD::ButtonD( int x, int y, int w, int h )
{
//Set the button's attributes
boxD.x = x;
boxD.y = y;
boxD.w = w;
boxD.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSEDOUT ];
}
Buttond1::Buttond1( int x, int y, int w, int h )
{
//Set the button's attributes
boxd1.x = x;
boxd1.y = y;
boxd1.w = w;
boxd1.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSEEOUT ];
}
Buttond2::Buttond2( int x, int y, int w, int h )
{
//Set the button's attributes
boxd2.x = x;
boxd2.y = y;
boxd2.w = w;
boxd2.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSEFOUT ];
}
Buttond3::Buttond3( int x, int y, int w, int h )
{
//Set the button's attributes
boxd3.x = x;
boxd3.y = y;
boxd3.w = w;
boxd3.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSEGOUT ];
}
Buttonh1::Buttonh1( int x, int y, int w, int h )
{
//Set the button's attributes
boxh1.x = x;
boxh1.y = y;
boxh1.w = w;
boxh1.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSEHOUT ];
}
Buttonh2::Buttonh2( int x, int y, int w, int h )
{
//Set the button's attributes
boxh2.x = x;
boxh2.y = y;
boxh2.w = w;
boxh2.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSEIOUT ];
}
Buttonh3::Buttonh3( int x, int y, int w, int h )
{
//Set the button's attributes
boxh3.x = x;
boxh3.y = y;
boxh3.w = w;
boxh3.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSEJOUT ];
}
void ButtonA::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
//If the mouse is over the button
if( ( x > boxA.x ) && ( x < boxA.x boxA.w ) && ( y > boxA.y ) && ( y < boxA.y boxA.h ) )
{
mm.a ;
clip = &clips[ CLIP_MOUSEAOVER ];
if(mm.a==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.a=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSEAOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxA.x ) && ( x < boxA.x boxA.w ) && ( y > boxA.y ) && ( y < boxA.y boxA.h ) )
{
to.y=true;
}
}
}
}
void ButtonB::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
if( ( x > boxB.x ) && ( x < boxB.x boxB.w ) && ( y > boxB.y ) && ( y < boxB.y boxB.h ) )
{
mm.b ;
//Set the button sprite
clip = &clips[ CLIP_MOUSEBOVER ];
if(mm.b==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.b=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSEBOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxB.x ) && ( x < boxB.x boxB.w ) && ( y > boxB.y ) && ( y < boxB.y boxB.h ) )
{
to.x=true;
}
}
}
}
void ButtonC::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
if( ( x > boxC.x ) && ( x < boxC.x boxC.w ) && ( y > boxC.y ) && ( y < boxC.y boxC.h ) )
{
mm.c ;
//Set the button sprite
clip = &clips[ CLIP_MOUSECOVER ];
if(mm.c==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.c=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSECOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxC.x ) && ( x < boxC.x boxC.w ) && ( y > boxC.y ) && ( y < boxC.y boxC.h ) )
{
to.z=true;
}
}
}
}
void ButtonD::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
if( ( x > boxD.x ) && ( x < boxD.x boxD.w ) && ( y > boxD.y ) && ( y < boxD.y boxD.h ) )
{
mm.d ;
//Set the button sprite
clip = &clips[ CLIP_MOUSEDOVER ];
if(mm.d==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.d=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSEDOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxD.x ) && ( x < boxD.x boxD.w ) && ( y > boxD.y ) && ( y < boxD.y boxD.h ) )
{
clean_up();
exit(0);
}
}
}
}
void Buttond1::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
if( ( x > boxd1.x ) && ( x < boxd1.x boxd1.w ) && ( y > boxd1.y ) && ( y < boxd1.y boxd1.h ) )
{
mm.d1 ;
//Set the button sprite
clip = &clips[ CLIP_MOUSEEOVER ];
if(mm.d1==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.d1=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSEEOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxd1.x ) && ( x < boxd1.x boxd1.w ) && ( y > boxd1.y ) && ( y < boxd1.y boxd1.h ) )
{
to.d1=true;
difficulty_v=40;
}
}
}
}
void Buttond2::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
if( ( x > boxd2.x ) && ( x < boxd2.x boxd2.w ) && ( y > boxd2.y ) && ( y < boxd2.y boxd2.h ) )
{
mm.d2 ;
//Set the button sprite
clip = &clips[ CLIP_MOUSEFOVER ];
if(mm.d2==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.d2=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSEFOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxd2.x ) && ( x < boxd2.x boxd2.w ) && ( y > boxd2.y ) && ( y < boxd2.y boxd2.h ) )
{
to.d2=true;
difficulty_v=25;
}
}
}
}
void Buttond3::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
if( ( x > boxd3.x ) && ( x < boxd3.x boxd3.w ) && ( y > boxd3.y ) && ( y < boxd3.y boxd3.h ) )
{
mm.d3 ;
//Set the button sprite
clip = &clips[ CLIP_MOUSEGOVER ];
if(mm.d3==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.d3=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSEGOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxd3.x ) && ( x < boxd3.x boxd3.w ) && ( y > boxd3.y ) && ( y < boxd3.y boxd3.h ) )
{
to.d3=true;
difficulty_v=10;
}
}
}
}
void Buttonh1::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
if( ( x > boxh1.x ) && ( x < boxh1.x boxh1.w ) && ( y > boxh1.y ) && ( y < boxh1.y boxh1.h ) )
{
mm.h1 ;
//Set the button sprite
clip = &clips[ CLIP_MOUSEHOVER ];
if(mm.h1==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.h1=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSEHOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxh1.x ) && ( x < boxh1.x boxh1.w ) && ( y > boxh1.y ) && ( y < boxh1.y boxh1.h ) )
{
to.h1=true;
huihe_num.x=1;
}
}
}
}
void Buttonh2::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
if( ( x > boxh2.x ) && ( x < boxh2.x boxh2.w ) && ( y > boxh2.y ) && ( y < boxh2.y boxh2.h ) )
{
mm.h2 ;
//Set the button sprite
clip = &clips[ CLIP_MOUSEIOVER ];
if(mm.h2==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.h2=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSEIOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxh2.x ) && ( x < boxh2.x boxh2.w ) && ( y > boxh2.y ) && ( y < boxh2.y boxh2.h ) )
{
to.h2=true;
huihe_num.x=3;
}
}
}
}
void Buttonh3::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
if( ( x > boxh3.x ) && ( x < boxh3.x boxh3.w ) && ( y > boxh3.y ) && ( y < boxh3.y boxh3.h ) )
{
mm.h3 ;
//Set the button sprite
clip = &clips[ CLIP_MOUSEJOVER ];
if(mm.h3==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.h3=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSEJOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxh3.x ) && ( x < boxh3.x boxh3.w ) && ( y > boxh3.y ) && ( y < boxh3.y boxh3.h ) )
{
to.h3=true;
huihe_num.x=5;
}
}
}
}
void ButtonA::show()
{
//Show the button
apply_surface( boxA.x, boxA.y, buttonSheet, screen, clip );
}
void ButtonB::show()
{
//Show the button
apply_surface( boxB.x, boxB.y, buttonSheet, screen, clip );
}
void ButtonC::show()
{
//Show the button
apply_surface( boxC.x, boxC.y, buttonSheet, screen, clip );
}
void ButtonD::show()
{
//Show the button
apply_surface( boxD.x, boxD.y, buttonSheet, screen, clip );
}
void Buttond1::show()
{
//Show the button
apply_surface( boxd1.x, boxd1.y, buttonSheet, screen, clip );
}
void Buttond2::show()
{
//Show the button
apply_surface( boxd2.x, boxd2.y, buttonSheet, screen, clip );
}
void Buttond3::show()
{
//Show the button
apply_surface( boxd3.x, boxd3.y, buttonSheet, screen, clip );
}
void Buttonh1::show()
{
//Show the button
apply_surface( boxh1.x, boxh1.y, buttonSheet, screen, clip );
}
void Buttonh2::show()
{
//Show the button
apply_surface( boxh2.x, boxh2.y, buttonSheet, screen, clip );
}
void Buttonh3::show()
{
//Show the button
apply_surface( boxh3.x, boxh3.y, buttonSheet, screen, clip );
}
int menu()
{
apply_surface( 0, 0, mbg, screen );
//Clip the sprite sheet return 0;
set_clips();
//Make the button
ButtonA myButtonA( 200, 120, 300, 70 );
ButtonB myButtonB( 200, 190, 300, 70 );
ButtonC myButtonC( 200, 260, 300, 70 );
ButtonD myButtonD( 200, 330, 300, 70 );
//While the user hasn't quit
while( mmenu.quit == false )
{
//If there's events to handle
if( SDL_PollEvent( &event ) )
{
//Handle button events
myButtonA.handle_events();
myButtonB.handle_events();
myButtonC.handle_events();
myButtonD.handle_events();
if(to.x==true||to.y==true||to.z==true)
mmenu.quit=true;
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
exit (0);
}
}
//Show the button
myButtonA.show();
myButtonB.show();
myButtonC.show();
myButtonD.show();
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
return 0;
}
int the_difficulty()
{
Buttond1 myButtond1( 220, 70, 230, 115 );
Buttond2 myButtond2( 220, 185, 230, 115 );
Buttond3 myButtond3( 220, 300, 230, 115 );
//While the user hasn't quit
while( difficulty.quit == false )
{
//If there's events to handle
if( SDL_PollEvent( &event ) )
{
//Handle button events
myButtond1.handle_events();
myButtond2.handle_events();
myButtond3.handle_events();
if( to.d1==true||to.d2==true||to.d3==true )
difficulty.quit=true;
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
exit (0);
}
}
apply_surface( 0,0,db,screen );
//Show the button
myButtond1.show();
myButtond2.show();
myButtond3.show();
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
return 0;
}
int the_huihe()
{
Buttonh1 myButtonh1( 277, 90, 110, 45 );
Buttonh2 myButtonh2( 270, 205, 110, 45 );
Buttonh3 myButtonh3( 270, 320, 110, 45 );
//While the user hasn't quit
while( huihe.quit == false )
{
//If there's events to handle
if( SDL_PollEvent( &event ) )
{
//Handle button events
myButtonh1.handle_events();
myButtonh2.handle_events();
myButtonh3.handle_events();
if( to.h1==true||to.h2==true||to.h3==true )
huihe.quit=true;
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
exit (0);
}
}
//Fill the screen white
apply_surface( 0,0,db,screen );
//Show the button
myButtonh1.show();
myButtonh2.show();
myButtonh3.show();
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
return 0;
}
int to_tip()
{
bool quit = false;
int x=0,y=0;
while( quit == false )
{
apply_surface( 0, 0, tip, screen );
apply_surface( 585, 415, return1, screen );
while( SDL_PollEvent( &event ) )
{
if( event.type == SDL_MOUSEMOTION )
{
x = event.motion.x;
y = event.motion.y;
if( ( x > 585 ) && ( x < 585 135 ) && ( y > 415 ) && ( y < 415 65 ) )
{
apply_surface( 585, 415, return2, screen );
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
if( event.button.button == SDL_BUTTON_LEFT )
{
x = event.button.x;
y = event.button.y;
if( ( x > 585 ) && ( x < 585 135 ) && ( y > 415 ) && ( y < 415 65 ) )
{
to.re=true;
}
}
}
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
exit(0);
}
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
if(to.re==true)
quit=true;
}
return 0;
}
int main( int argc, char* args[] )
{
bool restart=false;
set_clips();
if( init() == false )
{
return 1;
}
if( load_files() == false )
{
return 1;
}
while(restart==false)
{
date_clear();
replay==false;
date_init();
if( Mix_PlayingMusic() == 0 )
{
//Play the music
if( Mix_PlayMusic( music, -1 ) == -1 )
{
return 1;
}
}
Mix_PauseMusic();
if( menu() == 1 )
return 1;
if( to.x==true )
{
if(the_difficulty()==1)
return 1;
if(the_huihe()==1)
return 1;
while(replay==false)
{
if( Mix_PausedMusic() == 1 )
{
Mix_ResumeMusic();
}
regame.quit=false;
judge1.x=false;
judge2.x=false;
if( game_vs_computer() == 1 )
return 1;
}
}
if( to.y==true )
{
if(the_huihe()==1)
return 1;
while(replay==false)
{
if( Mix_PausedMusic() == 1 )
{
Mix_ResumeMusic();
}
regame.quit=false;
judge1.x=false;
judge2.x=false;
if( game_vs_player() == 1 )
return 1;
}
}
if( to.z==true )
{
if(to_tip()==1)
return 1;
}
}
clean_up();
return 0;
}
双人弹球
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h"
#include <string>
#include <cstdlib>
#include <stdlib.h>
//Screen attributes
const int SCREEN_WIDTH = 720;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The frame rate
const int FRAMES_PER_SECOND = 20;
//The attributes of the square
const int SQUARE_WIDTH = 20;
const int SQUARE_HEIGHT = 20;
const int CATCHER_HEIGHT = 20;
const int AWARD_HEIGHT=30;
const int AWARD_WIDTH=30;
const int PUNISH_HEIGHT=30;
const int PUNISH_WIDTH=30;
//Total particles
//The button states in the sprite sheet
const int CLIP_MOUSEAOVER = 0; //菜单按钮
const int CLIP_MOUSEAOUT = 1;
const int CLIP_MOUSEBOVER = 2;
const int CLIP_MOUSEBOUT= 3;
const int CLIP_MOUSECOVER = 4;
const int CLIP_MOUSECOUT = 5;
const int CLIP_MOUSEDOVER = 6;
const int CLIP_MOUSEDOUT= 7;
const int CLIP_MOUSEEOVER = 8; //难度选择按钮
const int CLIP_MOUSEEOUT = 9;
const int CLIP_MOUSEFOVER = 10;
const int CLIP_MOUSEFOUT= 11;
const int CLIP_MOUSEGOVER = 12;
const int CLIP_MOUSEGOUT = 13;
const int CLIP_MOUSEHOVER = 14; //回合数选择按钮
const int CLIP_MOUSEHOUT = 15;
const int CLIP_MOUSEIOVER = 16;
const int CLIP_MOUSEIOUT= 17;
const int CLIP_MOUSEJOVER = 18;
const int CLIP_MOUSEJOUT = 19;
const int TOTAL_PARTICLES = 20;
int CATCHER_WIDTH_NUM[]= {60,90,120};
int CATCHER1=CATCHER_WIDTH_NUM[1];
int CATCHER2=CATCHER_WIDTH_NUM[1];
int i=1; //挡块长度 数组
int j=1; //j=2为奖励 j=0为惩罚
int difficulty_v;
bool replay = false;
SDL_Surface *message0 = NULL;
SDL_Surface *message10 = NULL;
SDL_Surface *message11 = NULL;
SDL_Surface *message12 = NULL;
SDL_Surface *message13 = NULL;
SDL_Surface *message14 = NULL;
SDL_Surface *message15 = NULL;
SDL_Surface *message20 = NULL;
SDL_Surface *message21 = NULL;
SDL_Surface *message22 = NULL;
SDL_Surface *message23 = NULL;
SDL_Surface *message24 = NULL;
SDL_Surface *message25 = NULL;
SDL_Surface *background = NULL;
SDL_Surface *square = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *catcher10 = NULL;
SDL_Surface *catcher1 = NULL;
SDL_Surface *catcher11 = NULL;
SDL_Surface *catcher20 = NULL;
SDL_Surface *catcher2 = NULL;
SDL_Surface *catcher22 = NULL;
SDL_Surface *win1 = NULL;
SDL_Surface *win2 = NULL;
SDL_Surface *red = NULL;
SDL_Surface *green = NULL;
SDL_Surface *blue = NULL;
SDL_Surface *shimmer = NULL;
SDL_Surface *award = NULL;
SDL_Surface *punish = NULL;
SDL_Surface *buttonSheet = NULL;
SDL_Surface *mbg = NULL;
SDL_Surface *db = NULL;
SDL_Surface *tip = NULL;
SDL_Surface *return1 = NULL;
SDL_Surface *return2 = NULL;
TTF_Font *font = NULL;
Mix_Music *music = NULL;
Mix_Chunk *hit = NULL;
Mix_Chunk *mouse = NULL;
SDL_Color textColor = { 0, 0, 0 };
SDL_Event event;
SDL_Rect clips[ 20 ];
struct pa
{
int a=0,b=0,c=0,d=0;
int d1=0,d2=0,d3=0;
int h1=0,h2=0,h3=0;
}mm;
struct part
{
int x,y;
} part1,part2; //两个挡块的坐标信息
struct score
{
int x=0;
} score1,score2,huihe_num;
struct parttt
{
bool x=false;
bool y=false;
bool z=false;
bool re=false; // 菜单
bool d1=false;
bool d2=false;
bool d3=false;
bool h1=false;
bool h2=false;
bool h3=false;
} to;
struct judge
{
bool x=false;
bool quit=false;
} judge1,judge2,regame,huihe,difficulty,mmenu,data;
struct addv //y速度加速
{
int a=0;
} v1,v2;
struct add_v //x速度加速
{
int x=0;
} _v1,_v2;
struct square_information //方块的位置 速度信息
{
int vx,vy;
int x,y;
int t;
} s_i;
int jueduizhi(int a) //求绝9对值
{
if(a<0)
return -a;
else
return a;
}
struct award_punish //奖励与惩罚
{
int x=0,y=0;
int show=0; //是否显示奖励惩罚
int frame=0; //奖励惩罚
int add1=1; //挡块1的变化
int add2=1; //挡块2的变化
} address;
int random(int x,int y) //生成随机数,随机出现奖励惩罚
{
int a;
for(; a>y||a<x;)
{
a=rand();
if(a<=y&&a>=x)
break;
}
return a;
}
class ButtonA
{
private:
//The attributes of the button
SDL_Rect boxA;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
ButtonA( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class ButtonB
{
private:
//The attributes of the button
SDL_Rect boxB;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
ButtonB( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class ButtonC
{
private:
//The attributes of the button
SDL_Rect boxC;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
ButtonC( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class ButtonD
{
private:
//The attributes of the button
SDL_Rect boxD;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
ButtonD( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class Buttond1
{
private:
//The attributes of the button
SDL_Rect boxd1;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
Buttond1( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class Buttond2
{
private:
//The attributes of the button
SDL_Rect boxd2;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
Buttond2( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class Buttond3
{
private:
//The attributes of the button
SDL_Rect boxd3;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
Buttond3( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class Buttonh1
{
private:
//The attributes of the button
SDL_Rect boxh1;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
Buttonh1( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class Buttonh2
{
private:
//The attributes of the button
SDL_Rect boxh2;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
Buttonh2( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class Buttonh3
{
private:
//The attributes of the button
SDL_Rect boxh3;
//The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
//Initialize the variables
Buttonh3( int x, int y, int w, int h );
//Handles events and set the button's sprite region
void handle_events();
//Shows the button on the screen
void show();
};
class Particle
{
private:
//Offsets
int x, y;
//Current frame of animation
int frame;
//Type of particle
SDL_Surface *type;
public:
//Constructor
Particle( int X, int Y );
//Shows the particle
void show();
//Checks if particle is dead
bool is_dead();
};
class Square
{
private:
SDL_Rect boxs;
//The velocity of the square
int xVel, yVel;
//The particles
Particle *particles[ TOTAL_PARTICLES ];
public:
//Initializes the variables
Square();
//Cleans up particles
~Square();
void Square_hit_events();
//Moves the square
void Square_move();
void show_particles();
//Shows the square on the screen
void Square_show();
};
class Catcher1
{
private:
//The collision box of the catcher
SDL_Rect box1c;
//The velocity of the square
int xVel, yVel;
SDL_Surface *type;
public:
//Initializes the variables
Catcher1();
//Takes key presses and adjusts the square's velocity
void handle_input1();
//Moves the square
void Catcher1_move();
//Shows the square on the screen
void Catcher1_show();
};
class Catcher2
{
private:
//The collision box of the catcher
SDL_Rect box2c;
//The velocity of the square
int xVel, yVel;
SDL_Surface *type;
public:
//Initializes the variables
Catcher2();
//Takes key presses and adjusts the square's velocity
void computer_input2();
//Moves the square
void Catcher2_move();
//Shows the square on the screen
void Catcher2_show();
};
class Catcher3
{
private:
//The collision box of the catcher
SDL_Rect box3c;
//The velocity of the square
int xVel, yVel;
SDL_Surface *type;
public:
//Initializes the variables
Catcher3();
//Takes key presses and adjusts the square's velocity
void handle_input3();
//Moves the square
void Catcher3_move();
//Shows the square on the screen
void Catcher3_show();
};
//The timer
class Timer
{
private:
//The clock time when the timer started
int startTicks;
//The ticks stored when the timer was paused
int pausedTicks;
//The timer status
bool paused;
bool started;
public:
//Initializes variables
Timer();
//The various clock actions
void start();
void stop();
void pause();
void unpause();
//Gets the timer's time
int get_ticks();
//Checks the status of the timer
bool is_started();
bool is_paused();
};
SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old surface
SDL_FreeSurface( loadedImage );
//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 82, 95, 170 ) );
}
void Boss_exist();
//Shows the square on the screen
void Boss_show();
}
//Return the optimized surface
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
if( TTF_Init() == -1 )
{
return false;
}
if( Mix_OpenAudio( MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 512 ) == -1 )
{
return false;
}
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "game", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the square image
font = TTF_OpenFont( "lazy.ttf", 38 );
tip = load_image( "tip.png" );
buttonSheet = load_image( "button.png" );
background = load_image( "background.png" );
square = load_image( "square.png" );
catcher10= load_image( "catcher10.png" );
catcher1= load_image( "catcher1.png" );
catcher11= load_image( "catcher11.png" );
// catcher111= load_image( "catcher111.png" );
catcher20= load_image( "catcher20.png" );
catcher2= load_image( "catcher2.png" );
catcher22= load_image( "catcher22.png" );
// catcher222= load_image( "catcher222.png" );
db = load_image( "db.png" );
mbg = load_image( "mbg.png" );
win1 = load_image( "win1.png" );
win2 = load_image( "win2.png" );
award = load_image( "award.png" );
punish = load_image( "punish.png" );
return1 = load_image( "return1.png" );
return2 = load_image( "return2.png" );
message0 = TTF_RenderText_Solid( font, "..", textColor );
message10 = TTF_RenderText_Solid( font, "0", textColor );
message11 = TTF_RenderText_Solid( font, "1", textColor );
message12 = TTF_RenderText_Solid( font, "2", textColor );
message13 = TTF_RenderText_Solid( font, "3", textColor );
message14 = TTF_RenderText_Solid( font, "4", textColor );
message15 = TTF_RenderText_Solid( font, "5", textColor );
message20 = TTF_RenderText_Solid( font, "0", textColor );
message21 = TTF_RenderText_Solid( font, "1", textColor );
message22 = TTF_RenderText_Solid( font, "2", textColor );
message23 = TTF_RenderText_Solid( font, "3", textColor );
message24 = TTF_RenderText_Solid( font, "4", textColor );
message25 = TTF_RenderText_Solid( font, "5", textColor );
music = Mix_LoadMUS( "backgroundmusic.mp3" );
hit = Mix_LoadWAV( "hit.wav" );
mouse = Mix_LoadWAV( "mouse.wav" );
if( mouse == NULL )
{
return false;
}
if( ( hit == NULL ) )
{
return false;
}
if( ( db == NULL ) )
{
return false;
}
if( ( mbg == NULL ) )
{
return false;
}
if( music == NULL )
{
return false;
}
//If there was a problem in loading the square
if( font == NULL )
{
return false;
}
if( return1 == NULL )
{
return false;
}
if( return2 == NULL )
{
return false;
}
if( tip == NULL )
{
return false;
}
//If there was a problem in loading the square
if( buttonSheet == NULL )
{
return false;
}
if( background == NULL )
{
return false;
}
if( award == NULL )
{
return false;
}
if( punish == NULL )
{
return false;
}
if( square == NULL )
{
return false;
}
if( win1 == NULL )
{
return false;
}
if( win2 == NULL )
{
return false;
}
if( catcher10 == NULL )
{
return false;
}
if( catcher1 == NULL )
{
return false;
}
if( catcher11 == NULL )
{
return false;
}
// if( catcher111 == NULL )
// {
// return false;
// }
if( catcher20 == NULL )
{
return false;
}
if( catcher2 == NULL )
{
return false;
}
if( catcher22 == NULL )
{
return false;
}
// if( catcher222 == NULL )
// {
// return false;
// }
red = load_image( "red.bmp" );
green = load_image( "green.bmp" );
blue = load_image( "blue.bmp" );
shimmer = load_image( "shimmer.bmp" );
//If there was a problem in loading the images
if( ( shimmer == NULL ) || ( blue == NULL ) || ( green == NULL ) || ( red == NULL ) )
{
return false;
}
//Set alpha
SDL_SetAlpha( red, SDL_SRCALPHA | SDL_RLEACCEL, 192 );
SDL_SetAlpha( blue, SDL_SRCALPHA | SDL_RLEACCEL, 192 );
SDL_SetAlpha( green, SDL_SRCALPHA | SDL_RLEACCEL, 192 );
SDL_SetAlpha( shimmer, SDL_SRCALPHA | SDL_RLEACCEL, 192 );
//If everything loaded fine
return true;
}
void clean_up()
{
TTF_CloseFont( font );
SDL_FreeSurface( message0 );
SDL_FreeSurface( message10 );
SDL_FreeSurface( message11 );
SDL_FreeSurface( message12 );
SDL_FreeSurface( message13 );
SDL_FreeSurface( message14 );
SDL_FreeSurface( message15 );
SDL_FreeSurface( message20 );
SDL_FreeSurface( message21 );
SDL_FreeSurface( message22 );
SDL_FreeSurface( message23 );
SDL_FreeSurface( message24 );
SDL_FreeSurface( message25 );
SDL_FreeSurface( return1 );
SDL_FreeSurface( return2 );
SDL_FreeSurface( tip );
SDL_FreeSurface( buttonSheet );
SDL_FreeSurface( background );
SDL_FreeSurface( square );
SDL_FreeSurface( mbg );
SDL_FreeSurface( db );
SDL_FreeSurface( win1 );
SDL_FreeSurface( win2 );
SDL_FreeSurface( catcher10 );
SDL_FreeSurface( catcher1 );
SDL_FreeSurface( catcher11 );
// SDL_FreeSurface( catcher111 );
SDL_FreeSurface( catcher20 );
SDL_FreeSurface( catcher2 );
SDL_FreeSurface( catcher22 );
// SDL_FreeSurface( catcher222 );
SDL_FreeSurface( red );
SDL_FreeSurface( green );
SDL_FreeSurface( blue );
SDL_FreeSurface( shimmer );
SDL_FreeSurface( award );
SDL_FreeSurface( punish );
Mix_FreeMusic( music );
Mix_FreeChunk( hit );
Mix_FreeChunk( mouse );
//Quit SDL
TTF_Quit();
Mix_CloseAudio();
SDL_Quit();
}
Particle::Particle( int X, int Y )
{
//Set offsets
x = X - 5 ( rand() % 25 );
y = Y - 5 ( rand() % 25 );
//Initialize animation
frame = rand() % 5;
//Set type
switch( rand() % 3 )
{
case 0:
type = red;
break;
case 1:
type = green;
break;
case 2:
type = blue;
break;
}
}
void Particle::show()
{
//Show image
apply_surface( x, y, type, screen );
//Show shimmer
if( frame % 2 == 0 )
{
apply_surface( x, y, shimmer, screen );
}
//Animate
frame ;
}
bool Particle::is_dead()
{
if( frame > 10 )
{
return true;
}
return false;
}
Square::Square()
{
//Initialize the offsets
boxs.x=280;
boxs.y=440;
//Initialize the velocity
xVel = 0;
yVel = 0;
//Initialize particles
for( int p = 0; p < TOTAL_PARTICLES; p )
{
particles[ p ] = new Particle( boxs.x, boxs.y );
}
}
Square::~Square()
{
//Delete particles
for( int p = 0; p < TOTAL_PARTICLES; p )
{
delete particles[ p ];
}
}
Catcher1::Catcher1()
{
//Initialize the offsets
box1c.x = 280;
box1c.y = 460;
//Set the square's dimentions
box1c.w = CATCHER1;
box1c.h = CATCHER_HEIGHT;
//Initialize the velocity
xVel = 0;
yVel = 0;
}
void Catcher1::handle_input1()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_LEFT:
xVel -= SCREEN_WIDTH / 20;
break;
case SDLK_RIGHT:
xVel = SCREEN_WIDTH / 20;
break;
case SDLK_UP:
v1.a = SCREEN_WIDTH / 80;
break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_LEFT:
xVel = SCREEN_WIDTH / 20;
break;
case SDLK_RIGHT:
xVel -= SCREEN_WIDTH / 20;
break;
case SDLK_UP:
v1.a -= SCREEN_WIDTH / 80;
break;
}
}
}
void Catcher1::Catcher1_move()
{
//Move the square left or right
box1c.x = xVel;
box1c.y = yVel;
//If the square went too far to the left or right or has collided with the wall
if( ( box1c.x < 0 ) )
{
//Move backbox.y -= yVel;
box1c.x =0;
}
if( ( box1c.x CATCHER1 > SCREEN_WIDTH ) )
{
//Move backbox.y -= yVel;
box1c.x =SCREEN_WIDTH-CATCHER1;
}
part1.y=box1c.y;
part1.x=box1c.x;
_v1.x=xVel/5;
}
void Catcher1::Catcher1_show()
{
if(address.add1==0) type=catcher10;
if(address.add1==1) type=catcher1;
if(address.add1==2) type=catcher11;
apply_surface( box1c.x, box1c.y, type, screen );
}
Catcher2::Catcher2()
{
//Initialize the offsets
box2c.x = 280;
box2c.y = 0;
//Set the square's dimentions
box2c.w = CATCHER2;
box2c.h = CATCHER_HEIGHT;
//Initialize the velocity
xVel = 0;
yVel = 0;
}
void Catcher2::computer_input2()
{
if(s_i.vy<0)
{
if((s_i.x SQUARE_WIDTH/2)-(box2c.x CATCHER2/2)>0)
xVel =SCREEN_WIDTH / difficulty_v;
else if((s_i.x SQUARE_WIDTH/2)-(box2c.x CATCHER2/2)<=0)
xVel =-SCREEN_WIDTH / difficulty_v;
}
}
void Catcher2::Catcher2_move()
{
//Move the square left or right
box2c.x = xVel;
box2c.y = yVel;
//If the square went too far to the left or right or has collided with the wall
if( ( box2c.x < 0 ) )
{
//Move backbox.y -= yVel;
box2c.x =0;
}
if(( box2c.x CATCHER2 > SCREEN_WIDTH ) )
{
//Move backbox.y -= yVel;
box2c.x =SCREEN_WIDTH-CATCHER2;
}
part2.x=box2c.x;
part2.y=box2c.y;
//_v2.x=xVel/5;
}
void Catcher2::Catcher2_show()
{
if(address.add2==0) type=catcher20;
if(address.add2==1) type=catcher2;
if(address.add2==2) type=catcher22;
apply_surface( box2c.x, box2c.y, type, screen );
}
Catcher3::Catcher3()
{
//Initialize the offsets
box3c.x = 280;
box3c.y = 0;
//Set the square's dimentions
box3c.w = CATCHER2;
box3c.h = CATCHER_HEIGHT;
//Initialize the velocity
xVel = 0;
yVel = 0;
}
void Catcher3::handle_input3()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_a:
xVel -= SCREEN_WIDTH / 20;
break;
case SDLK_d:
xVel = SCREEN_WIDTH / 20;
break;
case SDLK_w:
v2.a = SCREEN_WIDTH / 80;
break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_a:
xVel = SCREEN_WIDTH / 20;
break;
case SDLK_d:
xVel -= SCREEN_WIDTH / 20;
break;
case SDLK_w:
v2.a -= SCREEN_WIDTH / 80;
break;
}
}
}
void Catcher3::Catcher3_move()
{
//Move the square left or right
box3c.x = xVel;
box3c.y = yVel;
//If the square went too far to the left or right or has collided with the wall
if( ( box3c.x < 0 ) )
{
//Move backbox.y -= yVel;
box3c.x =0;
}
if(( box3c.x CATCHER2 > SCREEN_WIDTH ) )
{
//Move backbox.y -= yVel;
box3c.x =SCREEN_WIDTH-CATCHER2;
}
part2.x=box3c.x;
part2.y=box3c.y;
_v2.x=xVel/5;
}
void Catcher3::Catcher3_show()
{
if(address.add2==0) type=catcher20;
if(address.add2==1) type=catcher2;
if(address.add2==2) type=catcher22;
apply_surface( box3c.x, box3c.y, type, screen );
}
void Square::Square_hit_events()
{
if( ( boxs.x <= 0 ) || ( boxs.x SQUARE_WIDTH >= SCREEN_WIDTH ) )
xVel=-xVel;
if( boxs.y SQUARE_HEIGHT>=part1.y&&(boxs.x<part1.x CATCHER1&&boxs.x SQUARE_WIDTH>part1.x) )
{
yVel=-yVel;
yVel-=v1.a;
xVel =_v1.x/2;
}
if( boxs.y SQUARE_HEIGHT>=part1.y-35&&boxs.y SQUARE_HEIGHT<=part1.y-10&&(boxs.x<part1.x CATCHER1&&boxs.x SQUARE_WIDTH>part1.x) )
{
if(yVel>=0)
Mix_PlayChannel( -1, hit, 0 );
}
if( (part2.y CATCHER_HEIGHT>=boxs.y)&&(boxs.x<part2.x CATCHER2&&boxs.x SQUARE_WIDTH>part2.x) )
{
yVel=-yVel;
yVel =v2.a;
xVel =_v2.x/2;
}
if( (part2.y CATCHER_HEIGHT 35>=boxs.y&&part2.y CATCHER_HEIGHT 10<=boxs.y)&&(boxs.x<part2.x CATCHER2&&boxs.x SQUARE_WIDTH>part2.x) )
{
if(yVel<=0)
Mix_PlayChannel( -1, hit, 0 );
}
}
void Square::Square_move()
{
boxs.x = xVel;
boxs.y = yVel;
if(boxs.y SQUARE_HEIGHT<0)
judge1.x=true;
if(boxs.y>SCREEN_HEIGHT)
judge2.x=true;
address.frame ;
s_i.vx=xVel;
s_i.vy=yVel;
s_i.x=boxs.x;
s_i.y=boxs.y;
if(boxs.x<address.x AWARD_WIDTH 10&&boxs.x>address.x-10&&boxs.y<address.y AWARD_HEIGHT 10&&boxs.y>address.y-10&&j==2)
{
if(yVel<0)
{
address.add1 ;
i ;
CATCHER1=CATCHER_WIDTH_NUM[i];
if(i>2)
{
address.add1--;
i--;
CATCHER1=CATCHER_WIDTH_NUM[i];
}
}
if(yVel>0)
{
address.add2 ;
i ;
CATCHER2=CATCHER_WIDTH_NUM[i];
if(i>2)
{
address.add2--;
i--;
CATCHER2=CATCHER_WIDTH_NUM[i];
}
}
address.show=0;
address.x=0;
address.y=0;
}
if(boxs.x<address.x AWARD_WIDTH 10&&boxs.x>address.x-10&&boxs.y<address.y AWARD_HEIGHT 10&&boxs.y>address.y-10&&j==0)
{
if(yVel<0)
{
address.add1--;
i--;
CATCHER1=CATCHER_WIDTH_NUM[i];
if(i<0)
{
address.add1 ;
i ;
CATCHER1=CATCHER_WIDTH_NUM[i];
}
}
if(yVel>0)
{
address.add2--;
i--;
CATCHER2=CATCHER_WIDTH_NUM[i];
if(i<0)
{
address.add2 ;
i ;
CATCHER2=CATCHER_WIDTH_NUM[i];
}
}
address.frame=0;
address.show=0;
address.x=0;
address.y=0;
}
}
void Square::show_particles()
{
//Go through particles
for( int p = 0; p < TOTAL_PARTICLES; p )
{
//Delete and replace dead particles
if( particles[ p ]->is_dead() == true )
{
delete particles[ p ];
particles[ p ] = new Particle( boxs.x, boxs.y );
}
}
//Show particles
for( int p = 0; p < TOTAL_PARTICLES; p )
{
particles[ p ]->show();
}
}
void Square::Square_show()
{
apply_surface( 332, 198, message0, screen );
if(score1.x==0)
apply_surface( 343, 230, message10, screen );
if(score1.x==1)
apply_surface( 343, 230, message11, screen );
if(score1.x==2)
apply_surface( 343, 230, message12, screen );
if(score1.x==3)
apply_surface( 343, 230, message13, screen );
if(score1.x==4)
apply_surface( 343, 230, message14, screen );
if(score1.x==5)
apply_surface( 343, 230, message15, screen );
if(score2.x==0)
apply_surface( 343, 190, message20, screen );
if(score2.x==1)
apply_surface( 343, 190, message21, screen );
if(score2.x==2)
apply_surface( 343, 190, message22, screen );
if(score2.x==3)
apply_surface( 343, 190, message23, screen );
if(score2.x==4)
apply_surface( 343, 190, message24, screen );
if(score2.x==5)
apply_surface( 343, 190, message25, screen );
apply_surface( boxs.x, boxs.y, square, screen );
if(address.frame==300)
{
address.show=1;
address.x=random(50,650);
address.y=random(100,360);
j=2;
}
if(address.frame==600)
{
address.show=1;
address.x=random(50,650);
address.y=random(100,360);
j=0;
}
if(address.show==1)
{
if(j==2)
apply_surface(address.x,address.y,award,screen);
if(j==0)
apply_surface(address.x,address.y,punish,screen);
}
//Show the particles
show_particles();
if(judge1.x==true)
{
score1.x ;
regame.quit=true;
}
if(judge2.x==true)
{
score2.x ;
regame.quit=true;
}
if(score1.x==huihe_num.x)
{
Mix_HaltMusic();
apply_surface( 0,0,win1,screen );
}
if(score2.x==huihe_num.x)
{
Mix_HaltMusic();
apply_surface( 0,0,win2,screen );
}
}
Timer::Timer()
{
//Initialize the variables
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
}
void Timer::start()
{
//Start the timer
started = true;
//Unpause the timer
paused = false;
//Get the current clock time
startTicks = SDL_GetTicks();
}
void Timer::stop()
{
//Stop the timer
started = false;
//Unpause the timer
paused = false;
}
void Timer::pause()
{
//If the timer is running and isn't already paused
if( ( started == true ) && ( paused == false ) )
{
//Pause the timer
paused = true;
//Calculate the paused ticks
pausedTicks = SDL_GetTicks() - startTicks;
}
}
void Timer::unpause()
{
//If the timer is paused
if( paused == true )
{
//Unpause the timer
paused = false;
//Reset the starting ticks
startTicks = SDL_GetTicks() - pausedTicks;
//Reset the paused ticks
pausedTicks = 0;
}
}
int Timer::get_ticks()
{
//If the timer is running
if( started == true )
{
//If the timer is paused
if( paused == true )
{
//Return the number of ticks when the timer was paused
return pausedTicks;
}
else
{
//Return the current time minus the start time
return SDL_GetTicks() - startTicks;
}
}
//If the timer isn't running
return 0;
}
bool Timer::is_started()
{
return started;
}
bool Timer::is_paused()
{
return paused;
}
void date_clear()
{
to.x=false;
to.y=false;
to.z=false;
regame.quit=false;
judge1.x=false;
judge2.x=false;
score1.x=0;
score2.x=0;
to.re=false;
to.d1=false;
to.d2=false;
to.d3=false;
to.h1=false;
to.h2=false;
to.h2=false;
}
void date_init()
{
to.x=false;
to.y=false;
to.z=false;
to.re=false;
to.d1=false;
to.d2=false;
to.d3=false;
to.h1=false;
to.h2=false;
to.h2=false;
regame.x=false;
replay=false;
judge1.x=false;
judge2.x=false;
huihe.x=false;
difficulty.x=false;
mmenu.x=false;
mmenu.quit == false;
regame.quit=false;
judge1.quit=false;
judge2.quit=false;
huihe.quit=false;
difficulty.quit=false;
mmenu.quit=false;
score1.x=0;
score2.x=0;
address.x=0;
address.y=0;
address.show=0;
address.frame=0;
address.add1=1;
address.add2=1;
replay=false;
data.x=false;
CATCHER1=CATCHER_WIDTH_NUM[1];
CATCHER2=CATCHER_WIDTH_NUM[1];
i=1;
j=1;
}
void mouse_to_restart()
{
if( event.type == SDL_KEYDOWN )
{
if( event.key.keysym.sym == SDLK_SPACE )
{
if(score1.x==huihe_num.x||score2.x==huihe_num.x)
{
date_init();
data.x=true;
}
}
}
if( event.type == SDL_QUIT )
{
date_clear();
exit(0);
}
}
int game_vs_computer()
{
//The square
Square mySquare;
Catcher1 myCatcher1;
Catcher2 myCatcher2;
//The frame rate regulator
Timer fps;
//While the user hasn't quit
while( regame.quit == false )
{
//Start the frame timer
fps.start();
mySquare.Square_hit_events();
myCatcher2.computer_input2();
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
myCatcher1.handle_input1();
mouse_to_restart();
if( event.type == SDL_QUIT )
{
//Quit the program
regame.quit = true;
clean_up();
exit(0);
}
}
mySquare.Square_move();
myCatcher1.Catcher1_move();
myCatcher2.Catcher2_move();
//Fill the screen white
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
apply_surface( 0, 0, background, screen );
myCatcher1.Catcher1_show();
myCatcher2.Catcher2_show();
mySquare.Square_show();
if(data.x==true)
{
replay=true;
regame.quit=true;
}
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//Cap the frame rate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
}
int game_vs_player()
{
//The square
Square mySquare;
Catcher1 myCatcher1;
Catcher3 myCatcher3;
//The frame rate regulator
Timer fps;
//While the user hasn't quit
while( regame.quit == false )
{
//Start the frame timer
fps.start();
mySquare.Square_hit_events();
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//Handle events for the square
myCatcher1.handle_input1();
myCatcher3.handle_input3();
mouse_to_restart();
if( event.type == SDL_QUIT )
{
//Quit the program
regame.quit = true;
clean_up();
exit(0);
}
}
mySquare.Square_move();
myCatcher1.Catcher1_move();
myCatcher3.Catcher3_move();
apply_surface( 0, 0, background, screen );
myCatcher1.Catcher1_show();
myCatcher3.Catcher3_show();
mySquare.Square_show();
if(data.x==true)
{
replay=true;
regame.quit=true;
}
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//Cap the frame rate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
}
void set_clips()
{
//Clip the sprite sheet
clips[ CLIP_MOUSEAOVER ].x =0;
clips[ CLIP_MOUSEAOVER ].y = 70;
clips[ CLIP_MOUSEAOVER ].w = 300;
clips[ CLIP_MOUSEAOVER ].h = 70;
clips[ CLIP_MOUSEAOUT ].x = 0;
clips[ CLIP_MOUSEAOUT ].y = 0;
clips[ CLIP_MOUSEAOUT ].w = 300;
clips[ CLIP_MOUSEAOUT ].h = 70;
clips[ CLIP_MOUSEBOUT ].x = 300;
clips[ CLIP_MOUSEBOUT ].y = 0;
clips[ CLIP_MOUSEBOUT ].w = 300-1;
clips[ CLIP_MOUSEBOUT ].h = 70;
clips[ CLIP_MOUSEBOVER ].x = 300;
clips[ CLIP_MOUSEBOVER ].y = 70;
clips[ CLIP_MOUSEBOVER ].w = 300-1;
clips[ CLIP_MOUSEBOVER ].h = 70;
clips[ CLIP_MOUSECOVER ].x =600-1;
clips[ CLIP_MOUSECOVER ].y = 70;
clips[ CLIP_MOUSECOVER ].w = 300;
clips[ CLIP_MOUSECOVER ].h = 70;
clips[ CLIP_MOUSECOUT ].x = 600-1;
clips[ CLIP_MOUSECOUT ].y = 0;
clips[ CLIP_MOUSECOUT ].w = 300;
clips[ CLIP_MOUSECOUT ].h = 70;
clips[ CLIP_MOUSEDOUT ].x = 900;
clips[ CLIP_MOUSEDOUT ].y = 0;
clips[ CLIP_MOUSEDOUT ].w = 300-2;
clips[ CLIP_MOUSEDOUT ].h = 70;
clips[ CLIP_MOUSEDOVER ].x = 900;
clips[ CLIP_MOUSEDOVER ].y = 70;
clips[ CLIP_MOUSEDOVER ].w = 300-2;
clips[ CLIP_MOUSEDOVER ].h = 70;
clips[ CLIP_MOUSEEOVER ].x =0;
clips[ CLIP_MOUSEEOVER ].y = 255;
clips[ CLIP_MOUSEEOVER ].w = 230;
clips[ CLIP_MOUSEEOVER ].h = 115;
clips[ CLIP_MOUSEEOUT ].x = 0;
clips[ CLIP_MOUSEEOUT ].y = 140;
clips[ CLIP_MOUSEEOUT ].w = 230;
clips[ CLIP_MOUSEEOUT ].h = 115;
clips[ CLIP_MOUSEFOUT ].x = 230;
clips[ CLIP_MOUSEFOUT ].y = 140;
clips[ CLIP_MOUSEFOUT ].w = 230;
clips[ CLIP_MOUSEFOUT ].h = 115;
clips[ CLIP_MOUSEFOVER ].x = 230;
clips[ CLIP_MOUSEFOVER ].y = 255;
clips[ CLIP_MOUSEFOVER ].w = 230;
clips[ CLIP_MOUSEFOVER ].h = 115;
clips[ CLIP_MOUSEGOVER ].x =460;
clips[ CLIP_MOUSEGOVER ].y = 255;
clips[ CLIP_MOUSEGOVER ].w = 230;
clips[ CLIP_MOUSEGOVER ].h = 115;
clips[ CLIP_MOUSEGOUT ].x = 460;
clips[ CLIP_MOUSEGOUT ].y = 140;
clips[ CLIP_MOUSEGOUT ].w = 230;
clips[ CLIP_MOUSEGOUT ].h = 115;
clips[ CLIP_MOUSEHOVER ].x = 697;
clips[ CLIP_MOUSEHOVER ].y = 183;
clips[ CLIP_MOUSEHOVER ].w = 110;
clips[ CLIP_MOUSEHOVER ].h = 45;
clips[ CLIP_MOUSEHOUT ].x = 697;
clips[ CLIP_MOUSEHOUT ].y = 138;
clips[ CLIP_MOUSEHOUT ].w = 110;
clips[ CLIP_MOUSEHOUT ].h = 45;
clips[ CLIP_MOUSEIOUT ].x = 806;
clips[ CLIP_MOUSEIOUT ].y = 138;
clips[ CLIP_MOUSEIOUT ].w = 110;
clips[ CLIP_MOUSEIOUT ].h = 45;
clips[ CLIP_MOUSEIOVER ].x = 806;
clips[ CLIP_MOUSEIOVER ].y = 183;
clips[ CLIP_MOUSEIOVER ].w = 110;
clips[ CLIP_MOUSEIOVER ].h = 45;
clips[ CLIP_MOUSEJOVER ].x = 916;
clips[ CLIP_MOUSEJOVER ].y = 183;
clips[ CLIP_MOUSEJOVER ].w = 110;
clips[ CLIP_MOUSEJOVER ].h = 45;
clips[ CLIP_MOUSEJOUT ].x = 916;
clips[ CLIP_MOUSEJOUT ].y = 138;
clips[ CLIP_MOUSEJOUT ].w = 110;
clips[ CLIP_MOUSEJOUT ].h = 45;
}
ButtonA::ButtonA( int x, int y, int w, int h )
{
//Set the button's attributes
boxA.x = x;
boxA.y = y;
boxA.w = w;
boxA.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSEAOUT ];
}
ButtonB::ButtonB( int x, int y, int w, int h )
{
//Set the button's attributes
boxB.x = x;
boxB.y = y;
boxB.w = w;
boxB.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSEBOUT ];
}
ButtonC::ButtonC( int x, int y, int w, int h )
{
//Set the button's attributes
boxC.x = x;
boxC.y = y;
boxC.w = w;
boxC.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSECOUT ];
}
ButtonD::ButtonD( int x, int y, int w, int h )
{
//Set the button's attributes
boxD.x = x;
boxD.y = y;
boxD.w = w;
boxD.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSEDOUT ];
}
Buttond1::Buttond1( int x, int y, int w, int h )
{
//Set the button's attributes
boxd1.x = x;
boxd1.y = y;
boxd1.w = w;
boxd1.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSEEOUT ];
}
Buttond2::Buttond2( int x, int y, int w, int h )
{
//Set the button's attributes
boxd2.x = x;
boxd2.y = y;
boxd2.w = w;
boxd2.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSEFOUT ];
}
Buttond3::Buttond3( int x, int y, int w, int h )
{
//Set the button's attributes
boxd3.x = x;
boxd3.y = y;
boxd3.w = w;
boxd3.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSEGOUT ];
}
Buttonh1::Buttonh1( int x, int y, int w, int h )
{
//Set the button's attributes
boxh1.x = x;
boxh1.y = y;
boxh1.w = w;
boxh1.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSEHOUT ];
}
Buttonh2::Buttonh2( int x, int y, int w, int h )
{
//Set the button's attributes
boxh2.x = x;
boxh2.y = y;
boxh2.w = w;
boxh2.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSEIOUT ];
}
Buttonh3::Buttonh3( int x, int y, int w, int h )
{
//Set the button's attributes
boxh3.x = x;
boxh3.y = y;
boxh3.w = w;
boxh3.h = h;
//Set the default sprite
clip = &clips[ CLIP_MOUSEJOUT ];
}
void ButtonA::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
//If the mouse is over the button
if( ( x > boxA.x ) && ( x < boxA.x boxA.w ) && ( y > boxA.y ) && ( y < boxA.y boxA.h ) )
{
mm.a ;
clip = &clips[ CLIP_MOUSEAOVER ];
if(mm.a==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.a=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSEAOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxA.x ) && ( x < boxA.x boxA.w ) && ( y > boxA.y ) && ( y < boxA.y boxA.h ) )
{
to.y=true;
}
}
}
}
void ButtonB::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
if( ( x > boxB.x ) && ( x < boxB.x boxB.w ) && ( y > boxB.y ) && ( y < boxB.y boxB.h ) )
{
mm.b ;
//Set the button sprite
clip = &clips[ CLIP_MOUSEBOVER ];
if(mm.b==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.b=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSEBOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxB.x ) && ( x < boxB.x boxB.w ) && ( y > boxB.y ) && ( y < boxB.y boxB.h ) )
{
to.x=true;
}
}
}
}
void ButtonC::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
if( ( x > boxC.x ) && ( x < boxC.x boxC.w ) && ( y > boxC.y ) && ( y < boxC.y boxC.h ) )
{
mm.c ;
//Set the button sprite
clip = &clips[ CLIP_MOUSECOVER ];
if(mm.c==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.c=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSECOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxC.x ) && ( x < boxC.x boxC.w ) && ( y > boxC.y ) && ( y < boxC.y boxC.h ) )
{
to.z=true;
}
}
}
}
void ButtonD::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
if( ( x > boxD.x ) && ( x < boxD.x boxD.w ) && ( y > boxD.y ) && ( y < boxD.y boxD.h ) )
{
mm.d ;
//Set the button sprite
clip = &clips[ CLIP_MOUSEDOVER ];
if(mm.d==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.d=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSEDOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxD.x ) && ( x < boxD.x boxD.w ) && ( y > boxD.y ) && ( y < boxD.y boxD.h ) )
{
clean_up();
exit(0);
}
}
}
}
void Buttond1::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
if( ( x > boxd1.x ) && ( x < boxd1.x boxd1.w ) && ( y > boxd1.y ) && ( y < boxd1.y boxd1.h ) )
{
mm.d1 ;
//Set the button sprite
clip = &clips[ CLIP_MOUSEEOVER ];
if(mm.d1==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.d1=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSEEOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxd1.x ) && ( x < boxd1.x boxd1.w ) && ( y > boxd1.y ) && ( y < boxd1.y boxd1.h ) )
{
to.d1=true;
difficulty_v=40;
}
}
}
}
void Buttond2::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
if( ( x > boxd2.x ) && ( x < boxd2.x boxd2.w ) && ( y > boxd2.y ) && ( y < boxd2.y boxd2.h ) )
{
mm.d2 ;
//Set the button sprite
clip = &clips[ CLIP_MOUSEFOVER ];
if(mm.d2==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.d2=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSEFOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxd2.x ) && ( x < boxd2.x boxd2.w ) && ( y > boxd2.y ) && ( y < boxd2.y boxd2.h ) )
{
to.d2=true;
difficulty_v=25;
}
}
}
}
void Buttond3::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
if( ( x > boxd3.x ) && ( x < boxd3.x boxd3.w ) && ( y > boxd3.y ) && ( y < boxd3.y boxd3.h ) )
{
mm.d3 ;
//Set the button sprite
clip = &clips[ CLIP_MOUSEGOVER ];
if(mm.d3==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.d3=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSEGOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxd3.x ) && ( x < boxd3.x boxd3.w ) && ( y > boxd3.y ) && ( y < boxd3.y boxd3.h ) )
{
to.d3=true;
difficulty_v=10;
}
}
}
}
void Buttonh1::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
if( ( x > boxh1.x ) && ( x < boxh1.x boxh1.w ) && ( y > boxh1.y ) && ( y < boxh1.y boxh1.h ) )
{
mm.h1 ;
//Set the button sprite
clip = &clips[ CLIP_MOUSEHOVER ];
if(mm.h1==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.h1=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSEHOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxh1.x ) && ( x < boxh1.x boxh1.w ) && ( y > boxh1.y ) && ( y < boxh1.y boxh1.h ) )
{
to.h1=true;
huihe_num.x=1;
}
}
}
}
void Buttonh2::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
if( ( x > boxh2.x ) && ( x < boxh2.x boxh2.w ) && ( y > boxh2.y ) && ( y < boxh2.y boxh2.h ) )
{
mm.h2 ;
//Set the button sprite
clip = &clips[ CLIP_MOUSEIOVER ];
if(mm.h2==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.h2=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSEIOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxh2.x ) && ( x < boxh2.x boxh2.w ) && ( y > boxh2.y ) && ( y < boxh2.y boxh2.h ) )
{
to.h2=true;
huihe_num.x=3;
}
}
}
}
void Buttonh3::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
if( ( x > boxh3.x ) && ( x < boxh3.x boxh3.w ) && ( y > boxh3.y ) && ( y < boxh3.y boxh3.h ) )
{
mm.h3 ;
//Set the button sprite
clip = &clips[ CLIP_MOUSEJOVER ];
if(mm.h3==1)
Mix_PlayChannel( -1, mouse, 0 );
}
//If not
else
{
mm.h3=0;
//Set the button sprite
clip = &clips[ CLIP_MOUSEJOUT ];
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
//如果鼠标左键被按下了
if( event.button.button == SDL_BUTTON_LEFT )
{
//获取鼠标坐标
x = event.button.x;
y = event.button.y;
if( ( x > boxh3.x ) && ( x < boxh3.x boxh3.w ) && ( y > boxh3.y ) && ( y < boxh3.y boxh3.h ) )
{
to.h3=true;
huihe_num.x=5;
}
}
}
}
void ButtonA::show()
{
//Show the button
apply_surface( boxA.x, boxA.y, buttonSheet, screen, clip );
}
void ButtonB::show()
{
//Show the button
apply_surface( boxB.x, boxB.y, buttonSheet, screen, clip );
}
void ButtonC::show()
{
//Show the button
apply_surface( boxC.x, boxC.y, buttonSheet, screen, clip );
}
void ButtonD::show()
{
//Show the button
apply_surface( boxD.x, boxD.y, buttonSheet, screen, clip );
}
void Buttond1::show()
{
//Show the button
apply_surface( boxd1.x, boxd1.y, buttonSheet, screen, clip );
}
void Buttond2::show()
{
//Show the button
apply_surface( boxd2.x, boxd2.y, buttonSheet, screen, clip );
}
void Buttond3::show()
{
//Show the button
apply_surface( boxd3.x, boxd3.y, buttonSheet, screen, clip );
}
void Buttonh1::show()
{
//Show the button
apply_surface( boxh1.x, boxh1.y, buttonSheet, screen, clip );
}
void Buttonh2::show()
{
//Show the button
apply_surface( boxh2.x, boxh2.y, buttonSheet, screen, clip );
}
void Buttonh3::show()
{
//Show the button
apply_surface( boxh3.x, boxh3.y, buttonSheet, screen, clip );
}
int menu()
{
apply_surface( 0, 0, mbg, screen );
//Clip the sprite sheet return 0;
set_clips();
//Make the button
ButtonA myButtonA( 200, 120, 300, 70 );
ButtonB myButtonB( 200, 190, 300, 70 );
ButtonC myButtonC( 200, 260, 300, 70 );
ButtonD myButtonD( 200, 330, 300, 70 );
//While the user hasn't quit
while( mmenu.quit == false )
{
//If there's events to handle
if( SDL_PollEvent( &event ) )
{
//Handle button events
myButtonA.handle_events();
myButtonB.handle_events();
myButtonC.handle_events();
myButtonD.handle_events();
if(to.x==true||to.y==true||to.z==true)
mmenu.quit=true;
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
exit (0);
}
}
//Show the button
myButtonA.show();
myButtonB.show();
myButtonC.show();
myButtonD.show();
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
return 0;
}
int the_difficulty()
{
Buttond1 myButtond1( 220, 70, 230, 115 );
Buttond2 myButtond2( 220, 185, 230, 115 );
Buttond3 myButtond3( 220, 300, 230, 115 );
//While the user hasn't quit
while( difficulty.quit == false )
{
//If there's events to handle
if( SDL_PollEvent( &event ) )
{
//Handle button events
myButtond1.handle_events();
myButtond2.handle_events();
myButtond3.handle_events();
if( to.d1==true||to.d2==true||to.d3==true )
difficulty.quit=true;
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
exit (0);
}
}
apply_surface( 0,0,db,screen );
//Show the button
myButtond1.show();
myButtond2.show();
myButtond3.show();
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
return 0;
}
int the_huihe()
{
Buttonh1 myButtonh1( 277, 90, 110, 45 );
Buttonh2 myButtonh2( 270, 205, 110, 45 );
Buttonh3 myButtonh3( 270, 320, 110, 45 );
//While the user hasn't quit
while( huihe.quit == false )
{
//If there's events to handle
if( SDL_PollEvent( &event ) )
{
//Handle button events
myButtonh1.handle_events();
myButtonh2.handle_events();
myButtonh3.handle_events();
if( to.h1==true||to.h2==true||to.h3==true )
huihe.quit=true;
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
exit (0);
}
}
//Fill the screen white
apply_surface( 0,0,db,screen );
//Show the button
myButtonh1.show();
myButtonh2.show();
myButtonh3.show();
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
return 0;
}
int to_tip()
{
bool quit = false;
int x=0,y=0;
while( quit == false )
{
apply_surface( 0, 0, tip, screen );
apply_surface( 585, 415, return1, screen );
while( SDL_PollEvent( &event ) )
{
if( event.type == SDL_MOUSEMOTION )
{
x = event.motion.x;
y = event.motion.y;
if( ( x > 585 ) && ( x < 585 135 ) && ( y > 415 ) && ( y < 415 65 ) )
{
apply_surface( 585, 415, return2, screen );
}
}
if( event.type == SDL_MOUSEBUTTONUP )
{
if( event.button.button == SDL_BUTTON_LEFT )
{
x = event.button.x;
y = event.button.y;
if( ( x > 585 ) && ( x < 585 135 ) && ( y > 415 ) && ( y < 415 65 ) )
{
to.re=true;
}
}
}
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
exit(0);
}
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
if(to.re==true)
quit=true;
}
return 0;
}
int main( int argc, char* args[] )
{
bool restart=false;
set_clips();
if( init() == false )
{
return 1;
}
if( load_files() == false )
{
return 1;
}
while(restart==false)
{
date_clear();
replay==false;
date_init();
if( Mix_PlayingMusic() == 0 )
{
//Play the music
if( Mix_PlayMusic( music, -1 ) == -1 )
{
return 1;
}
}
Mix_PauseMusic();
if( menu() == 1 )
return 1;
if( to.x==true )
{
if(the_difficulty()==1)
return 1;
if(the_huihe()==1)
return 1;
while(replay==false)
{
if( Mix_PausedMusic() == 1 )
{
Mix_ResumeMusic();
}
regame.quit=false;
judge1.x=false;
judge2.x=false;
if( game_vs_computer() == 1 )
return 1;
}
}
if( to.y==true )
{
if(the_huihe()==1)
return 1;
while(replay==false)
{
if( Mix_PausedMusic() == 1 )
{
Mix_ResumeMusic();
}
regame.quit=false;
judge1.x=false;
judge2.x=false;
if( game_vs_player() == 1 )
return 1;
}
}
if( to.z==true )
{
if(to_tip()==1)
return 1;
}
}
clean_up();
return 0;
}