练手小项目

1.使用Eaxys实现的小游戏,参考于爱消除

#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <string>
#include <iostream>
#pragma comment(lib,"Winmm.lib")//加载音乐的库

//定义7种颜色的小球
COLORREF colors[7] = { RGB(255,0,0),RGB(0,0,255),RGB(255,255,255),
					RGB(0,255,0),RGB(255,0,255),RGB(255,255,0),RGB(0,255,255) };

#define KEY_DOWN(vk_c)(GetAsyncKeyState(vk_c)&0x8000)

typedef struct _Pos {
	int x;
	int y;
}Pos;

Pos cur;
Pos ballArr[180];
int index;

void dworePos(Pos pos, COLORREF c);
void dworeTime(int sec);
void dworeSore(int sore);
void getsamcolorballs(Pos cur, COLORREF c);
bool isVal(Pos pos, COLORREF c);
void ballDown();
void turm();
void overGame();
void dworeLevel(int level);
void ruleGame();

//初始化游戏
void initGame() {
	initgraph(900, 700);
}

//游戏开始界面
void beginGame() {
	//音乐响起来
	//mciSendString(_T("open background.wav alias a1 wait"), NULL, 0, NULL);
	//音乐是需要自己本地的音乐放到与程序同一个文件执行这行代码才会有效
	mciSendString(_T("play 极乐净土.mp3"), 0, 0, 0);
	//绘制边框
	setlinestyle(PS_SOLID, 10);//设置边框线条
	setlinecolor(RGB(100, 100, 100));//边框颜色设置
	rectangle(255, 45, 745, 655);//边框位置大小设置
	//绘制小球
	setlinestyle(PS_SOLID);
	srand((unsigned)time(NULL));
	for (int x = 280; x < 740; x += 40) {
		for (int y = 70; y < 650; y += 40) {
			//随机生成7种颜色的小球
			COLORREF c1 = colors[rand() % 7];
			setlinecolor(c1);
			setfillcolor(c1);
			fillcircle(x, y, 18);
		}
	}
	//绘制光标
	cur.x = 480;
	cur.y = 390;
	dworePos(cur, RGB(0, 250, 0));
	//绘制时间
	dworeTime(30);
	//绘制分数
	dworeSore(0);
	//绘制关卡
	dworeLevel(0);
	//绘制游戏规则
	ruleGame();
}

//游戏控制
void playGame() {

	int sce = 0;
	int level = 0;

	for (int i = 200; i > -1; i--) {
		if(i%10==0){
			dworeTime(i/10);
		}
		if (KEY_DOWN(VK_RETURN) || KEY_DOWN(VK_SPACE)) {
			i = 200;
		}
		if (KEY_DOWN(VK_UP) && cur.y > 70) {//上
			dworePos(cur, RGB(0, 0, 0));
			cur.y -= 40;
			dworePos(cur, RGB(255, 255, 255));
		}
		else if (KEY_DOWN(VK_DOWN) && cur.y < 610) {//下
			dworePos(cur, RGB(0, 0, 0));
			cur.y += 40;
			dworePos(cur, RGB(255, 255, 255));
		}
		else if (KEY_DOWN(VK_LEFT) && cur.x > 280) {//左
			dworePos(cur, RGB(0, 0, 0));
			cur.x -= 40;
			dworePos(cur, RGB(255, 255, 255));
		}
		else if (KEY_DOWN(VK_RIGHT) && cur.x < 700) {//右
			dworePos(cur, RGB(0, 0, 0));
			cur.x += 40;
			dworePos(cur, RGB(255, 255, 255));
		}
		else if (KEY_DOWN(VK_RETURN)) {//回车键退出
			break;
		}
		else if (KEY_DOWN(VK_SPACE)) {
			//将光标所在位置周围的同色小球存进数组,并记录个数
			getsamcolorballs(cur, getpixel(cur.x, cur.y));
			//将数组中的小球黑置一段时间
			if (index > 2) {
				for (int i = 0; i < index; i++) {
					setlinecolor(RGB(0, 0, 0));
					setfillcolor(RGB(0, 0, 0));
					fillcircle(ballArr[i].x, ballArr[i].y, 18);
				}

				//小球掉落
				Sleep(500);
				ballDown();
				sce += index;
				dworeSore(sce);
				if (sce >= 200) {
					level += 1;
					dworeLevel(level);
					sce = 0;
					system("pause");
					continue;
				}
			}
			index = 0;
		}

		Sleep(110);
	}
	cleardevice();//清除屏幕
	settextcolor(RGB(0, 200, 0));
	settextstyle(100, 0, "楷体");
	outtextxy(250, 250, "游戏结束!");
	dworeSore(sce);
}

