snake game

#include <iostream>

 
#include <vector>

 
#include <cstdlib>

 
#include <SDL/SDL.h>

 
#include <SDL/SDL_ttf.h>

 
#ifndef SNAKE_H

 
#define SNAKE_H

 
 

 
struct snakepart{

 
        int x,y;

 
        snakepart(int col,int row);

 
        snakepart();

 
};

 
 

 
class snakeclass{

 
        int points,del,width,height;

 
        char direction;

 
        bool get;

 
        snakepart food;

 
        std::vector<snakepart> snake;

 
        SDL_Surface* screen;

 
        TTF_Font* font;

 
 

 
        void drawRect(int x,int y,Uint32 color,int w=10,int h=10);

 
        void putfood();

 
        bool collision();

 
        void movesnake();

 
public:

 
        snakeclass();

 
        ~snakeclass();

 
        void start();

 
};

 
 

 
#endif

 

 
 
 
#include "snake.h"

/*
TODO list
-make a menu
-toplist
-colors
*/


void snakeclass::drawRect(int x,int y,Uint32 color,int w,int h)
{
	SDL_Rect tmp = {x,y,w,h};
	SDL_FillRect(screen,&tmp,color);
}


snakepart::snakepart(int col,int row)
{
	x=col;
	y=row;
}

snakepart::snakepart()
{
	x=0;
	y=0;
}

snakeclass::snakeclass()
{
	width=800;
	height=400;
	SDL_Init(SDL_INIT_EVERYTHING);
	TTF_Init();
	font=TTF_OpenFont("air.ttf",20);
	screen=SDL_SetVideoMode(width,height,32,SDL_SWSURFACE);

	for(int i=0;i<5;i++)
		snake.push_back(snakepart(40+i,10));
	points=0;
	del=110;
	get=false;
	direction='l';
	srand(time(0));
	putfood();
	//put the edges
	drawRect(0,0,SDL_MapRGB(screen->format,0xff,0x00,0x00),width,10);
	drawRect(0,0,SDL_MapRGB(screen->format,0xff,0x00,0x00),10,height-20);
	drawRect(0,height-30,SDL_MapRGB(screen->format,0xff,0x00,0x00),width,10);
	drawRect(width-10,0,SDL_MapRGB(screen->format,0xff,0x00,0x00),10,height-20);
	//draw the snake*/
	for(int i=0;i<snake.size();i++)
	{
			drawRect(snake[i].x*10,snake[i].y*10,SDL_MapRGB(screen->format,0x00,0xff,0x00));
	}
}
snakeclass::~snakeclass()
{
	TTF_CloseFont(font);
	TTF_Quit();
	SDL_Quit();
}

void snakeclass::putfood()
{
	while(1)
	{
		int tmpx=rand()%width/10+1;
		int tmpy=rand()%height/10+1;
		for(int i=0;i<snake.size();i++)
			if(snake[i].x==tmpx && snake[i].y==tmpy)
				continue;
		if(tmpx>=width/10-2 || tmpy>=height/10-3)
			continue;
		food.x=tmpx;
		food.y=tmpy;
		break;
	}
	drawRect(food.x*10,food.y*10,SDL_MapRGB(screen->format,0xff,0x00,0xff));
}

bool snakeclass::collision()
{
	if(snake[0].x==0 || snake[0].x==width/10-1 || snake[0].y==0 || snake[0].y==height/10-3)
		return 1;
	
	for(int i=2;i<snake.size();i++)
		if(snake[0].x==snake[i].x && snake[i].y==snake[0].y)
			return true;
	if(snake[0].x==food.x && snake[0].y==food.y)
	{
		get=true;
		putfood();
		points+=10;
		char c[5];
		sprintf(c,"%d",points);
		SDL_Surface* text;
		SDL_Color color = {255,255,255};
		text=TTF_RenderText_Solid(font,c,color);
		SDL_Rect tmp = {0,380};
		drawRect(0,380,SDL_MapRGB(screen->format,0x00,0x00,0x00),100,20);
		SDL_BlitSurface(text,NULL,screen,&tmp);
		SDL_FreeSurface(text);
		if((points%100)==0 && del>0)
			del-=10;
	}else
		get=false;
	return false;
}

void snakeclass::movesnake()
{
	SDL_Event event;
	while(SDL_PollEvent(&event))
	{
		if(event.type==SDL_KEYDOWN)
		{
			switch(event.key.keysym.sym)
			{
					case SDLK_LEFT:
						if(direction!='r')
							direction='l';
						break;
					case SDLK_UP:
						if(direction='d')
							direction='u';
						break;
					case SDLK_DOWN:
						if(direction!='u')
							direction='d';
						break;
					case SDLK_RIGHT:
						if(direction!='l')
							direction='r';
						break;
					case SDLK_ESCAPE:
						direction='q';
						break;
			}
		}else if(event.type==SDL_QUIT)
			direction='q';
	}
	if(!get)
	{
		drawRect(snake[snake.size()-1].x*10,snake[snake.size()-1].y*10,SDL_MapRGB(screen->format,0x00,0x00,0x00));
		snake.pop_back();
	}
	if(direction=='l')
		snake.insert(snake.begin(),snakepart(snake[0].x-1,snake[0].y));
	else if(direction=='r')
		snake.insert(snake.begin(),snakepart(snake[0].x+1,snake[0].y));
	else if(direction=='u')
		snake.insert(snake.begin(),snakepart(snake[0].x,snake[0].y-1));
	else if(direction=='d')
		snake.insert(snake.begin(),snakepart(snake[0].x,snake[0].y+1));
	drawRect(snake[0].x*10,snake[0].y*10,SDL_MapRGB(screen->format,0x00,0xff,0x00));
}


void snakeclass::start()
{
	while(1)
	{
		if(collision())
		{
			std::cout << "game over" << std::endl;
			break;
		}
		movesnake();
		if(direction=='q')
			break;
		SDL_Flip(screen);
		SDL_Delay(del);
	}
}

#include "snake.h"

 //the entry point

 int main(int argc,char* argv[])

 {

         snakeclass s;

         s.start();

 7.
        return 0;

 8.
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值