当前位置:网站首页>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
边栏推荐
猜你喜欢

1.16 learning summary

2021-02-07

1.21 learning summary

Use of better scroll

文件上传与安全狗

Database design (3): database maintenance and optimization

Yapi cross domain request plug-in installation

Essential foundation of programming - Summary of written interview examination sites - computer network (1) overview

Zhimeng CMS will file a lawsuit against infringing websites

Mysql8.0 configuring my SQL in INI file_ mode=NO_ AUTO_ CREATE_ User can start
随机推荐
Text horizontal alignment attribute text align and element vertical alignment attribute vertical align
[Qunhui] command line acme SH automatically apply for domain name certificate
排序查询
CTF PHP audit bypasses filtering learning from topics
2021-01-31
What are the advantages and risks of paper gold investment
CTF serialization and deserialization
TP5 distinct method paging problem
[Qunhui] this suite requires you to start PgSQL adapter service
Oracle 數據泵導錶
1.17 learning summary
Redis cluster mode
I like you!
Group by and order by are used together
ROS 笔记(07)— 客户端 Client 和服务端 Server 的实现
Multipass中文文档-使用Packer打包Multipass镜像
Is education important or ability important in software testing
Ueeditor automatically appends P tags to rich text.br tags always wrap.br tag solutions
Tp6 controller does not exist: app\index\controller\index
[H5 development] 03- take you hand in hand to improve H5 development - single submission vs batch submission with a common interface