//游戏结束
void overGame() {
	system("pause");
	closegraph();
}

//重绘光标
void dworePos(Pos pos, COLORREF c) {
	setlinecolor(c);
	rectangle(pos.x - 20, pos.y - 20, pos.x + 20, pos.y + 20);
}

//绘制时间
void dworeTime(int sec) {
	char str[30];
	settextcolor(RGB(0, 200, 10));
	settextstyle(25, 0, "楷体");
	sprintf_s(str, "剩余时间: %2d秒", sec);
	outtextxy(20, 50, str);
}

//绘制分数
void dworeSore(int sore) {
	char str[30];
	settextcolor(RGB(0, 200, 10));
	settextstyle(25, 0, "楷体");
	sprintf_s(str, "分数: %2d分", sore);
	outtextxy(20, 100, str);
}

//关卡设置
void dworeLevel(int level) {
	char str[30];
	settextcolor(RGB(0, 200, 10));
	settextstyle(25, 0, "楷体");
	sprintf_s(str, "关卡: 第%d关", level);
	outtextxy(20, 150, str);
}

//游戏规则
void ruleGame() {
	settextcolor(RGB(0, 200, 10));
	settextstyle(25, 0, "楷体");
	outtextxy(20, 250, "游戏规则如下:");
	outtextxy(40, 300, "上键: ↑");
	outtextxy(40, 350, "下键: ↓");
	outtextxy(40, 400, "左键: ←");
	outtextxy(40, 450, "右键: →");
	outtextxy(40, 500, "回车键: 退出");
	outtextxy(40, 550, "空格键: ");
	outtextxy(40, 600, "消除同色小球");
}

//找出同色小球
void getsamcolorballs(Pos cur, COLORREF c) {
	ballArr[index].x = cur.x;
	ballArr[index].y = cur.y;
	index++;

	Pos tmp;
	for (int i = 0; i < 4; i++) {
		switch (i)
		{
		case 0://上
			tmp.x = cur.x;
			tmp.y = cur.y - 40;
			break;
		case 1://下
			tmp.x = cur.x;
			tmp.y = cur.y + 40;
			break;
		case 2://左
			tmp.x = cur.x - 40;
			tmp.y = cur.y;
			break;
		case 3://右
			tmp.x = cur.x + 40;
			tmp.y = cur.y;
			break;
		default:
			break;
		}

		if (isVal(tmp, c)) {
			getsamcolorballs(tmp, c);
		}
	}
}

//判断对比的小球颜色结果
bool isVal(Pos pos, COLORREF c) {
	if (getpixel(pos.x, pos.y) != c) {
		return false;
	}
	else {

		for (int i = 0; i < index; i++) {
			if (pos.x == ballArr[i].x && pos.y == ballArr[i].y) {
				return false;
			}
		}
		return true;
	}
}

//光标在下面时处理小球消失
void turm() {
	Pos tmp;

	for (int i = 0; i < index - 1; i++) {
		for (int j = 0; j < index - 1 - i; j++) {
			if (ballArr[j].x > ballArr[j + 1].x) {
				tmp = ballArr[j];
				ballArr[j] = ballArr[j + 1];
				ballArr[j + 1] = tmp;
			}

			if (ballArr[j].y > ballArr[j + 1].y) {
				tmp = ballArr[j];
				ballArr[j] = ballArr[j + 1];
				ballArr[j + 1] = tmp;
			}
		}
	}
}

