当前位置:网站首页>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
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
边栏推荐
- 6、 Project practice --- identifying cats and dogs
- pip 批量完全卸载包
- Composer version rollback version switching
- 1.24 learning summary
- PIP batch complete uninstall package
- SixTool-多功能多合一代挂助手源码
- Ubuntu installs PostgreSQL and uses omnidb to view
- Redis cluster mode
- 1.17 learning summary
- Add, delete, modify and query curd in PHP native SQL
猜你喜欢
A new paradigm for large model application: unified feature representation optimization (UFO)
Essential foundation of programming - Summary of written interview examination sites - computer network (1) overview
Tp6 controller does not exist: app\index\controller\index
CTF PHP audit bypasses filtering learning from topics
Nabicat连接:本地Mysql&&云服务Mysql以及报错
Dameng database backup and restore
MySQL enable logbin in Qunhui docker
Motivational skills for achieving goals
1.11 learning summary
Laravel framework Alipay payment fails to receive asynchronous callback request [original]
随机推荐
1.12 learning summary
PHP syntax summary
Composer version rollback version switching
排序查询
NVM installation and use and NPM package installation failure record
PHP get mobile number operator
MySQL index details
Thinkphp6 implements a simple lottery system
Simple personal summary of tp6 multi application deployment -- Part I [original]
Tp6 is easy to tread [original]
1.17 learning summary
Oracle data pump table
Resolve PHP is not an internal or external command
[Qunhui] this suite requires you to start PgSQL adapter service
Is education important or ability important in software testing
Thinkphp6 using kindeditor
Navicat connects the pit of shardingsphere sub table and sub library plug-ins
Rdkit chemical formula molecular formula search
OSS CDN alicloud configuration method
2020-12-18