#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include<SDL/SDL_image.h>
#include<time.h>
#include<string.h>
#include<SDL/SDL_thread.h>
#include<SDL/SDL_mutex.h>
#include <windows.h>
/*全局变量:全部坐标,图片加载指针*/
SDL_Surface *screen, *image;
SDL_Surface *back_image;
SDL_Rect back_dest;
int start=-1;//开始状态,1开始0初始-1暂停
int score;//分数
int team_long=0;//队伍长度初始化
char picName[15];//随机图片名字
int rapid;//速度
int d;//碰撞标记
Uint32 now;
struct team{
struct SDL_Rect dest;
struct SDL_Rect old_dest[13];
int dirction;
int old_dirction[13];
char picName[15];
};
struct team item[37],rand_item[5];
//方向坐标转换
SDL_Rect head_dest(int i){
SDL_Rect dest;
dest.w=dest.h=35;
dest.y=0;
if(i==0)i=3;
switch(i){
case 1:dest.x=0;break;
case 2:dest.x=35;break;
case 3:dest.x=70;break;
case 4:dest.x=105;break;
default:break;
}
return dest;
}
//加锁
void Slock(SDL_Surface *lock){
if(SDL_MUSTLOCK(lock)){
if(SDL_LockSurface(lock)<0){
return;
}
}
}
/*解锁*/
void Sulock(SDL_Surface *lock){
if(SDL_MUSTLOCK(lock)){
SDL_UnlockSurface(lock);
}
}
/*产生随机图片名称,存入picName[]中*/
void rand_picName(){
int i;
FILE *fp;
if((fp=fopen("pic.list","r"))==NULL){
printf("Can't open pic_list");}
i=(int)rand()%36+1;
fseek(fp,i*15,SEEK_SET);
fgets(picName,15,fp);
// rewind(fp,0,SEEK_SET);//指针归文件头位置
fclose(fp);
}
/*输出错误函数*/
void print_error(){
fprintf(stderr,"no is :%s\n",SDL_GetError());
atexit(SDL_Quit);
}
/*加载图片到内存中*/
SDL_Surface *load_image(char *filename){
SDL_Surface *loadedImage = NULL;
SDL_Surface *optimizedImage = NULL;
loadedImage = IMG_Load(filename);
if(loadedImage!=NULL){
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}
/*把内存中图片显示到屏幕中*/
void apply_surface(int x,int y,SDL_Surface *source, SDL_Surface *destination){
SDL_Rect offset;
offset.x=x;
offset.y=y;
Slock(destination);
SDL_BlitSurface(source,NULL,destination,&offset);
Sulock(screen);
SDL_FreeSurface(destination);
}
/*游戏区范围设置*/
SDL_Rect bump(SDL_Rect image_dest){
if(image_dest.x>500)
image_dest.x=500;
if(image_dest.x<0)
image_dest.x=0;
if(image_dest.y<0)
image_dest.y=0;
if(image_dest.y>380)
image_dest.y=380;
return image_dest;
}
/*产生随机坐标*/
SDL_Rect rand_dest(){
SDL_Rect dest;
int i;
while(1){
dest.x=(int)(rand()%500);
dest.y=(int)(rand()%380);
dest.w=35;
dest.h=35;
for(i=0;i<=team_long;i++){
if((dest.x>item[i].dest.x-35)&&(dest.x<(item[i].dest.x+35))&&(dest.y>item[i].dest.y-35)&&(dest.y<(item[i].dest.y+35)))
break;
if(i==team_long)return dest;
}
}
}
/*产生随机图片到屏幕中*/
void rand_image(){
int i,j;
SDL_Surface *rand_image;
SDL_Rect dest;
back_dest.w=35;back_dest.h=35;//70,70
back_dest.x=560; back_dest.y=260; //560,180
i=0;
while(i<5)
{
rand_picName();
strcpy(rand_item[i].picName,picName);
rand_item[i].dest=rand_dest();
if(i!=0)
{
for(j=0;j<i;j++)
{
if(strcmp(rand_item[i].picName,rand_item[j].picName)==0||(rand_item[j].dest.x<rand_item[i].dest.x+35&&rand_item[j].dest.x>rand_item[i].dest.x-35&&rand_item[j].dest.y<rand_item[i].dest.y+35&&rand_item[j].dest.y>rand_item[i].dest.y-35))
{
i=0;
continue;
}
}
}
i++;
}
for(i=0;i<5;i++){
// rand_item[i].dest=rand_dest();
// rand_picName();
// strcpy(rand_item[i].picName,picName);
rand_image=load_image(rand_item[i].picName);
dest=head_dest(3);
Slock(screen);
SDL_BlitSurface(rand_image,&dest,screen,&rand_item[i].dest);
if(i==2)
SDL_BlitSurface(rand_image,&dest,screen,&back_dest);
SDL_Flip(screen);
SDL_FreeSurface(rand_image);
Sulock(screen);
}
}
/*移动控制*/
void move(){
Uint8 *keys = SDL_GetKeyState(NULL);
int DOWN=1,UP=2,LEFT=3,RIGHT=4;
if(keys[SDLK_DOWN]&&item[0].dirction!=UP||item[0].dirction==DOWN){
item[0].dest.y+=3;
item[0].dirction=DOWN;
}
if(keys[SDLK_UP]&&item[0].dirction!=DOWN||item[0].dirction==UP){
item[0].dest.y-=3;
item[0].dirction=UP;
}
if(keys[SDLK_LEFT]&&item[0].dirction!=RIGHT||item[0].dirction==LEFT) {
item[0].dest.x-=3;
item[0].dirction=LEFT;
}
if(keys[SDLK_RIGHT]&&item[0].dirction!=LEFT||item[0].dirction==RIGHT){
item[0].dest.x+=3;
item[0].dirction=RIGHT;
}
SDL_GetKeyState(NULL);
}
/*碰撞检测*/
int pic_bump(){
int i,a=-1;
SDL_Rect dest=item[0].dest;
for(i=0;i<5;i++){
if((dest.x-30)<rand_item[i].dest.x&&(dest.x+30>rand_item[i].dest.x)){
if((dest.y-30)<rand_item[i].dest.y&&(dest.y+30>rand_item[i].dest.y)){
a=i;
break;
}
}
}
return a;
}
/*咬尾检测*/
int head_bump(int o){
int i;
SDL_Rect dest=item[0].dest;
if(o==0){
for(i=3;i<=team_long;i++){
if((dest.x>item[i].dest.x-23)&&(dest.x<(item[i].dest.x+23))&&(dest.y>item[i].dest.y-23)&&(dest.y<(item[i].dest.y+23))){
score-=20;
if(i==team_long){
item[0].dest=item[i].old_dest[12];
item[0].dirction=item[i].old_dirction[12];
o=300;
break;
}
else{
team_long=i;
break;
}
}
}
}
return o;
}
/*身体加长*/
void longer(){
int i;
SDL_Rect back_dest,dest;
back_dest.h=35;
back_dest.w=35;
Slock(screen);
for(i=team_long;i>=1;i--){
item[i].dest=item[i-1].old_dest[12];
item[i].dirction=item[i-1].old_dirction[12];
image=load_image(item[i].picName);
dest=head_dest(item[i].dirction);
SDL_BlitSurface(image,&dest,screen,&item[i].dest);
SDL_FreeSurface(image);
}
image=load_image("./pics/s0.gif");//加载火炬手
dest=head_dest(item[0].dirction);
SDL_BlitSurface(image,&dest,screen,&item[0].dest);
SDL_FreeSurface(image);
SDL_Flip(screen);
Sulock(screen);
}
/*去除残留影象*/
void clear(){
int i,j;
back_dest.w=35;
back_dest.h=35;
for(i=0;i<=team_long;i++){
back_dest=item[i].dest;
Slock(screen);
SDL_BlitSurface(back_image,&back_dest,screen,&back_dest);
Sulock(screen);
item[i].old_dest[0]=item[i].dest;
item[i].old_dirction[0]=item[i].dirction;
for(j=12;j>0;j--){
item[i].old_dest[j]=item[i].old_dest[j-1];
item[i].old_dirction[j]=item[i].old_dirction[j-1];
}
}
}
//加载放大要选择的图片
//int right_item(){
// SDL_Rect dest;
// SDL_Surface *rand_image;
// int i,j,h;
// rand_image=load_image(rand_item[2].picName);
// Slock(screen);
// dest.w=35;dest.h=35;dest.y=0;
// dest.x=rand_item[2].dest.x;
// back_dest.w=50;back_dest.h=50;//70,70
// back_dest.x=5; back_dest.y=5; //560,180
// SDL_BlitSurface(back_image,&back_dest,screen,&back_dest);
//// SDL_BlitSurface(rand_image,&rand_item[2].dest,screen,&back_dest);
// SDL_BlitSurface(rand_image,&dest,screen,&back_dest);
//// for(h=0;h<35;h++){
//// for(i=0;i<35;i++){
//// dest.x=70+i; dest.y=h;dest.w=1; dest.h=1;
//// back_dest.w=1;back_dest.h=1;
//// for(j=0;j<2;j++){
//// back_dest.x=560+i*2+j; back_dest.y=180+h*2;
//// SDL_BlitSurface(rand_image,&dest,screen,&back_dest);
//// back_dest.x=560+i*2+j; back_dest.y=180+h*2+1;
//// SDL_BlitSurface(rand_image,&dest,screen,&back_dest);
//// }
//// }
//// }
// SDL_Flip(screen);
// SDL_FreeSurface(rand_image);
// Sulock(screen);
// return 0;
//}
/*初始化*/
void initialize(){
SDL_Rect dest;
back_dest.x=0;
back_dest.y=0;
back_image=load_image("./pics/game.jpg");//加载背影(主画面)
SDL_BlitSurface(back_image,NULL,screen,NULL);
image=load_image("./pics/s0.gif");//加载火炬手
dest=head_dest(item[0].dirction);
score=0;
item[0].dirction=4;
item[0].dest.x=150;
item[0].dest.y=50;
item[0].dest.w=image->w;
item[0].dest.h=image->h;
SDL_Bl
奥运贪吃蛇游戏源代码
需积分: 9 90 浏览量
2008-09-05
10:53:44
上传
评论
收藏 2.35MB RAR 举报
cuihaitao
- 粉丝: 0
- 资源: 3
最新资源
- Python的pyqt5写的图书管理系统期末大作业源码带文档设计.zip
- pcff_interface_v1_5.off
- apk.tw_LineLite_v8a_v.2.17.1_sign.apk
- Elasticsearch实战:构建高效搜索系统的秘诀.zip
- HTML+CSS+JS网页设计:从入门到精通.zip
- 数据库课程设计:从理论到实践的全面指南.zip
- Python闭包:深入理解与应用场景解析.zip
- Win64OpenSSL-3-3-0.exe
- 课高分程设计-基于C++实现的民航飞行与地图简易管理系统-南京航空航天大学
- 航天器遥测数据故障检测系统python源码+文档说明+数据库(课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