//小球掉落
void ballDown() {

	turm();

	for (int i = 0; i < index; i++) {
		for (int j = ballArr[i].y; j > 70; j -= 40) {
			COLORREF c = getpixel(ballArr[i].x, j - 40);
			setlinecolor(c);
			setfillcolor(c);
			fillcircle(ballArr[i].x, j, 18);
		}

		COLORREF c1 = colors[rand() % 7];
		setlinecolor(c1);
		setfillcolor(c1);
		fillcircle(ballArr[i].x, 70, 18);
	}
}

int main(void) {

	initGame();

	beginGame();

	playGame();

	overGame();

	return 0;
}

2.使用链表优化的一个控制台的星空

#ifndef _LINKLIST_//包含一次头文件
#define _LINKLIST_
// !_MAIN_H_



#define MAX_STAR 100 
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480 
#define MAX_STEP 5
#define MAX_RADIUS 3
#define BOTTOM_MARGIN 100
//星星状态
enum STATUS {
    STOP = 0,
    UP,
    DOWN,
    LEFT,
    RIGHT,
    RANDOM,
    ALL_STATUS
};

struct STAR {
    int x; //星星的 x 坐标 
    int y; //星星的 y 坐标 
    enum STATUS stat; //状态 
    unsigned int radius; //星星的半径 
    int step; //每次跳跃的间隔
    int color; //星星的颜色 
};

typedef struct {
    struct STAR* elems;
    int length;
    int size;
}Star;

typedef struct linkList {
    struct STAR elmes;
    struct linkList* next;
}linkLink, linkNode;

//顺序表的初始化
bool initList(Star& s);
//访问顺序表
void listPrint(Star& s);
//顺序表的删除
bool listDelete(Star& s, int i);
//顺序表的销毁
void destroyList(Star& s);
//顺序表的尾部插入元素
bool listAppend(Star& s, struct STAR n);

//单链表初始化
bool initList(linkLink*& L);
//单链表表的尾部插入元素
bool listAppend(linkLink*& L, linkNode* node);
//单链表表的删除
bool listDelete(linkLink*& L, int i);
//单链表的销毁
void destroyList(linkLink*& L);
#endif _MAIN_H_
#include <iostream>
#include "linklist.h"

using namespace std;


//顺序表的初始化
bool initList(Star& s) {
	s.elems = new struct STAR[MAX_STAR];
	if (!s.elems) return false;//检查分配堆空间是否成功

	s.length = 0;
	s.size = MAX_STAR;

	return true;
}
//访问顺序表
void listPrint(Star& s) {
	cout << "顺序表的总空间大小: (" << s.size << ") 已用的元素个数: " << s.length << endl;
	for (int i = 0; i <= s.length - 1; i++) {
		cout << "第" << i << "颗星星: x=" << s.elems[i].x << " y=" << s.elems[i].y
			<< " radius=" << s.elems[i].radius << " step=" << s.elems[i].step << endl;
	}
	cout << endl;
}

//尾部插入一个元素
bool listAppend(Star& s, struct STAR n) {
	//检查是否越界
	if (s.length == s.size) return false;

	s.elems[s.length] = n;
	s.length++;
	return true;
}

//顺序表的任意指定位删除
bool listDelete(Star& s, int i) {
	if (i < 0 || i >= s.length)return false;

	if (i == s.length - 1) {
		s.length--;
		return true;
	}
	for (int j = i; j < s.length - 1; j++) {
		s.elems[j] = s.elems[j + 1];
	}
	s.length--;
	return true;

}

//顺序表的删除
void destroyList(Star& s) {
	if (s.elems) {
		delete[] s.elems;
		s.elems = NULL;
		s.length = 0;
		s.size = 0;
	}
}

