- 浏览: 161687 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
沙舟狼客:
为了方便使用可以配置到环境变量里面MINGW_HOME=C:\ ...
windows下用mingw32+sdl进行简单2d游戏开发(c语言) -
沙舟狼客:
如果安装autotools时不用gcccc相当于gcc的链接n ...
windows下用mingw32+sdl进行简单2d游戏开发(c语言) -
lirihong:
java中文乱码完全解决方案 ?? 高度很高,深度、全面度全 ...
java中文乱码完全解决方案 -
沙舟狼客:
非常适合想写windows游戏的菜鸟
windows下用mingw32+sdl进行简单2d游戏开发(c语言) -
xixilive:
噢喔~~语义全无
京东导航的jquery实现
#include<stdlib.h>
#include<SDL/SDL.h>
#include<SDL/SDL_image.h>
#include<time.h>
SDL_Surface *screen;
SDL_Surface *bg,*fish,*fimg;
SDL_Surface *img0,*img1,*img2,*img3,*img4,*img5,*img6,*img7,*img8,*img9;//number picture
SDL_Surface *gate1,*gate2,*gate3,*gate4,*gate5,*gate6,*gate7,*gate8,*gate9;//gate picture
SDL_Surface *f1,*f2,*f3,*f4,*f5;//5 fish picture
SDL_Surface *e1,*e2,*e3;//enemy pic
SDL_Surface *gBox;
SDL_Surface *sMain,*helpBut,*startBut,*helpMain,*returnBut1,*returnBut2;
SDL_Surface *gOver,*returnMain1,*returnMain2,*restart1,*restart2,*gfinish;
SDL_Surface *gcup,*scorea,*gate;
Uint32 color;
int len=1;//fish length
int x=0,y=240,speed=2;
int state=0;//0没开始;1游戏开始;2游戏暂停;
int score=0,live=0;//score;live 0:die,1:live;
int foodCount=5;//food count
int enemyCount=1;//enemy count
//creat snake struct
struct snake{
int x;
int y;
}s[100];
//creact cup struct
struct Cup{
int m;
int n;
}cup[9];
//creat food struct
struct Food{
float x,y;
int isc;
}food[10];
//creat enemy struct
struct Enemy{
float x,y;
int die;
}enemy[10];
//creat gold box struct
struct Box{
int x,y;
int isExist;//0:noExist;1:Exist;
}box;
//initab game video
void init(){
if(SDL_Init(SDL_INIT_VIDEO)<0){
fprintf(stderr,"Can't init video!%s\n",SDL_GetError());
exit(1);
}
screen=SDL_SetVideoMode(500,600,16,SDL_SWSURFACE);
if(screen==NULL){
fprintf(stderr,"Can't init video!%s\n",SDL_GetError());
exit(1);
}
SDL_WM_SetCaption("GreekSnake",NULL);
atexit(SDL_Quit);
}
//load a image(bmp,jpeg,jpg,gif,png)
SDL_Surface *load(char *file){
SDL_Surface *img;
img=IMG_Load(file);
if(img==NULL){
fprintf(stderr,"Can't init video!%s\n",SDL_GetError());
exit(1);
}
return img;
}
//paint game background
int imgx=0;
void DrawBG(int x,int y){
SDL_Rect dest,imgDest;
dest.x=x;
dest.y=y+50;
if(imgx<2500){
imgDest.x=imgx;
}else{
imgx=0;
imgDest.x=imgx;
}
imgx+=1;
imgDest.y=y;
imgDest.w=500;
imgDest.h=bg->h;
SDL_BlitSurface(bg,&imgDest,screen,&dest);
}
//paint any picture in window
void DrawIMG(SDL_Surface *img,int x,int y){
SDL_Rect dest;
dest.x=x;
dest.y=y+50;
SDL_BlitSurface(img,NULL,screen,&dest);
}
//change snake struct x,y;and draw snake body;
void game(){
printf("%d _____________\n",len);
int i;
for(i=len-1;i>=0;i--){
s[i].x=s[i-1].x-32;
s[i].y=s[i-1].y;
}
s[0].x=x;
s[0].y=y;
for(i=0;i<len;i++){
DrawIMG(fish,s[i].x,s[i].y);
}
}
//gameover
void gamePlay();
void initMain();
void gameInit();
void gameStart();
void gameOver(){
int ix=1,mouseX,mouseY,bX,bY;
while(1){
SDL_Event event;
while(SDL_PollEvent(&event)){
switch (event.type) {
case SDL_MOUSEMOTION:
mouseX=event.motion.x;
mouseY=event.motion.y;
if(((mouseX>=195)&&(mouseX<=315))&&((mouseY>=320)&&(mouseY<=370))){
ix=1;
}else if(((mouseX>=195)&&(mouseX<=315))&&((mouseY>=430)&&(mouseY<=480))){
ix=2;
}else{
ix=3;
}
break;
case SDL_MOUSEBUTTONDOWN:
if(event.button.button==1){
bX=event.button.x;
bY= event.button.y;
printf("________%d----%d__\n",bX,bY);
if(((bX>=195)&&(bX<=315))&&((bY>=320)&&(bY<=370))){
//ix=1;
color=SDL_MapRGB(screen->format,0,0,0);
SDL_FillRect(screen,&screen->clip_rect,color);
gamePlay();
score=0;
printf("restart\n");
}else if(((bX>=195)&&(bX<=315))&&((bY>=430)&&(bY<=480))){
//ix=2;
initMain();
}else{
//ix=3;
}
}
break;
case SDL_QUIT:
exit(0);
}
}
DrawIMG(gOver,0,0);
if(ix==1){
DrawIMG(restart2,195,270);
DrawIMG(returnMain1,195,377);
}else if(ix==2){
DrawIMG(restart1,195,270);
DrawIMG(returnMain2,195,377);
}else if(ix==3){
DrawIMG(restart1,195,270);
DrawIMG(returnMain1,195,377);
}
SDL_Flip(screen);
}
}
void gameFinish(){
int ix=1,mouseX,mouseY,bX,bY;
while(1){
SDL_Event event;
while(SDL_PollEvent(&event)){
switch (event.type) {
case SDL_MOUSEMOTION:
mouseX=event.motion.x;
mouseY=event.motion.y;
if(((mouseX>=195)&&(mouseX<=315))&&((mouseY>=280)&&(mouseY<=330))){
ix=1;
}else if(((mouseX>=195)&&(mouseX<=315))&&((mouseY>=390)&&(mouseY<=440))){
ix=2;
}else{
ix=3;
}
break;
case SDL_MOUSEBUTTONDOWN:
if(event.button.button==1){
bX=event.button.x;
bY= event.button.y;
printf("________%d----%d__\n",bX,bY);
if(((bX>=195)&&(bX<=315))&&((bY>=280)&&(bY<=330))){
//ix=1;
gamePlay();
//gameInit();
//gameStart();
printf("restart\n");
}else if(((bX>=195)&&(bX<=315))&&((bY>=390)&&(bY<=440))){
//ix=2;
initMain();
}else{
//ix=3;
}
}
break;
case SDL_QUIT:
exit(0);
}
}
DrawIMG(gfinish,0,0);
if(ix==1){
DrawIMG(restart2,195,230);
DrawIMG(returnMain1,195,337);
}else if(ix==2){
DrawIMG(restart1,195,230);
DrawIMG(returnMain2,195,337);
}else if(ix==3){
DrawIMG(restart1,195,230);
DrawIMG(returnMain1,195,337);
}
SDL_Flip(screen);
}
}
//random creat food X,Y
float fx=100,fy=100;
void rXY(int i){
fx=468;
//fx=1+(int)(468.00*rand()/(RAND_MAX+1.00));
//int i;
//for(i=0;i<5;i++){
fy=1+(466.00*rand()/(RAND_MAX+1.00));
food[i].x=fx;
food[i].y=fy;
food[i].isc=1;
//}
}
//paint food
void rFood(){
int i;
for(i=0;i<foodCount;i++){
food[i].x=food[i].x-(i+0.3);
if(food[i].x<0){
food[i].x=468;
}
food[i].isc=1;
}
DrawIMG(f1,food[0].x,food[0].y);
DrawIMG(f2,food[1].x,food[1].y);
DrawIMG(f3,food[2].x,food[2].y);
DrawIMG(f4,food[3].x,food[3].y);
DrawIMG(f5,food[4].x,food[4].y);
}
//hit food
void hitFood(){
int d1,d2,i;
float r=32.00,d;
for(i=0;i<5;i++){
d1=abs(s[0].x-food[i].x);
d2=abs(s[0].y-food[i].y);
d=sqrt(d1*d1+d2*d2);//the length of food to fish
if(d<r){
score++;
if((score%50)==0){
len=len+1;
enemyCount++;
}
food[i].isc=0;
}
}
}
//random enemy location
void rEXY(int i){
float ex=468,ey,j;
//for(j=0;j<5;j++){
//while((ey=1+(int)(440.00*rand()/(RAND_MAX+1.00)))!=food[j].y){
ey=1+(440.00*rand()/(RAND_MAX+1.00));
//}
//}
enemy[i].x=ex;
enemy[i].y=ey;
enemy[i].die=1;
}
//random draw enemy
void rEnemy(){
int i;
for(i=0;i<enemyCount;i++){
enemy[i].x=enemy[i].x-(i+1.2);
if(enemy[i].x<0){
enemy[i].x=468;
rEXY(i);
}
enemy[i].die=1;
}
for(i=0;i<enemyCount;i++){
if((i%2)==0){
DrawIMG(e1,enemy[i].x,enemy[i].y);
}else{
DrawIMG(e2,enemy[i].x,enemy[i].y);
}
}
}
//hitEnemy
void hitEnemy(){
int d1,d2,i,j;
float r=32.00,d;
for(j=0;j<enemyCount;j++){
for(i=0;i<len;i++){
d1=abs(s[i].x-enemy[j].x);
d2=abs(s[i].y-enemy[j].y);
d=sqrt(d1*d1+d2*d2);
if(d<r){
if(len>=1){
len=len-1;
}
enemy[j].die=0;
}
}
}
if(len==0){
//printf("game over\n");
gameOver();
}
}
//creat gold box XY
void rBXY(){
float ex,ey;
ex=1+(440.00*rand()/(RAND_MAX+1.00));
ey=1+(468.00*rand()/(RAND_MAX+1.00));
box.x=ex;
box.y=ey;
box.isExist=1;
}
//creat a gold box;
void rBox(){
DrawIMG(gBox,box.x,box.y);
}
//hit gold box
void hitBox(){
int d1,d2,i;
float r=32.00,d;
d1=abs(s[0].x-box.x);
d2=abs(s[0].y-box.y);
d=sqrt(d1*d1+d2*d2);
if(d<r){
score+=10;
len++;
box.isExist=0;
}
}
//game init fish station
void gameInit(){
srand((int)time(0));
int i;
len=1;
for(i=0;i<len;i++){
s[i].x=320-i*32;
s[i].y=240;
}
}
//start game and init Data;
void gameStart(){
imgx=0;
gameInit();
x=0,y=240;
enemyCount=1;
live=1;
score=0;
int i;
for(i=0;i<foodCount;i++){
rXY(i);
}
rFood(fimg);
for(i=0;i<enemyCount;i++){
rEXY(i);
}
rEnemy(e1);
state=1;
//len=4;
}
//load number image
void loadIMG(){
//load number picture>>>>>>>>>>>>
img0=load("./img/num/0.gif");
img1=load("./img/num/1.gif");
img2=load("./img/num/2.gif");
img3=load("./img/num/3.gif");
img4=load("./img/num/4.gif");
img5=load("./img/num/5.gif");
img6=load("./img/num/6.gif");
img7=load("./img/num/7.gif");
img8=load("./img/num/8.gif");
img9=load("./img/num/9.gif");
//load gateNumber picturn>>>>>>>>>>>>>
gate1=load("./img/1.gif");
gate2=load("./img/2.gif");
gate3=load("./img/3.gif");
gate4=load("./img/4.gif");
gate5=load("./img/5.gif");
gate6=load("./img/6.gif");
gate7=load("./img/7.gif");
gate8=load("./img/8.gif");
gate9=load("./img/9.gif");
//load food fish picture>>>>>>>>>>>>>>>>>>>>>>>
f1=load("./img/f1.gif");
f2=load("./img/f2.gif");
f3=load("./img/f3.gif");
f4=load("./img/f4.gif");
f5=load("./img/f5.gif");
//load background>>>>>>>>>>>>>>>>>>>>>>>>>
bg=load("./img/bg1.jpg");
//load mian fish>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
fish=load("./img/food.gif");
//load enemy picturn>>>>>>>>>>>>>>>>>>>>>>
e1=load("./img/em2.gif");
e2=load("./img/e2.gif");
//load gold box picture
gBox=load("./img/box.gif");
//load start main window picture>>>>>>>>>>>>>>>>>>>
sMain=load("./img/main.jpg");
//start button picture>>>>>>>>>
helpBut=load("./img/help.jpg");
startBut=load("./img/start.jpg");
helpMain=load("./img/helpmain.jpg");
returnBut1=load("./img/return1.jpg");
returnBut2=load("./img/return2.jpg");
//game over main window>>>>>>>>>>>>>>>>>>>>>>>>>>>
gOver=load("./img/gameover.jpg");
returnMain1=load("./img/returnMain1.jpg");
returnMain2=load("./img/returnMain2.jpg");
restart1=load("./img/restart1.jpg");
restart2=load("./img/restart2.jpg");
gfinish=load("./img/tongguan.jpg");
//game load cup window>>>>>>>>>>>>>>>>>>>>>>>
gcup=load("./img/test.gif");
gate=load("./img/gate.gif");
scorea=load("./img/score.gif");
}
//paint Score;
void initCup(int n)
{
int i,k;
k=n;
/*if(i==0)
{
cup[i].m=400;
cup[i].n=-48;
}
else if(i>0){*/
for(i=0;i<=k;i++)
{
cup[i].m=400+i*10;
cup[i].n=-48;
}
//}
}
void drawCup()
{
int i,m;
if(score>0&&score<49){
m=0;
initCup(m);
/*if(i==0)
{
DrawIMG(fish,cup[i].m,cup[i].n);
}*/
for(i=0;i<=m;i++)
{
DrawIMG(gcup,cup[i].m,cup[i].n);
//SDL_Flip(screen);
}
}
if(score%50==0){
m=score/50;
initCup(m);
for(i=0;i<=m;i++)
{
DrawIMG(gcup,cup[i].m,cup[i].n);
//SDL_Flip(screen);
}
}
}
void drawNum(int n,int xnum,int ynum){
int w=img0->w;
if((n<=9)&&(n>=0)){
DrawIMG(scorea,xnum,ynum);
DrawIMG(gate,xnum+5*w-20,ynum);
switch(n){
case 0:
DrawIMG(img0,xnum+2*w-20,ynum);
DrawIMG(img0,xnum+3*w-20,ynum);
break;
case 1:
DrawIMG(img0,xnum+2*w-20,ynum);
DrawIMG(img1,xnum+3*w-20,ynum);
break;
case 2:
DrawIMG(img0,xnum+2*w-20,ynum);
DrawIMG(img2,xnum+3*w-20,ynum);
break;
case 3:
DrawIMG(img0,xnum+2*w-20,ynum);
DrawIMG(img3,xnum+3*w-20,ynum);
break;
case 4:
DrawIMG(img0,xnum+2*w-20,ynum);
DrawIMG(img4,xnum+3*w-20,ynum);
break;
case 5:
DrawIMG(img0,xnum+2*w-20,ynum);
DrawIMG(img5,xnum+3*w-20,ynum);
break;
case 6:
DrawIMG(img0,xnum+2*w-20,ynum);
DrawIMG(img6,xnum+3*w-20,ynum);
break;
case 7:
DrawIMG(img0,xnum+2*w-20,ynum);
DrawIMG(img7,xnum+3*w-20,ynum);
break;
case 8:
DrawIMG(img0,xnum+2*w-20,ynum);
DrawIMG(img8,xnum+3*w-20,ynum);
break;
case 9:
DrawIMG(img0,xnum+2*w-20,ynum);
DrawIMG(img9,xnum+3*w-20,ynum);
break;
}
}else if((n>=10)&&(n<=99)){
int a=n/10;
int b=n%10;
switch(a){
case 0:
DrawIMG(img0,xnum+2*w-20,ynum);
break;
case 1:
DrawIMG(img1,xnum+2*w-20,ynum);
break;
case 2:
DrawIMG(img2,xnum+2*w-20,ynum);
break;
case 3:
DrawIMG(img3,xnum+2*w-20,ynum);
break;
case 4:
DrawIMG(img4,xnum+2*w-20,ynum);
break;
case 5:
DrawIMG(img5,xnum+2*w-20,ynum);
break;
case 6:
DrawIMG(img6,xnum+2*w-20,ynum);
break;
case 7:
DrawIMG(img7,xnum+2*w-20,ynum);
break;
case 8:
DrawIMG(img8,xnum+2*w-20,ynum);
break;
case 9:
DrawIMG(img9,xnum+2*w-20,ynum);
break;
}
switch(b){
case 0:
DrawIMG(img0,xnum+3*w-20,ynum);
break;
case 1:
DrawIMG(img1,xnum+3*w-20,ynum);
break;
case 2:
DrawIMG(img2,xnum+3*w-20,ynum);
break;
case 3:
DrawIMG(img3,xnum+3*w-20,ynum);
break;
case 4:
DrawIMG(img4,xnum+3*w-20,ynum);
break;
case 5:
DrawIMG(img5,xnum+3*w-20,ynum);
break;
case 6:
DrawIMG(img6,xnum+3*w-20,ynum);
break;
case 7:
DrawIMG(img7,xnum+3*w-20,ynum);
break;
case 8:
DrawIMG(img8,xnum+3*w-20,ynum);
break;
case 9:
DrawIMG(img9,xnum+3*w-20,ynum);
break;
}
}else if((n>=100)&&(n<=999)){
int a=n/100;
int b=(n/10)%10;
int c=n%10;
switch(a){
case 0:
DrawIMG(img0,xnum+2*w-20,ynum);
break;
case 1:
DrawIMG(img1,xnum+2*w-20,ynum);
break;
case 2:
DrawIMG(img2,xnum+2*w-20,ynum);
break;
case 3:
DrawIMG(img3,xnum+2*w-20,ynum);
break;
case 4:
DrawIMG(img4,xnum+2*w-20,ynum);
break;
case 5:
DrawIMG(img5,xnum+2*w-20,ynum);
break;
case 6:
DrawIMG(img6,xnum+2*w-20,ynum);
break;
case 7:
DrawIMG(img7,xnum+2*w-20,ynum);
break;
case 8:
DrawIMG(img8,xnum+2*w-20,ynum);
break;
case 9:
DrawIMG(img9,xnum+2*w-20,ynum);
break;
}
switch(b){
case 0:
DrawIMG(img0,xnum+3*w-20,ynum);
break;
case 1:
DrawIMG(img1,xnum+3*w-20,ynum);
break;
case 2:
DrawIMG(img2,xnum+3*w-20,ynum);
break;
case 3:
DrawIMG(img3,xnum+3*w-20,ynum);
break;
case 4:
DrawIMG(img4,xnum+3*w-20,ynum);
break;
case 5:
DrawIMG(img5,xnum+3*w-20,ynum);
break;
case 6:
DrawIMG(img6,xnum+3*w-20,ynum);
break;
case 7:
DrawIMG(img7,xnum+3*w-20,ynum);
break;
case 8:
DrawIMG(img8,xnum+3*w-20,ynum);
break;
case 9:
DrawIMG(img9,xnum+3*w-20,ynum);
break;
}
switch(c){
case 0:
DrawIMG(img0,xnum+4*w-20,ynum);
break;
case 1:
DrawIMG(img1,xnum+4*w-20,ynum);
break;
case 2:
DrawIMG(img2,xnum+4*w-20,ynum);
break;
case 3:
DrawIMG(img3,xnum+4*w-20,ynum);
break;
case 4:
DrawIMG(img4,xnum+4*w-20,ynum);
break;
case 5:
DrawIMG(img5,xnum+4*w-20,ynum);
break;
case 6:
DrawIMG(img6,xnum+4*w-20,ynum);
break;
case 7:
DrawIMG(img7,xnum+4*w-20,ynum);
break;
case 8:
DrawIMG(img8,xnum+4*w-20,ynum);
break;
case 9:
DrawIMG(img9,xnum+4*w-20,ynum);
break;
}
}
}
void gotoGate(int i){
switch(i){
case 0:
DrawIMG(gate1,150,150);
break;
case 1:
DrawIMG(gate2,150,150);
break;
case 2:
DrawIMG(gate3,150,150);
break;
case 3:
DrawIMG(gate4,150,150);
break;
case 4:
DrawIMG(gate5,150,150);
break;
case 5:
DrawIMG(gate6,150,150);
break;
case 6:
DrawIMG(gate7,150,150);
break;
case 7:
DrawIMG(gate8,150,150);
break;
case 8:
DrawIMG(gate9,150,150);
break;
}
}
//timer,use a function per 100ms;
void Timer(){
static Uint32 next_time=0;
int i;
Uint32 now;
now=SDL_GetTicks();
if(next_time<=now){
next_time+=100;
DrawBG(0,0);
game();
rFood();
hitFood();
rEnemy();
hitEnemy();
//rBXY();
if(score>=30){
if((score%30)==0){
rBox();
hitBox();
}
}
}
}
void gamePlay(){
Uint8 *keys;
gameInit();
gameStart();
int i=0;
while(1){
SDL_Event event;
while(SDL_PollEvent(&event)){
if(event.type==SDL_QUIT){
exit(0);
}
}
keys=SDL_GetKeyState(NULL);
if(keys[SDLK_UP]){
if(y<0){
y=0;
}else{
y-=speed;
}
}
if(keys[SDLK_DOWN]){
if(y>466){
y=466;
}else{
y+=speed;
}
}
if(keys[SDLK_LEFT]){
if(x<0){
x=0;
}else{
x-=speed;
}
}
if(keys[SDLK_RIGHT]){
if(x>466){
x=466;
}else{
x+=speed;
}
}
if(keys[SDLK_SPACE]){
speed=0;
s[0].x=s[0].x;
s[0].y=s[0].y;
}
if(keys[SDLK_a]){
speed=2;
}
if(state==1){
if(live==1){
int i;
for(i=0;i<foodCount;i++){
if(food[i].isc==0){
rXY(i);
}
}
for(i=0;i<enemyCount;i++){
if(enemy[i].die==0){
rEXY(i);
}
}
if(box.isExist==0){
rBXY();
printf("sdfsdf\n");
}
Timer();
drawNum(score,0,-50);
if(score==0){
gotoGate(0);
drawCup();
}else if(score==50){
drawCup();
gotoGate(1);
}else if(score==100){
gotoGate(2);
drawCup();
}else if(score==150){
gotoGate(3);
drawCup();
}else if(score==200){
gotoGate(4);
drawCup();
}else if(score==250){
gotoGate(5);
drawCup();
}else if(score==300){
gotoGate(6);
drawCup();
}else if(score==350){
gotoGate(7);
drawCup();
}else if(score==400){
gotoGate(8);
drawCup();
}else if(score>=450){
gameFinish();
}
}
//hitSelf(endImg);
}else if(state==0){
}else if(state==2){
}
SDL_Flip(screen);
}
}
void initHelp()
{
int ix=2,mouseX,mouseY,bX,bY;
while(1){
SDL_Event event;
while(SDL_PollEvent(&event)){
switch (event.type) {
case SDL_MOUSEMOTION:
printf("鼠标移动了%d,%d 到 (%d,%d)\n",
event.motion.xrel, event.motion.yrel,
event.motion.x, event.motion.y);
mouseX=event.motion.x;
mouseY=event.motion.y;
if(((mouseX>=350)&&(mouseX<=420))&&((mouseY>=450)&&(mouseY<=500))){
ix=1;
}else{
ix=2;
}
break;
case SDL_MOUSEBUTTONDOWN:
/*printf("鼠标 %d 点击在 (%d,%d)\n",
event.button.button, event.button.x, event.button.y);*/
if(event.button.button==1){
bX=event.button.x;
bY= event.button.y;
if(((bX>=350)&&(bX<=420))&&((bY>=450)&&(bY<=500))){
//ix=1;
initMain();
printf("hit\n");
}
}
break;
case SDL_QUIT:
exit(0);
}
}
DrawIMG(helpMain,0,0);
if(ix==1){
DrawIMG(returnBut1,350,400);
}else if(ix==2){
DrawIMG(returnBut2,350,400);
}
SDL_Flip(screen);
}
}
void initMain(){
Uint32 color;
int ix=1,mouseX,mouseY,bX,bY;
while(1){
SDL_Event event;
while(SDL_PollEvent(&event)){
switch (event.type) {
case SDL_MOUSEMOTION:
printf("鼠标移动了%d,%d 到 (%d,%d)\n",
event.motion.xrel, event.motion.yrel,
event.motion.x, event.motion.y);
mouseX=event.motion.x;
mouseY=event.motion.y;
if(((mouseX>=195)&&(mouseX<=315))&&((mouseY>=280)&&(mouseY<=330))){
ix=1;
}else if(((mouseX>=195)&&(mouseX<=315))&&((mouseY>=390)&&(mouseY<=440))){
ix=2;
}else{
ix=3;
}
break;
case SDL_MOUSEBUTTONDOWN:
/*printf("鼠标 %d 点击在 (%d,%d)\n",
event.button.button, event.button.x, event.button.y);*/
if(event.button.button==1){
bX=event.button.x;
bY= event.button.y;
if(((bX>=195)&&(bX<=315))&&((bY>=280)&&(bY<=330))){
//ix=1;
gamePlay();
}else if(((bX>=195)&&(bX<=315))&&((bY>=390)&&(bY<=440))){
//ix=2;
initHelp();
}else{
//ix=3;
}
}
break;
case SDL_QUIT:
exit(0);
}
}
//SDL_Flip(screen);
color=SDL_MapRGB(screen->format,0,0,0);
SDL_FillRect(screen,&screen->clip_rect,color);
DrawIMG(sMain,0,0);
if(ix==1){
DrawIMG(startBut,195,230);
}else if(ix==2){
DrawIMG(helpBut,195,337);
}else if(ix==3){
DrawIMG(sMain,0,0);
}
SDL_Flip(screen);
}
}
//main function start!
int main(int argc, char *argv[])
{
loadIMG();
init();
initMain();
printf("Hello, world\n");
return 0;
}
//需要的话到资源里下,图片之类的独有!
发表评论
-
win7技巧70个
2010-01-02 12:10 1103Windows 7比Vista要好用而且有很多新功能。 不过, ... -
我的右键菜单,我做主!
2010-01-02 12:52 1767在Windows系统中,只要对着桌面或是文件(夹)单击右键,就 ... -
win7 系统服务详解及设置参考
2010-01-02 12:56 3739为用户和服务身份验证维护此计算机和域控制器之间的安全通道。如 ... -
QQ目录下文件分析
2010-01-02 13:04 1899在QQ的安装目录下以你的QQ号码为名字的文件夹里有几个关键文件 ... -
清理系统垃圾文件
2010-08-01 20:05 803color fc@echo off@ ECHO.沙舟狼客工作室 ... -
BIOS中英文对照表
2010-08-01 20:20 759Time/System Time 时间/系统时间Date/Sy ... -
60个绝对经典的电脑技巧
2010-08-01 20:22 8411. 重装Windows XP不需再激活 如果你需要重装 ... -
恶意设置IE首页!
2010-08-01 20:40 961需要两个文件:run.vbs home.cmd run.vb ... -
autorun.inf完全操作手册
2010-08-01 20:51 1093一、autorun.inf是windows下操纵光盘行为的一个 ... -
常见的病毒及其特点
2010-08-01 21:00 1788常见计算机病毒的分类: ...
相关推荐
基础太差,现在重新学习...因为训练要求,要学习SDL库的使用,编写个SDL+俄罗斯方块小游戏。转眼一周多,小白终于写出来了。以下是一些体会。感觉还是好好学习,多撸代码才是正道。 借鉴了挺多前辈的经验【纯个人手码】
《基于SDL-2的C语言扫雷游戏》是一款利用C++11标准和SDL-2图形库开发的经典扫雷游戏。在这款游戏中,开发者没有设计复杂的用户界面(UI),而是专注于游戏的核心逻辑,即游戏界面的实现。接下来,我们将深入探讨这个...
【C语言基于SDL的贪吃蛇项目】是一个利用C语言编程实现的游戏,它基于Simple DirectMedia Layer (SDL) 库,这是一个跨平台的多媒体库,主要用于游戏开发。在这个项目中,开发者利用SDL2来处理图形、音频以及输入事件...
在本项目中,我们关注的是一个基于C语言设计的游戏,标题为“看谁守得住”,这显然是一款具有挑战性和趣味性的游戏。C语言是计算机编程领域中的基础语言,以其高效、灵活和对底层硬件的控制力强而著称,因此用C语言...
由于提供的文件内容并不包含实际的教学信息,以上知识点是基于“C语言游戏编程从入门到精通”这个标题来进行的一般性描述。如果你有具体的C语言游戏编程问题或需要学习某一部分的详细内容,请进一步提出,我会根据你...
通过这个教程,你可以从零开始学习SDL,逐步构建自己的游戏项目。每个实例都是一次新的学习机会,让你更深入地了解SDL库以及游戏开发的基本原理。在实践中,你会遇到更多高级特性,如图像加载、动画处理、音频播放等...
基于以上信息,我们可以深入探讨以下几个C语言游戏开发的知识点: 1. **C语言基础**:游戏开发的基础是理解C语言的基本语法,包括变量、数据类型、控制结构(如if语句、循环)、函数、指针等。 2. **游戏逻辑**:...
【标题】:“基于C语言编写的俄罗斯方块游戏的程序” 在编程领域,C语言以其高效、简洁和灵活的特点被广泛应用于系统编程、游戏开发等多个领域。本项目是利用C语言实现的一个经典小游戏——俄罗斯方块。俄罗斯方块...
在本次分享中,我们将深入探讨一个基于C语言的迷宫游戏,它不仅展示了C语言的基本语法和逻辑控制,还体现了编程思维与游戏设计的巧妙结合。 首先,C语言以其高效、简洁的特性,成为编写小游戏的理想选择。它允许...
c语言
本文将深入探讨一款基于C语言编写的坦克大战游戏的源代码,旨在帮助读者了解游戏开发的基本流程、C语言在游戏中的应用以及如何通过源代码学习编程技巧。 一、游戏开发基础 1. 游戏框架:坦克大战游戏通常采用循环...
本篇将深入探讨一个基于C语言编写的“吃豆游戏”的源码,帮助读者理解游戏的基本逻辑和C语言编程技巧。 “吃豆游戏”(Pacman)是一款经典的街机游戏,玩家控制一个小角色在迷宫中吃掉豆子,同时避开或消灭鬼魂。在...
1.基于C语言开发的魂斗罗程序,与原版魂斗罗完全一致,运行效果完美! 2.直接编译、链接生成可执行程序后,将可执行程序放至代码工程根目录下(注意:不是Debug或Release目录下,而是Debug或Release目录的上一层目录...
本项目是基于C语言进行的一次课程设计,旨在通过创建一个迷宫小游戏,让学习者深入理解和运用C语言的核心概念和技术。 迷宫小游戏的设计与实现涉及到多个关键知识点: 1. **数据结构**:游戏中的迷宫可以抽象为一...
【SDL拼图小游戏】是一款基于Linux操作系统的娱乐项目,它利用了C语言的强大功能和SDL(Simple DirectMedia Layer)库来实现。SDL是一个跨平台的开发库,专门用于处理图形、音频和输入设备,使得开发者可以轻松创建...
标题中的“sdl推箱子源码和资源.zip”表明这是一个基于SDL库开发的推箱子游戏的源代码和资源集合。SDL(Simple DirectMedia Layer)是一个跨平台的开发库,主要用于处理图形、音频和输入设备,常用于制作2D游戏。这...
在本文中,我们将深入探讨基于C语言开发的小游戏——坦克大战。C语言是一种基础且强大的编程语言,常用于系统编程、游戏开发、嵌入式系统等领域。坦克大战是一款经典的游戏,它融合了策略、动作和一定的射击元素,是...
SDL(Simple DirectMediaLayer)是一套开放源代码的跨平台多媒体开发库,使用C语言写成。SDL提供了数种控制图像、声音、输出入的函数,让开发者只要用相同或是相似的代码就可以开发出跨多个平台(Linux、Windows、...
【标题】:“俄罗斯方块”是一款经典的电子游戏,它的源码基于OpenGL库并采用C语言进行编程。OpenGL是一个跨语言、跨平台的图形程序接口,用于渲染2D、3D矢量图形。C语言作为底层编程语言,因其高效、灵活和广泛支持...
在C语言游戏开发的世界里,源码是学习和探索的核心。这个压缩包包含了7个经典游戏的源码和对应的分析报告,为初学者提供了一条深入理解C语言编程的路径。下面,我们将逐一探讨这些游戏及其相关的C语言知识点。 1. *...