Bootstrap

《贪吃蛇》游戏代码

这段时间我故意不小心的断更了,I'm very sorry.

那么我干什么了吗?当然给粉丝们带来了干货(-^-)

正题:

我写的贪吃蛇程序是用《小熊猫C++》写的,在其他环境下可能有一些问题,所以建议大家下一个《小熊猫C++》。

游戏简介:

开局一条蛇,吃一个彩色小球就会增加一节,吃一个黑色小球就会减少一半,速度与生成的彩黑小球的数量是根据蛇长度的,超过边界从另一边回来。

代码(全部免费):

#include <easyx/graphics.h>
#include <vector>
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
using namespace std;
#define WIDTH 800
#define HEIGHT 600
#define RADIUS 10

float velocityScale = 0.1;
float ration = velocityScale / (2 * RADIUS);

struct Point {
	float x, y;
	COLORREF color;
};

struct controlCircle {
	int x, y;
	int Radius;
	float angle;
	int ballX, ballY;
	int ballRadius;
};

vector<Point> snake;
controlCircle cCircle;
vector<Point> food;
vector<Point> block;
int bestScore;

Point generateRandPoint() {
	Point p;
	p.x = rand() % WIDTH;
	p.y = rand() % HEIGHT;
	return p;
}

bool isHeadTouchCircle(float x, float y, float r) {
	float xs = snake[0].x - x;
	float ys = snake[0].y - y;
	float rs = RADIUS + r;
	if (xs * xs + ys * ys < rs * rs) return true;
	else return false;
}

void startup() {
	srand(time(0));
	initgraph(WIDTH, HEIGHT);
	setbkcolor(RGB(230, 235, 235));
	cleardevice();
	cCircle.Radius = 40;
	cCircle.ballRadius = 20;
	cCircle.x = 2.5 * cCircle.Radius;
	cCircle.y = HEIGHT - 2.5 * cCircle.Radius;
	cCircle.angle = 0;
	for(int i=0;i<5;i++) {
		Point sec;
		float h = i * 3;
		sec.color = HSVtoRGB(h, 0.5, 0.9);
		snake.push_back(sec);
	}
	snake[0].x = WIDTH / 2;
	snake[0].y = HEIGHT / 2;
	for(int i=1;i<snake.size();i++) {
		snake[i].x = snake[i - 1].x - 2 * RADIUS * cos(cCircle.angle);
		snake[i].y = snake[i - 1].y + 2 * RADIUS * sin(cCircle.angle);
	}
	bestScore = snake.size();
	for(int i=0;i<30;i++) food.push_back(generateRandPoint());
	for(int i=0;i<5;i++) block.push_back(generateRandPoint());
	setbkmode(TRANSPARENT);
	BeginBatchDraw();
}

void show() {
	cleardevice();
	for(int i=0;i<food.size();i++) {
		setlinecolor(HSVtoRGB(i * 2, 0.6, 1));
		setfillcolor(HSVtoRGB(i * 2, 0.6, 1));
		fillcircle(food[i].x, food[i].y, RADIUS / 2);
	}
	
	for(int i=0;i<block.size();i++) {
		setlinecolor(RGB(230, 235, 235));
		setfillcolor(RGB(50, 50, 50));
		fillcircle(block[i].x, block[i].y, 2 * RADIUS / 3);
	}
	
	setlinecolor(HSVtoRGB(0, 0.5, 0.9));
	for(int i=0;i<snake.size();i++) {
		setfillcolor(snake[i].color);
		fillcircle(snake[i].x, snake[i].y, RADIUS);
	}
	float leftEyeAngle = cCircle.angle + 1;
	float rightEyeAngle = cCircle.angle - 1;
	float leftEyeX = snake[0].x + 0.8 * RADIUS * cos(leftEyeAngle);
	float leftEyeY = snake[0].y - 0.8 * RADIUS * sin(leftEyeAngle);
	float rightEyeX = snake[0].x + 0.8 * RADIUS * cos(rightEyeAngle);
	float rightEyeY = snake[0].y - 0.8 * RADIUS * sin(rightEyeAngle);
	setlinecolor(RGB(250, 250, 250));
	setfillcolor(RGB(250, 250, 250));
	fillcircle(leftEyeX, leftEyeY, 0.5 * RADIUS);
	fillcircle(rightEyeX, rightEyeY, 0.5 * RADIUS);
	setlinecolor(RGB(50, 50, 50));
	setfillcolor(RGB(50, 50, 50));
	fillcircle(leftEyeX, leftEyeY, 0.25 * RADIUS);
	fillcircle(rightEyeX, rightEyeY, 0.25 * RADIUS);
	setlinecolor(RGB(100, 100, 100));
	circle(cCircle.x, cCircle.y, cCircle.Radius);
	setlinecolor(RGB(200, 230, 200));
	setfillcolor(RGB(200, 230, 200));
	fillcircle(cCircle.ballX, cCircle.ballY, cCircle.ballRadius);
	settextstyle(25, 0, _T("微软雅黑"));
	settextcolor(BLUE);
	TCHAR s[20];
	_stprintf(s, _T("目前长度:%d"), snake.size());
	outtextxy(20, 20, s);
	_stprintf(s, _T("历史最大长度:%d"), bestScore);
	outtextxy(20, 50, s);
	FlushBatchDraw();
}

