day8

Uncategorized
1.7k words

day8 - 拨云见日

IT实战项目

还记的走迷宫的那个程序吗?

只能按wasd键吗,只能用getch()吗

按键检测:

1
2
3
4
5
6
7
8
9
10
11
12
13
if(_kbhit()){
if(GetAsyncKeyState(VK_UP))
dir=UP;
else
if(GetAsyncKeyState(VK_DOWN))
dir=DOWN;
else
if(GetAsyncKeyState(VK_LEFT))
dir=LEFT;
else
if(GetAsyncKeyState(VK_RIGHT))
dir=RIGHT;
}
  1. 奔跑的H

一般做法:通过打印合适的空格来显示H奔跑的效果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<bits/stdc++.h>
#include<windows.h> //Sleep函数在里面
using namespace std;
int main(){
int i,j;
for(i=1;i<=10;i++){
system("cls");
for(j=1;j<=i-1;j++)
printf(" ");
printf("H");
Sleep(1000);
}
return 0;
}

有没有更方便的做法?

直接通过API来设置光标的位置。不用每次记录整个地图,再来全部打印出来。

1
2
3
4
5
6
7
8
void SetCCPos(int x, int y) {
HANDLE hOut; //句柄 HANDLE
hOut = GetStdHandle(STD_OUTPUT_HANDLE); //获取标注输出句柄
COORD pos; //在屏中的位置坐标 (x,y)
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hOut, pos); //偏移光标位置
}

注意:这个x, y坐标是横坐标向右是x坐标正方向,纵坐标向下是y坐标正方向。坐标包含(0, 0)

完整代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
void SetCCPos(int x, int y) {
HANDLE hOut; //句柄 HANDLE
hOut = GetStdHandle(STD_OUTPUT_HANDLE); //获取标注输出句柄
COORD pos; //在屏中的位置坐标 (x,y)
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hOut, pos); //偏移光标位置
}
int main(){
int i,j;
for(i=1;i<=10;i++){
system("cls");
SetCCPos(i,0);
printf("H");
Sleep(1000);
}
return 0;
}

感觉光标显示出来很难看?

1
2
3
4
/****************** 隐 藏 光 标 *************************/ 
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
/************** 此 处 插 入 你 的 程 序 *****************/
  1. C++贪吃蛇

知识点:

  1. 动态数组:vector, push_back()函数用法,size()函数

  2. 枚举类型:enum用法

  3. 死亡判定,碰撞检测

  4. 控制窗口大小

  5. 蛇身的跟随:下一段蛇身的坐标等于上一段蛇身的坐标

  6. 结构体的含义和用法

1
2
3
//蛇身的跟随 
for(int i = nodes.size() - 1;i > 0;i--)
nodes[i]=nodes[i-1];

完整代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// C语言贪吃蛇 
#include<cstdlib> // 要有
#include<windows.h>
#include<conio.h>
#include<vector>
#include<iostream>
#include<ctime>

#define COL 30 //行
#define ROW 50 //列

const int InitLength=5;

enum Dir{UP,DOWN,LEFT,RIGHT};
int dir=RIGHT;

int is_food=1;
int food_x,food_y;

struct Snake{
Snake(){}
Snake(int x,int y){_x=x; _y=y;}
int _x,_y;
};
std::vector<Snake> nodes;

void SetCCPos(int x, int y) {
HANDLE hOut; //句柄 HANDLE
hOut = GetStdHandle(STD_OUTPUT_HANDLE); //获取标注输出句柄
COORD pos; //在屏中的位置坐标 (x,y)
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hOut, pos); //偏移光标位置
}

void InitSnake(){
for(int i = InitLength;i >= 1;i--)
nodes.push_back(Snake(i,1));
}
//碰撞检测
bool collision(){
int x=nodes[0]._x;
int y=nodes[0]._y;
if(x <= 0 || x >= ROW || y <= 0 || y >= COL)
return false;
for(int i = 1;i < nodes.size();i++)
if(x==nodes[i]._x && y==nodes[i]._y)
return false;
return true;
}

void CreateFood(){
food_x=rand()%(ROW - 2) + 1;
food_y=rand()%(COL - 2) + 1;
SetCCPos(food_x,food_y);
std::cout<<"*";
}

void DrawFood(){
if(is_food)
{
SetCCPos(food_x,food_y);
std::cout<<"*";
}
}

void DrawSnake(){
for(int i = 0;i < nodes.size();i++)
{
SetCCPos(nodes[i]._x,nodes[i]._y);
std::cout<<"O";
}
}

void MoveSnake(){

//蛇身的跟随
for(int i = nodes.size() - 1;i > 0;i--)
nodes[i]=nodes[i-1];
//按键检测
if(_kbhit()){
if(GetAsyncKeyState(VK_UP))
dir=UP;
else
if(GetAsyncKeyState(VK_DOWN))
dir=DOWN;
else
if(GetAsyncKeyState(VK_LEFT))
dir=LEFT;
else
if(GetAsyncKeyState(VK_RIGHT))
dir=RIGHT;
}
switch(dir){
case(UP): nodes[0]._y -= 1;break;
case(DOWN): nodes[0]._y += 1;break;
case(LEFT): nodes[0]._x -= 1;break;
case(RIGHT): nodes[0]._x += 1;break;
}
}

