Discuz! Board

 找回密码
 立即注册
查看: 385|回复: 0

贪吃蛇

[复制链接]

17

主题

17

帖子

637

积分

高级会员

Rank: 4

积分
637
发表于 2023-2-18 11:40:38 | 显示全部楼层 |阅读模式
[C++] 纯文本查看 复制代码
#include <graphics.h>                 //包含EGE的头文件
#include<time.h> //包含随机数,时间函数
#include<bits/stdc++.h>
using namespace std;
int width =800;
int height =600;
int num;
char ch;
int wallk=50;
int wallj=100;

struct body
{
    int x;
    int y; 
};
struct snake
{
	int len;
	int v;
	body bo[1000];
	char dir;
}sna;



void start()//初始化 
{
    srand(time(0));//随机种子函数      
    sna.v=20;
    sna.len=4;
	sna.dir='d';
	for(int i=0;i<4;i++)
	{
		sna.bo[i].x=20-i;
		sna.bo[i].y=15;	
    }

    
    
    
    //float g=0;
    initgraph(width, height);               //初始化图形界面
    setcolor(RED);  //设置画图线为红色
    setbkcolor(WHITE);//设置背景颜色为白色
    setfillcolor(RED);//设置填充颜色为红色
}


void move()
{
	int i;
	for(i=sna.len;i>0;i--)
	{
		sna.bo[i].x=sna.bo[i-1].x;
		sna.bo[i].y=sna.bo[i-1].y;
	}	
	switch(sna.dir)
	{
		case 'W':
		case 'w':
			sna.bo[0].y--;break;
		case 'A':
		case 'a':
			sna.bo[0].x--;break;
		case 'S':
		case 's':
			sna.bo[0].y++;break;
		case 'd':
		case 'D':
			sna.bo[0].x++;break;
	}
}
void update()//数据更新
{		

    
    /*if(ball.y>=height-ball.radius)
    {
        ball.vy=-ball.vy;
    }
    if(ball.y<=ball.radius)
    {
        ball.vy=-ball.vy;
    }
    if(ball.x>=width-ball.radius)
    {
        ball.vx=-ball.vx;
    }
    if(ball.x<=ball.radius)
    {
        ball.vx=-ball.vx;
    }*/
    if(kbhit())
    {
       sna.dir = getch();
       move();
    }
} 
void drawball()//画球 
{

    //fillellipse(ball.x, ball.y, ball.radius, ball.radius); //画一个实心圆
    for(int i=0;i<sna.len;i++)
    {
    	setfillcolor(hsv2rgb(10*i%360, 1, 1));//设置填充颜色  参数(颜色,饱和度,明亮度)
    	bar(sna.bo[i].x*20,sna.bo[i].y*20,sna.bo[i].x*20+20,sna.bo[i].y*20+20);
    	
	}
    Sleep(10);
    cleardevice();         
}

int main()
{
    start();
    while(1)
    {
        update();
        drawball();
    }
    return 0;
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-2-13 01:55 , Processed in 0.048595 second(s), 24 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.. 技术支持 by 巅峰设计

快速回复 返回顶部 返回列表