SDL小尝试,是男人就坚持20秒
2010-03-03 21:49
357 查看
今天在电脑里掏出来自己以前试着使用SDL游戏引擎的时候做的一个简单版 是男人就坚持20秒的小游戏。。
玩家通过键盘WSAD操作人物躲避四面八方来的物体,看最终能坚持多长时间。
图片是随便在网上找的或者自己画的。
GAME OVER
简单的贴一下代码~
玩家通过键盘WSAD操作人物躲避四面八方来的物体,看最终能坚持多长时间。
图片是随便在网上找的或者自己画的。
GAME OVER
简单的贴一下代码~
#pragma comment(lib, "sdl/lib/SDL.lib") #pragma comment(lib, "sdl/lib/SDLmain.lib") #pragma comment(lib, "sdl/lib/SDL_ttf.lib") #include "sdl/include/SDL.h" #include "sdl/include/SDL_main.h" #include "sdl/include/SDL_ttf.h" #include <iostream> #include <stdlib.h> #include <vector> #include <math.h> #include <time.h> using namespace std; SDL_Surface* screen = NULL; SDL_Surface* background = NULL; SDL_Surface* plane = NULL; SDL_Surface* ballimage = NULL; SDL_Surface* gameover = NULL; SDL_Surface* planeGameOver = NULL; SDL_Surface* textimage = NULL; TTF_Font* font=NULL; SDL_Color BLACK = { 0, 100, 100, 0 }; SDL_Event gameevent; Uint32 starttime; Uint32 gamestart; bool isGameOver = false; struct Player { int x; int y; } player; struct BallType { float x; float y; float dx; float dy; }; vector<BallType> balls; int const dx = 1; int const dy = 1; int const speed = 5; int BALLSSTART = 20; int MAXBALLS = 20; int const FPS = 50; void init() { SDL_Init( SDL_INIT_VIDEO ); screen = SDL_SetVideoMode( 640, 480, 16, SDL_SWSURFACE ); SDL_WM_SetCaption( "True Man 20 seconds", NULL ); background = SDL_LoadBMP( "res/background.bmp" ); plane = SDL_LoadBMP( "res/plane.bmp" ); ballimage = SDL_LoadBMP( "res/ball.bmp" ); gameover = SDL_LoadBMP( "res/gameover.bmp" ); planeGameOver = SDL_LoadBMP( "res/planeover.bmp" ); Uint32 colorkey = SDL_MapRGB( plane->format, 0x0, 0x0, 0x0 ); SDL_SetColorKey( background, SDL_SRCCOLORKEY, colorkey ); SDL_SetColorKey( plane, SDL_SRCCOLORKEY, colorkey ); SDL_SetColorKey( ballimage, SDL_SRCCOLORKEY, colorkey ); SDL_SetColorKey( gameover, SDL_SRCCOLORKEY, colorkey ); SDL_SetColorKey( planeGameOver, SDL_SRCCOLORKEY, colorkey ); TTF_Init(); font = TTF_OpenFont("res/times.ttf", 16); srand( time(0) ); } void reset() { gamestart = SDL_GetTicks(); player.x = 300; player.y = 200; isGameOver = false; cout<<"start clear"<<endl; balls.clear(); cout<<balls.size()<<endl; /* vector<BallType>::iterator iter; for(iter=balls.begin();iter!=balls.end();++iter) { balls.erase(iter); } */ cout<<"end clear"<<endl; MAXBALLS = BALLSSTART; } void finish(); void handle() { SDL_PollEvent( &gameevent ); if( gameevent.type == SDL_QUIT ) { finish(); exit(0); } /* else if( gameevent.type == SDL_KEYDOWN ) { switch( gameevent.key.keysym.sym ) { case SDLK_UP: case SDLK_w: player.y -= dy; break; case SDLK_DOWN: case SDLK_s: player.y += dy; break; case SDLK_LEFT: case SDLK_a: player.x -= dx; break; case SDLK_RIGHT: case SDLK_d: player.x += dx; break; default: break; } } */ Uint8 *keystates = SDL_GetKeyState( NULL ); if( keystates[ SDLK_UP ] || keystates[ SDLK_w ] ) player.y-=dy*speed; if( keystates[ SDLK_DOWN ] || keystates[ SDLK_s ] ) player.y+=dy*speed; if( keystates[ SDLK_LEFT ] || keystates[ SDLK_a ] ) player.x-=dx*speed; if( keystates[ SDLK_RIGHT ] || keystates[ SDLK_d ] ) player.x+=dx*speed; } void judgePlayerPos() { if( player.x<=0 ) player.x = 0; if( player.x + plane->w >=640 ) player.x = 640 - plane->w ; if( player.y<=0 ) player.y = 0; if( player.y + plane->h >=480 ) player.y = 480 - plane->h; } void MakeBall() { BallType ball; int tmp = rand()%4; switch(tmp) { case 0: ball.x = rand()%640; ball.y = 0; break; case 1: ball.x = rand()%640; ball.y = 480; break; case 2: ball.x = 0; ball.y = rand()%480; break; case 3: ball.x = 640; ball.y = rand()%480; break; default: break; } float dx = player.x - ball.x; float dy = player.y - ball.y; float m = sqrt( dx*dx + dy*dy ); int speed = rand()%2 + 2; ball.dy = (dy / m)*speed; ball.dx = (dx / m)*speed; balls.push_back(ball); } void BallsMove() { vector<BallType>::iterator iter; for( iter = balls.begin(); iter != balls.end(); ++iter ) { iter->x += iter->dx; iter->y += iter->dy; if( iter->x < 0 || iter->x > 640 || iter->y < 0 || iter->y > 480 ) balls.erase( iter ); } } float const dR = 20; void CheckHit() { vector<BallType>::iterator iter; for( iter = balls.begin(); iter != balls.end(); ++iter ) { float c1x = iter->x + (ballimage->w/2); float c1y = iter->y + (ballimage->h/2); float c1r = sqrt(2.0) * (ballimage->w/2); float c2x = player.x + plane->w/2; float c2y = player.y + plane->h/2; float c2r = sqrt(2.0) * (plane->w/2); float distance = sqrt((c1x-c2x)*(c1x-c2x) + (c1y-c2y)*(c1y-c2y)); if( distance <= c1r + c2r - dR ) { isGameOver = true; return; } } } void logic() { judgePlayerPos(); cout<<balls.size()<<endl; if(balls.size()<MAXBALLS) MakeBall(); BallsMove(); int blockadd = rand()%30; if(blockadd == 0) MAXBALLS++; } void show() { SDL_Rect offset; offset.x = 0; offset.y = 0; SDL_BlitSurface( background, NULL, screen, &offset ); offset.x = player.x; offset.y = player.y; offset.w = plane->w; offset.h = plane->h; if( isGameOver ) SDL_BlitSurface( planeGameOver, NULL, screen, &offset ); else SDL_BlitSurface( plane, NULL, screen, &offset ); vector<BallType>::iterator iter; for( iter = balls.begin(); iter != balls.end(); ++iter ) { offset.x = (int)(iter->x); offset.y = (int)(iter->y); SDL_BlitSurface( ballimage, NULL, screen, &offset ); } SDL_UpdateRect(screen, 0, 0, 640, 480); } void delay() { Uint32 rightnow = SDL_GetTicks(); if( rightnow - starttime < 1000 / FPS ) { SDL_Delay( ( 1000 / FPS ) - ( rightnow - starttime ) ); } } void finish() { TTF_CloseFont( font ); TTF_Quit(); SDL_FreeSurface( planeGameOver ); SDL_FreeSurface( gameover ); SDL_FreeSurface( screen ); SDL_FreeSurface( background ); SDL_FreeSurface( plane ); SDL_FreeSurface( ballimage ); SDL_Quit(); } void showGameOver() { SDL_Rect offset; offset.x = 100; offset.y = 30; SDL_BlitSurface( gameover, NULL, screen, &offset ); //SDL_Delay( 2000 ); Uint32 nowtime = SDL_GetTicks() - gamestart; char s[20]; itoa( nowtime, s, 10 ); textimage = TTF_RenderText_Blended(font, s, BLACK ); offset.x = 300; offset.y = 400; SDL_BlitSurface( textimage, NULL, screen, &offset ); SDL_FreeSurface( textimage ); SDL_UpdateRect(screen, 0, 0, 640, 480); } int main(int argc, char *argv[]) { init(); while(1) { cout<<"game restart"<<endl; reset(); while( isGameOver==false ) { cout<<"game looping"<<endl; starttime = SDL_GetTicks(); handle(); logic(); show(); CheckHit(); delay(); } cout<<balls.size()<<endl; cout<<"game over"<<endl; show(); cout<<"##############1##"<<balls.size()<<endl; showGameOver(); cout<<"##############2##"<<balls.size()<<endl; bool restart = false; cout<<"##############3##"<<balls.size()<<endl; while(restart==false) { cout<<"geting space pressed"<<endl; SDL_PollEvent( &gameevent ); if(gameevent.type==SDL_QUIT) { finish(); exit(0); } if(gameevent.key.keysym.sym==SDLK_SPACE ||gameevent.key.keysym.sym==SDLK_KP_ENTER) restart = true; SDL_Delay( 50 ); } } return 0; }
相关文章推荐
- CocosCreator手记04——是男人就坚持20秒Remastered (TypeScript版)
- cocos2d-x游戏例子01:是男人就坚持20秒(WIN32)
- javascript版是男人就坚持20秒吧
- 是男人就坚持20秒 java的swing弹弹球GAME
- 是男人就坚持20秒—python版本
- Java小游戏——是男人就坚持20秒
- 【AS3代码】是男人就坚持30秒
- cocos2d-x for wp 之Box2D游戏-是男人就坚持60M(bug篇)(二)
- 这个男人坚持了八年,撑起了无数人的IT之梦!
- 对了还是错了?男人,不后悔,坚持梦想,永不放弃
- 对男人不要尝试的十件事[女士必看]
- cocos2dx-是男人就坚持20s 练手项目
- 坚持#第205天~培训前尽力学习云计算,终于100套那个弄完了,我是有梦想的男人
- html5 仿"是男人就坚持20s"游戏
- 男人帮之程序员坚持语录
- 尝试坚持做一件事情
- 坚持#第99天~尝试把忙的事情简单化、快速化!
- 尝试着作一个成熟的男人!
- 是男人就坚持100下(强力益智,请留下战绩)
- html5做的 是男人就坚持10秒