当前位置:网站首页>2022.2.10

2022.2.10

2022-06-26 04:38:00 bu_ xiang_ tutou

9:40----13:00

Compress Words

15:00---18:00

[BOI2009]Radio Transmission Wireless transmission

P4391 [BOI2009]Radio Transmission Wireless transmission - Luogu | New ecology of computer science education (luogu.com.cn)

Adjust Snake code

#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <easyx.h>
#include <time.h>
#include <conio.h>
#include <cstring>
#define snakenum 30000// Macro definitions do not have semicolons 
#define _CRT_SECURE_NO_WARNINGS
HWND  hnd = NULL;// Represents the main window 
// The snake , food , coordinate 
enum position { right = 77, left = 75, down = 80, up = 72 };
struct Snake// The snake 
{
	int size;// The number of knots 
    char dir;// Direction 
	POINT xy[snakenum];// Snake coordinates 
}snake;
struct Food// food 
{
	POINT foodxy;// Food coordinates 
	int eatgrade=0;// score 
	int sign;// Record whether the food is present 
}food;
void initsnake()// Initialize snake 
{
	snake.xy[2].x = 10;
	snake.xy[2].y = 10;
	snake.xy[1].x = 20;
	snake.xy[1].y = 10;
	snake.xy[0].x = 30;// The snake head is in front 
	snake.xy[0].y = 10;
	snake.size = 3;// Initialize snake , The snake started with 3 section 
	snake.dir = right;
	food.sign = 0;
}
void drawsnake()// Draw a snake  
{
	for (int i = 0; i < snake.size; i++)
	{
		setfillcolor(YELLOW);// Set the color of the snake , Before you draw a snake 
		setlinecolor(BLACK);
		fillrectangle(snake.xy[i].x, snake.xy[i].y, snake.xy[i].x+10,snake.xy[i].y+10);// Draw a snake 
	}
}
void movesnake()// Move the snake 
{
	for (int i = snake.size - 1; i > 0; i--)
	{
		snake.xy[i].x = snake.xy[i - 1].x;
		snake.xy[i].y = snake.xy[i - 1].y;
	}
	switch (snake.dir)
	{
	case up:
		snake.xy[0].y -= 10;
		break;
	case down:
		snake.xy[0].y += 10;
		break;
	case left:
		snake.xy[0].x -= 10;
		break;
	case right:
		snake.xy[0].x += 10;
		break;
	default:
			break;
	}
}
void keydown()// Keyboard control 
{
	char userskey = 0;
	userskey =_getch();
	switch (userskey)
	{
	case right:
		if (snake.dir != left)
			snake.dir = right;
		break;
	case left:
		if (snake.dir != right)
			snake.dir = left;
		break;
	case up:
		if (snake.dir != down)
			snake.dir = up;
		break;
	case down:
		if (snake.dir != up)
			snake.dir = down;
		break;
	default :
		break;
	}
}
void initfood()// Initializing food 
{
	food.foodxy.x = rand() % 64 * 10;
	food.foodxy.y = rand() % 48 * 10;
	for (int i = 0; i < snake.size; i++)
	{
		if (food.foodxy.x == snake.xy[i].x && food.foodxy.y == snake.xy[i].y)
		{
			food.foodxy.x = rand() % 64 * 10;
			food.foodxy.y = rand() % 48 * 10;
		}
	} 
	food.sign = 1;
}
void drawfood()// Painting food 
{
	setfillcolor(RGB(rand() % 250, rand() % 250, rand() % 250));// Set the color of the food , Before you draw a snake 
	setlinecolor(RGB(rand() % 250, rand() % 250, rand() % 250));
	fillrectangle(food.foodxy.x, food.foodxy.y, food.foodxy.x+10, food.foodxy.y+10);
}
void eatfood()// Eat food 
{
	if (snake.xy[0].x == food.foodxy.x && snake.xy[0].y == food.foodxy.y)
	{
		snake.size+=1;// The snake is getting longer 
		food.eatgrade += 1;// Scores increase 
		food.foodxy.x = rand() % 64 * 10;
		food.foodxy.y = rand() % 48 * 10;
		food.sign = 0;
	}
}
void show()// Show the score 
{
	char str[10000];// The window can only display strings , Convert an integer to a string 
	sprintf(str, "%d", food.eatgrade);
	setbkmode(TRANSPARENT);// Set to transparent mode 
	settextcolor(BLACK);
	SetWindowText(hnd," snake ");
	outtextxy(560, 20, " fraction :");
	outtextxy(600, 20, str);
}
int snakedie()// Snake death 
{
	if (snake.xy[0].x > 640 || snake.xy[0].x < 0 || snake.xy[0].y>480 || snake.xy[0].y < 0)
	{
		outtextxy(200, 220," The wall was ");
		MessageBox(NULL, " The wall was ", "Gameover",MB_OKCANCEL);// Pop-up window 
		return 1;
	}
	for (int i = 1; i < snake.size; i++)
	{
		if(snake.xy[0].x==snake.xy[i].x&& snake.xy[0].y == snake.xy[i].y)
		{
			outtextxy(200, 220," Hit yourself ");
			MessageBox(NULL, " Hit yourself ", "Gameover", MB_OKCANCEL);
			return 1;
		}
	}
	return 0;
}
int main()
{
	initgraph(640, 480);// initialization 
	setbkcolor(WHITE);// Set the background color 
	srand((unsigned)time(NULL));// Plant random seeds 
	initsnake();
	while (1)
	{
		cleardevice();
		if (food.sign == 0)
		{
			initfood();// initialization 
		}
		BeginBatchDraw();// Prevent flashing 
		drawfood();
		eatfood();
		show();
		drawsnake();
		EndBatchDraw();// Prevent flashing 
		if (snakedie())
			break;
		movesnake();
		while (_kbhit())
		{
			keydown();
		}
		Sleep(50);// The speed at which the snake moves 
	}
	system("pause");
	return 0;
}

19:00----21:00

(13 Bar message ) hash problem _bu_xiang_tutou The blog of -CSDN Blog

(13 Bar message ) kmp Simple application of _bu_xiang_tutou The blog of -CSDN Blog

原网站

版权声明
本文为[bu_ xiang_ tutou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180512196013.html