//单链表初始化
bool initList(linkLink*& L) {
	L = new linkLink;
	if (!L) {
		cout << "单链表初始化失败!..." << endl;
		return false;
	}
	L->next = NULL;
	return true;
}
//单链表表的尾部插入元素
bool listAppend(linkLink*& L, linkNode* node) {
	if (!L || !node) {
		cout << "链表为空" << endl;
		return false;
	}
	linkLink* p = NULL;
	p = L;
	while (p->next)p = p->next;
	p->next = node;
	node->next = NULL;
	return true;
}
//单链表的元素删除
bool listDelete(linkLink*& L, int i) {
	if (!L || !L->next)return false;
	linkLink* p = NULL, * q = NULL;
	int index = 0;

	p = L;
	while (p->next && index < i - 1) {
		p = p->next;
		index++;
	}
	if (!p->next || index > i - 1)return false;

	q = p->next;
	p->next = q->next;

	delete q;
	return true;
}
//单链表的销毁
void destroyList(linkLink*& L) {
	if (!L)return;

	linkLink* p = NULL;
	p = L;

	while (p) {
		L = L->next;
		delete p;
		p = L;
	}
}


#include <iostream>
#include <graphics.h> 
#include <conio.h> //控制台输入输出
#include <stdio.h>
#include <string>
#include "linklist.h"

using namespace std;

void initStar(linkLink*& L) {
    int rgb = 0;
    //if (i<0 || i>MAX_STAR) {
    //   return; 
    //}

    //rand() 得到随机数范围 0 - 32767 RAND_MAX
    L->elmes.x = rand() % SCREEN_WIDTH; // x 范围 0 -639 
    L->elmes.y = rand() % (SCREEN_HEIGHT - BOTTOM_MARGIN);// y 范围 0 - 379 
    L->elmes.stat = UP;//(enum STATUS)(rand() % ALL_STATUS);
    L->elmes.radius = 1 + rand() % MAX_RADIUS; //半径控制 1 - 3 
    L->elmes.step = rand() % MAX_STEP + 1; //步长 1 - 5
    rgb = 255 * L->elmes.step / MAX_STEP; // 0 - 255 
    L->elmes.color = RGB(rgb, rgb, rgb);
}

//void MoveStar(Star& s, int i) {
void MoveStar(linkLink*& L) {
    if (!L || !L->next)return;
    linkLink* p = NULL, * q = NULL;

    int index = 1;

    //擦除原来的星星 
    p = L->next;
    while (p) {

        //if (p->elmes.stat == STOP) return;
        setfillcolor(BLACK);
        solidcircle(p->elmes.x, p->elmes.y, p->elmes.radius);
        if (p->elmes.stat == DOWN) {
            p->elmes.y = p->elmes.y + p->elmes.step;
            if (p->elmes.y > SCREEN_HEIGHT) {
                listDelete(L, index);
                p = L->next;
                index = 1;
                //goto LOVE;
            }
            //if(p->elmes.y>SCREEN_HEIGHT) p->elmes.y = 0;
        }
        else if (p->elmes.stat == UP) {
            p->elmes.y -= p->elmes.step;
            if (p->elmes.y < 0) {
                listDelete(L, index);
                p = L->next;
                index = 1;
                // goto LOVE;
            }
            //if(p->elmes.y<0) p->elmes.y = SCREEN_HEIGHT;
        }
        else if (p->elmes.stat == LEFT) {
            p->elmes.x -= p->elmes.step;
            if (p->elmes.x < 0) {
                listDelete(L, index);
                p = L->next;
                index = 1;
                //goto LOVE;
            }

            //if (p->elmes.x < 0) p->elmes.x = SCREEN_WIDTH;
        }
        else if (p->elmes.stat == RIGHT) {
            p->elmes.x += p->elmes.step;
            if (p->elmes.x > SCREEN_WIDTH) {
                listDelete(L, index);
                p = L->next;
                index = 1;
                //goto LOVE;
            }
            //if(p->elmes.x>SCREEN_WIDTH) p->elmes.x = 0;
        }

        if (p) {
            setfillcolor(p->elmes.color);
            solidcircle(p->elmes.x, p->elmes.y, p->elmes.radius);

            p = p->next;
            index++;
        }
    }

}