void update() {
	MOUSEMSG m;
	if(MouseHit()) {
		m = GetMouseMsg();
		if(m.uMsg == WM_MOUSEMOVE) {
			float xs = m.x - cCircle.x;
			float ys = m.y - cCircle.y;
			cCircle.angle = atan2(-ys, xs);
			cCircle.ballX = cCircle.x + cCircle.Radius * cos(cCircle.angle);
			cCircle.ballY = cCircle.y - cCircle.Radius * sin(cCircle.angle);
		}
	}
	snake[0].x += velocityScale * cos(cCircle.angle);
	snake[0].y += -velocityScale * sin(cCircle.angle);
	if(snake[0].x < 0) snake[0].x = WIDTH;
	if(snake[0].x > WIDTH) snake[0].x = 0;
	if(snake[0].y < 0) snake[0].y = HEIGHT;
	if(snake[0].y > HEIGHT) snake[0].y = 0;
	for(int i=1;i<snake.size();i++) {
		snake[i].x = (1 - ration) * snake[i].x + ration * snake[i - 1].x;
		snake[i].y = (1 - ration) * snake[i].y + ration * snake[i - 1].y;
	}
	for(int i=0;i<food.size();i++) {
		if(isHeadTouchCircle(food[i].x, food[i].y, RADIUS / 2)) {
			Point newSec;
			float h = snake.size() * 3;
			newSec.color = HSVtoRGB(h, 0.5, 0.9);
			snake.push_back(newSec);
			snake[snake.size() - 1].x = snake[snake.size() - 2].x;
			snake[snake.size() - 1].y = snake[snake.size() - 2].y;
			food[i].x = rand() % WIDTH;
			food[i].y = rand() % HEIGHT;
			if(rand() % 6 > 0) if(food.size() < 120) food.push_back(generateRandPoint());
			else if(block.size() < 20) block.push_back(generateRandPoint());
			if(snake.size() > bestScore) bestScore = snake.size();
		}
	}
	
	for(int i=0;i<block.size();i++) {
		if(isHeadTouchCircle(block[i].x, block[i].y, 2 * RADIUS / 3)) {
			int delNum = snake.size() / 2;
			if (delNum > 1) snake.erase(snake.end() - delNum, snake.end());
			block.erase(block.begin() + i);
		}
	}
	
	velocityScale = 0.05 + 0.01 * snake.size() + 0.001 * (food.size() + block.size());
	ration = velocityScale / (2 * RADIUS);
}

int main() {
	startup();
	while (1) {
		update();
		show();
	}
	return 0;
}

 

当然:

这有亿点难,所以也可以不生成黑色小球。

就成养老游戏了。

代码(全部免费):

#include <easyx/graphics.h>
#include <vector>
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
using namespace std;
#define WIDTH 800
#define HEIGHT 600
#define RADIUS 10

float velocityScale = 0.1;
float ration = velocityScale / (2 * RADIUS);

struct Point {
	float x, y;
	COLORREF color;
};

struct controlCircle {
	int x, y;
	int Radius;
	float angle;
	int ballX, ballY;
	int ballRadius;
};

vector<Point> snake;
controlCircle cCircle;
vector<Point> food;
vector<Point> block;
int bestScore;

Point generateRandPoint() {
	Point p;
	p.x = rand() % WIDTH;
	p.y = rand() % HEIGHT;
	return p;
}

bool isHeadTouchCircle(float x, float y, float r) {
	float xs = snake[0].x - x;
	float ys = snake[0].y - y;
	float rs = RADIUS + r;
	if (xs * xs + ys * ys < rs * rs) return true;
	else return false;
}

void startup() {
	srand(time(0));
	initgraph(WIDTH, HEIGHT);
	setbkcolor(RGB(230, 235, 235));
	cleardevice();
	cCircle.Radius = 40;
	cCircle.ballRadius = 20;
	cCircle.x = 2.5 * cCircle.Radius;
	cCircle.y = HEIGHT - 2.5 * cCircle.Radius;
	cCircle.angle = 0;
	for(int i=0;i<5;i++) {
		Point sec;
		float h = i * 3;
		sec.color = HSVtoRGB(h, 0.5, 0.9);
		snake.push_back(sec);
	}
	snake[0].x = WIDTH / 2;
	snake[0].y = HEIGHT / 2;
	for(int i=1;i<snake.size();i++) {
		snake[i].x = snake[i - 1].x - 2 * RADIUS * cos(cCircle.angle);
		snake[i].y = snake[i - 1].y + 2 * RADIUS * sin(cCircle.angle);
	}
	bestScore = snake.size();
	for(int i=0;i<30;i++) food.push_back(generateRandPoint());
	for(int i=0;i<5;i++) block.push_back(generateRandPoint());
	setbkmode(TRANSPARENT);
	BeginBatchDraw();
}

