#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>
#include <iostream>
#include <time.h> //使用当前时间做种子;
using namespace std;
enum key{SDL_UP = 0,SDL_DOWN,SDL_LEFT,SDL_RIGHT};
enum number{num1 = 0,num2,num3,num4,num5,num6,num7,num8,num9};
key lip;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
int speed = 100;
int num = 0;
SDL_Surface* background = NULL;
SDL_Surface* message = NULL;
SDL_Surface* put = NULL;
SDL_Surface* bian = NULL;
SDL_Surface* screen = NULL;
SDL_Surface* number = NULL;
SDL_Surface* start = NULL;
SDL_Color textColor = {0,0,0};
SDL_Rect clip[6];
SDL_Event event;
TTF_Font* font = NULL;
TTF_Font* font2 = NULL;
void apply_surface(int x,int y,SDL_Surface *source,SDL_Surface *destination,SDL_Rect*clip = NULL)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source,clip,destination,&offset);
}
SDL_Surface* load_Image(std::string filename)
{
SDL_Surface* loadedImage;
SDL_Surface* optimizedImage;
loadedImage = IMG_Load(filename.c_str());
if(loadedImage != NULL)
{
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
if(optimizedImage != NULL)
{
SDL_SetColorKey(optimizedImage,SDL_RLEACCEL|SDL_SRCCOLORKEY,SDL_MapRGB(optimizedImage->format,255,255,255));
}
}
return optimizedImage;
}
bool init()
{
if(SDL_Init(SDL_INIT_AUDIO) == -1)
{
return false;
}
screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);
if(screen == NULL)
{
return false;
}
if(TTF_Init() == -1)
{
return false;
}
SDL_WM_SetCaption("keyget!",NULL);
return true;
}
bool load_file()
{
//Load the background image
background = load_Image( "background.bmp" );
message = load_Image("snake2.bmp");
bian = load_Image("fost.bmp");
start = load_Image("start.bmp");
//Open the font
font = TTF_OpenFont( "simfang.ttf", 40 );
font2 = TTF_OpenFont( "simfang.ttf",30);
//If there was a problem in loading the background
if( background == NULL )
{
return false;
}
//If there was an error in loading the font
if( font == NULL )
{
return false;
}
if(message == NULL)
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up()
{
//Free the surfaces
SDL_FreeSurface( background );
SDL_FreeSurface( message );
SDL_FreeSurface(put);
SDL_FreeSurface(number);
//Close the font that was used
TTF_CloseFont( font );
TTF_CloseFont(font2);
//Quit SDL_ttf
TTF_Quit();
//Quit SDL
SDL_Quit();
}
void bian_HUA()
{
for(int i = 0;i < 64;i++)
for(int j = 0;j < 48;j++)
{
apply_surface(i*10,0,bian,screen,&clip[1]);
apply_surface(i*10,470,bian,screen,&clip[1]);
apply_surface(0,j*10,bian,screen,&clip[1]);
apply_surface(490,j*10,bian,screen,&clip[1]);
}
}
void apply_number(int x,int y,int num)
{
int a[10],n = num;
for(int i = 0;i < 10;i++)
{
if(n != 0)
{
a[i] = n%10;
n/=10;
}
else a[i] = -1;
}
for(int i = 9;i>=0;i--)
{
if(a[i] != -1)
{
switch(a[i])
{
case 0:number = TTF_RenderText_Solid(font2,"0",textColor);break;
case 1:number = TTF_RenderText_Solid(font2,"1",textColor);break;
case 2:number = TTF_RenderText_Solid(font2,"2",textColor);break;
case 3:number = TTF_RenderText_Solid(font2,"3",textColor);break;
case 4:number = TTF_RenderText_Solid(font2,"4",textColor);break;
case 5:number = TTF_RenderText_Solid(font2,"5",textColor);break;
case 6:number = TTF_RenderText_Solid(font2,"6",textColor);break;
case 7:number = TTF_RenderText_Solid(font2,"7",textColor);break;
case 8:number = TTF_RenderText_Solid(font2,"8",textColor);break;
case 9:number = TTF_RenderText_Solid(font2,"9",textColor);break;
}
apply_surface(x,y,number,screen);
x+=10;
}
}
}
int* Put_Food(int *f)
{
srand((unsigned int) time(NULL));
f[0] = 10*(rand()%46+2);
f[1] = 10*(rand()%44+2);
return f;
}
class Node
{
private:
public:
Node(){}
~Node(){}
int x,y;
Node* next;
Node* previous;
Node(int a,int b):x(a),y(b){next = NULL;previous = NULL;}
friend class Snake;
Node* Get_Next(){return next;}
Node* Get_Previous(){return previous;}
void Give_Next(){ next->x = x;next -> y = y; }
void In_Next(Node* one){next = one;}
void In_Previous(Node* one){previous = one;}
void Printf_All(){std::cout<<"x = "<<x<<"\ny = "<<y<<std::endl;}
};
class Snake
{
private:
Node *head,*tail;
int number;
public:
Snake(){head = NULL,tail = NULL;number = -30;}
~Snake(){}
Node* Get_Head(){return head;}
Node* Get_Tail(){return tail;}
int Get_Number(){return number;}
void Add_One(Node* one);
void Move();
void Delete();
void Hua();
void Get_Food(int *f);
};
void Snake::Get_Food(int *f)
{
if(head->x == *(f)&&head->y == *(f+1))
{
switch(lip)
{
case SDL_UP:*(f+1) -= 10;break;
case SDL_DOWN:*(f+1) += 10;break;
case SDL_LEFT:*(f) -= 10;break;
case SDL_RIGHT:*(f) += 10;break;
}
Node *one = new Node(*(f),*(f+1));
Add_One(one);
Put_Food(f);
if(num == 5 && speed >=10)
{
speed -= 20;
num = 0;
}
num++;
}
}
void Snake::Hua()
{
Node *p = head;
apply_surface(p->x,p->y,message,screen);
p = p->Get_Next();
while(p != NULL)
{
apply_surface(p->x,p->y,message,screen);
p = p->Get_Next();
}
}
void Snake::Move()
{
Node *q = tail->Get_Previous();
//cout<<"DDDDDDD\nx = "<<q->x<<"\ny = "<<q->y<<"\nEEEEEEEE"<<endl;
do
{
q->Give_Next();
q = q->Get_Previous();
}while(q != NULL);
switch(lip)
{
case SDL_UP:head->y -= 10;break;
case SDL_DOWN:head->y += 10;break;
case SDL_LEFT:head->x -= 10;break;
case SDL_RIGHT:head->x += 10;break;
}
q = head->Get_Next();
while(q != NULL)
{
if(head ->y == q->y&&head->x == q->x)
{
head -> y = 1000;
head -> x = 1000;
}
q = q->Get_Next();
}
}
void Snake::Add_One(Node* one)
{
if(head == NULL)
{
head = one;
tail = one;
}
else
{
one->In_Next(Get_Head());
head->In_Previous(one);
head = one;
}
number += 10;
}
void Snake::Delete()
{
Node *p = head;
Node *q = head;
while(p -> Get_Next() != NULL)
{
p = p->Get_Next();
q = p;
delete q;
}
}
void SCORE_LEVEL(Snake* snake)
{
number = TTF_RenderText_Solid(font2,"SCORE:",textColor);
apply_surface(500,200,number,screen);
apply_number(580,200,snake->Get_Number());SDL_Delay(speed);
number = TTF_RenderText_Solid(font2,"SCORE:",textColor);
apply_surface(500,200,number,screen);
apply_number(580,200,snake->Get_Number());
number = TTF_RenderText_Solid(font2,"LEVEL:",textColor);
apply_surface(500,150,number,screen);
apply_number(580,150,((120 - speed)/20));
/*if(num == 5)
{
apply_surface(500,100,number,screen);
}*/
}
int Game_Start();
int Game_Play()
{
Snake snake;
Node *one = new Node(100,100);
Node *one2 = new Node(100,90);
Node *one3 = new Node(100,80);
snake.Add_One(one);
snake.Add_One(one2);
snake.Add_One(one3);
bool quit = false;
clip[0].x=25;
clip[0].y=25;
clip[0].w=10;
clip[0].h=10;
clip[1].x=0;
clip[1].y=0;
clip[1].w=10;
clip[1].h=10;
int Food[2] = {100,200};
if(init() == false)
{
return 1;
}
int head[2] = {100,100};
if(load_file() == false)
{
return 1;
}
SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0xFF,0xFF,0xFF));
//message = TTF_RenderText_Solid(font,"Up was pressed.",textColor);
apply_surface
自己写的贪吃蛇用了SDL图形化界面
5星 · 超过95%的资源 需积分: 13 50 浏览量
2013-07-13
14:19:37
上传
评论
收藏 3.1MB GZ 举报
一念寻风
- 粉丝: 4
- 资源: 14
最新资源
- 电力场景设备漏油检测数据集VOC+YOLO格式338张1类别.7z
- 基于yolov8+pyqt5实现精美界面支持图片视频和摄像检测源码.zip
- 用C语言为母亲节献上一份特别的祝福.zip
- LCD1602液晶显示屏的深入探索与实用指南.zip
- 基于Matlab人脸肤色定理的教师人数统计+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab霍夫曼变换的表盘读数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab火灾烟雾检测源码带GUI界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的恶劣天气交通标志识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的霍夫曼变换的表盘示数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的车道线识别系统 +源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