int main() {
    bool quit = false;
    //Star starList;
    //struct STAR _star;

    //初始化保存星星状态的顺序表
    //initList(starList);

    linkLink* L = NULL;
    linkLink* p = NULL, * q = NULL;

    //初始化保存星星状态的单链表
    initList(L);

    initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);

    for (int i = 0; i < MAX_STAR; i++) {
        linkNode* node = NULL;
        node = new linkNode;
        if (!node)return -2;
        initStar(node);

        if (listAppend(L, node)) {
            cout << "Append Success..." << endl;
        }
        else {
            cout << "Append Fallure..." << endl;
        }
    }

    p = L->next;
    for (int i = 0; i < MAX_STAR; i++) {

        setfillcolor(p->elmes.color);
        solidcircle(p->elmes.x, p->elmes.y,
            p->elmes.radius);
        p = p->next;
        // setfillcolor(star[i].color);
    }

    while (L->next) {

        MoveStar(L);
        Sleep(50);
    }

    destroyList(L);

    system("pause");
    closegraph();

    //cout << star[1].radius << endl;
    return 0;
}

流星雨项目(待优化)

#include <iostream>
#include <graphics.h>
#include <time.h>
#include <conio.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")

using namespace std;

#define MAXSTAR 1000 //星星数量
#define MAXMETEOR 400//流星数量
#define LENGTH 1000 //窗口长度
#define WIDE 600    //窗口宽度

struct Star {
	int x, y;
	int step;
	int color;
}star[MAXSTAR];

struct Meteor {
	int x, y;
	int step;
	int style;
}meteor[MAXMETEOR];

IMAGE img1, img2;

void initStar(int i);
void drawMeteor(int i);
void initMeteor(int i);
void initData(int i);
void moveStar(int i);
void moveMeteor(int i);

int main(void) {

	mciSendString("open 流星雨.mp3 alias A", 0, 0, 0);
	mciSendString("play A repeat", 0, 0, 0);
	initgraph(LENGTH, WIDE);
	int i = 0;
	initData(i);
	while (1)
	{
		clearcliprgn();
		for (int i = 0; i < MAXSTAR; i++) {
			moveStar(i);
		}
		drawMeteor(i);
		moveMeteor(i);
		Sleep(100);
	}

	system("pause");
	closegraph();
	return 0;
}

void initData(int i) {

	//加载资源,就是图片
	//loadimage(&img1, "1.jpg", 40, 40);
	loadimage(&img2, "2.jpg", 40, 40);

	srand((unsigned int)time(NULL));
	for (int i = 0; i < MAXSTAR; i++) {
		initStar(i);
	}
	for (int i = 0; i < MAXMETEOR; i++) {
		initMeteor(i);
	}
}
//初始化星星
void initStar(int i) {
		star[i].x = rand() % 1000;
		star[i].y = rand() % 600;
		star[i].step = rand() % 6;
		star[i].color = RGB(star[i].step * 51, star[i].step * 51, star[i].step * 51);
}
//画星星
void moveStar(int i) {
		putpixel(star[i].x, star[i].y, star[i].color);
		star[i].x += star[i].step;//移动的过程

		if (star[i].x > LENGTH) {
			putpixel(star[i].x, star[i].y, star[i].color);
		}
}
//流星出现
void initMeteor(int i) {

		meteor[i].x = rand() % 2000 - 1000;
		meteor[i].y = -100;
		meteor[i].step = rand() % 30 + 3;
		meteor[i].style = rand() % 2 + 1;
}

//加载流星
void drawMeteor(int i) {
	for (i = 0; i < MAXMETEOR; i++) {
		switch (meteor[i].style)
		{
		case 1:
			putimage(meteor[i].x, meteor[i].y, &img1, SRCPAINT);
			break;
		case 2:
			putimage(meteor[i].x, meteor[i].y, &img2, SRCPAINT);
			break;
		}
	}
}
void moveMeteor(int i) {

	for (i = 0; i < MAXMETEOR; i++) {
		meteor[i].x += meteor[i].step;
		meteor[i].y += (int)(meteor[i].step * 0.8);
	}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值