void show() {
	cleardevice();
	for(int i=0;i<food.size();i++) {
		setlinecolor(HSVtoRGB(i * 2, 0.6, 1));
		setfillcolor(HSVtoRGB(i * 2, 0.6, 1));
		fillcircle(food[i].x, food[i].y, RADIUS / 2);
	}
	/*
	for(int i=0;i<block.size();i++) {
		setlinecolor(RGB(230, 235, 235));
		setfillcolor(RGB(50, 50, 50));
		fillcircle(block[i].x, block[i].y, 2 * RADIUS / 3);
	}
	*/
	setlinecolor(HSVtoRGB(0, 0.5, 0.9));
	for(int i=0;i<snake.size();i++) {
		setfillcolor(snake[i].color);
		fillcircle(snake[i].x, snake[i].y, RADIUS);
	}
	float leftEyeAngle = cCircle.angle + 1;
	float rightEyeAngle = cCircle.angle - 1;
	float leftEyeX = snake[0].x + 0.8 * RADIUS * cos(leftEyeAngle);
	float leftEyeY = snake[0].y - 0.8 * RADIUS * sin(leftEyeAngle);
	float rightEyeX = snake[0].x + 0.8 * RADIUS * cos(rightEyeAngle);
	float rightEyeY = snake[0].y - 0.8 * RADIUS * sin(rightEyeAngle);
	setlinecolor(RGB(250, 250, 250));
	setfillcolor(RGB(250, 250, 250));
	fillcircle(leftEyeX, leftEyeY, 0.5 * RADIUS);
	fillcircle(rightEyeX, rightEyeY, 0.5 * RADIUS);
	setlinecolor(RGB(50, 50, 50));
	setfillcolor(RGB(50, 50, 50));
	fillcircle(leftEyeX, leftEyeY, 0.25 * RADIUS);
	fillcircle(rightEyeX, rightEyeY, 0.25 * RADIUS);
	setlinecolor(RGB(100, 100, 100));
	circle(cCircle.x, cCircle.y, cCircle.Radius);
	setlinecolor(RGB(200, 230, 200));
	setfillcolor(RGB(200, 230, 200));
	fillcircle(cCircle.ballX, cCircle.ballY, cCircle.ballRadius);
	settextstyle(25, 0, _T("微软雅黑"));
	settextcolor(BLUE);
	TCHAR s[20];
	_stprintf(s, _T("目前长度:%d"), snake.size());
	outtextxy(20, 20, s);
	_stprintf(s, _T("历史最大长度:%d"), bestScore);
	outtextxy(20, 50, s);
	FlushBatchDraw();
}

void update() {
	MOUSEMSG m;
	if(MouseHit()) {
		m = GetMouseMsg();
		if(m.uMsg == WM_MOUSEMOVE) {
			float xs = m.x - cCircle.x;
			float ys = m.y - cCircle.y;
			cCircle.angle = atan2(-ys, xs);
			cCircle.ballX = cCircle.x + cCircle.Radius * cos(cCircle.angle);
			cCircle.ballY = cCircle.y - cCircle.Radius * sin(cCircle.angle);
		}
	}
	snake[0].x += velocityScale * cos(cCircle.angle);
	snake[0].y += -velocityScale * sin(cCircle.angle);
	if(snake[0].x < 0) snake[0].x = WIDTH;
	if(snake[0].x > WIDTH) snake[0].x = 0;
	if(snake[0].y < 0) snake[0].y = HEIGHT;
	if(snake[0].y > HEIGHT) snake[0].y = 0;
	for(int i=1;i<snake.size();i++) {
		snake[i].x = (1 - ration) * snake[i].x + ration * snake[i - 1].x;
		snake[i].y = (1 - ration) * snake[i].y + ration * snake[i - 1].y;
	}
	for(int i=0;i<food.size();i++) {
		if(isHeadTouchCircle(food[i].x, food[i].y, RADIUS / 2)) {
			Point newSec;
			float h = snake.size() * 3;
			newSec.color = HSVtoRGB(h, 0.5, 0.9);
			snake.push_back(newSec);
			snake[snake.size() - 1].x = snake[snake.size() - 2].x;
			snake[snake.size() - 1].y = snake[snake.size() - 2].y;
			food[i].x = rand() % WIDTH;
			food[i].y = rand() % HEIGHT;
			if(rand() % 6 > 0) if(food.size() < 120) food.push_back(generateRandPoint());
			else if(block.size() < 20) block.push_back(generateRandPoint());
			if(snake.size() > bestScore) bestScore = snake.size();
		}
	}
	/*
	for(int i=0;i<block.size();i++) {
		if(isHeadTouchCircle(block[i].x, block[i].y, 2 * RADIUS / 3)) {
			int delNum = snake.size() / 2;
			if (delNum > 1) snake.erase(snake.end() - delNum, snake.end());
			block.erase(block.begin() + i);
		}
	}
	*/
	velocityScale = 0.05 + 0.01 * snake.size() + 0.001 * (food.size() + block.size());
	ration = velocityScale / (2 * RADIUS);
}

int main() {
	startup();
	while (1) {
		update();
		show();
	}
	return 0;
}

 

这样良心的博主不多了,就给个赞吧! 

 

;