void Drawbackground(){
//设置背景及边框

for(int i = 0; i < ROW; i++)
{
SetCCPos(i,0);
std::cout<<"#";
}
for(int i = 0; i < ROW; i++)
{
SetCCPos(i,COL-1);
std::cout<<"#";
}
for(int i = 1; i < COL - 1; i++)
{
SetCCPos(0,i);
std::cout<<"#";
}
for(int i = 1; i < COL - 1; i++)
{
SetCCPos(ROW-1,i);
std::cout<<"#";
}
}

void InitGame(){
//初始化蛇
InitSnake();
//初始化背景
system("mode con cols=50 lines=30");
// cols=ROW lines=ROW
Drawbackground();

/****************** 隐 藏 光 标 *************************/
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
/************** 此 处 插 入 你 的 程 序 *****************/

CreateFood();
}

int main(){
InitGame();
// 设置随机数种子
srand(time(NULL));
while(1){
MoveSnake();
system("cls"); //清屏,但是会闪屏
DrawSnake();
Drawbackground();
DrawFood();
if(nodes[0]._x==food_x && nodes[0]._y==food_y){
CreateFood();
nodes.push_back(Snake());
}
if(!collision()){
MessageBox(NULL,"你死了","",MB_OK|MB_ICONHAND);
return 0;
}
Sleep(100); //等待很卡,可以不要
}
}

课外实践 - HTML贪吃蛇

HTML(超文本标记语言)是一种用于创建网页结构和内容的标记语言。它是构建网页的基础,用于描述文本、图像、链接、表单等元素的排布和呈现方式。HTML 使用一系列的标签(也称为标记)来定义这些元素,并将它们嵌套在一起以构建网页的整体结构。

HTML 并不是一种编程语言,而是一种标记语言。它用于指示浏览器如何渲染页面的不同部分,以及如何将文本和媒体内容组织在一起。HTML 文件通常以 .html.htm 扩展名保存。

HTML 标签是以尖括号包围的单词,如 <tagname>,其中 tagname 是标签的名称。HTML 标签通常是成对出现的,包括一个开放标签和一个闭合标签。例如:

1
<p>这是一个段落。</p>

在上述示例中,<p> 是开放标签,</p> 是闭合标签,它们一起定义了一个段落。

HTML 还可以包含属性,属性提供了关于标签的额外信息。例如,链接元素 <a> 可以使用 href 属性来指定链接的目标地址:

1
<a href="https://www.example.com">访问示例网站</a>

除了创建基本结构和内容,HTML 还支持嵌套和组合,以创建更复杂的页面布局。然而,HTML 本身不负责页面的样式和交互,这些方面通常由 CSS(层叠样式表)和 JavaScript 来处理。

总之,HTML 是用于构建网页结构和内容的标记语言,它是构建互联网上所有网页的基础。

程序员:chatGPT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<!DOCTYPE html>
<html>
<head>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
canvas {
border: 2px solid #000;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<script>
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");

const gridSize = 20;
let snake = [{ x: 5, y: 5 }];
let food = { x: 10, y: 10 };
let direction = "right";

function drawSnake() {
ctx.fillStyle = "#00ff00";
for (let i = 0; i < snake.length; i++) {
ctx.fillRect(snake[i].x * gridSize, snake[i].y * gridSize, gridSize, gridSize);
}
}

function drawFood() {
ctx.fillStyle = "#ff0000";
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize, gridSize);
}

function moveSnake() {
let newHead = { x: snake[0].x, y: snake[0].y };
switch (direction) {
case "up":
newHead.y -= 1;
break;
case "down":
newHead.y += 1;
break;
case "left":
newHead.x -= 1;
break;
case "right":
newHead.x += 1;
break;
}

snake.unshift(newHead);

if (newHead.x === food.x && newHead.y === food.y) {
generateFood();
} else {
snake.pop();
}
}

function generateFood() {
food = {
x: Math.floor(Math.random() * canvas.width / gridSize),
y: Math.floor(Math.random() * canvas.height / gridSize)
};
}

function checkCollision() {
if (
snake[0].x < -1 ||
snake[0].x >= canvas.width / gridSize ||
snake[0].y < 0 ||
snake[0].y >= canvas.height / gridSize
) {
clearInterval(gameLoop);
alert("Game Over");
}
}

function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);

drawSnake();
drawFood();
moveSnake();
checkCollision();
}

document.addEventListener("keydown", (event) => {
switch (event.key) {
case "ArrowUp":
if (direction !== "down") {
direction = "up";
}
break;
case "ArrowDown":
if (direction !== "up") {
direction = "down";
}
break;
case "ArrowLeft":
if (direction !== "right") {
direction = "left";
}
break;
case "ArrowRight":
if (direction !== "left") {
direction = "right";
}
break;
}
});

generateFood();
const gameInterval = setInterval(gameLoop, 200);
</script>
</body>
</html>

致谢

红日初生,其道大光;河出伏流,一泻汪洋。

当今世界,程序员才是能够改变世界的魔术师,正如米哈游口号:技术宅改变世界。愿汝辈能抱定梦想,不弱星光之势。