穿墙效果

本文介绍了一种使用HTML和CSS创建动态鼠标悬停效果的方法,通过JavaScript增强用户体验,当鼠标悬停在图片上时,会显示一个带有文字的红色透明遮罩层。
  1 <!doctype html>
  2 <html>
  3 <head>
  4 <meta charset="utf-8">
  5 <title>无标题文档</title>
  6 <style>
  7 <style>
  8         *{
  9             margin: 0;
 10             padding: 0;
 11             list-style: none;
 12         }
 13         #ul1{
 14             width: 880px;
 15             margin: 100px auto;
 16         }
 17         #ul1 li{
 18             width: 200px;
 19             height: 200px;
 20             margin: 10px;
 21             float: left;
 22             position: relative;
 23             overflow: hidden;
 24         }
 25         #ul1 li img{
 26             width: 100%;
 27             height: 100%;
 28         }
 29         #ul1 li span{
 30             width: 100%;
 31             height: 100%;
 32             position: absolute;
 33             left: -200px;
 34             top: 0;
 35             background: red;
 36             opacity: 0.3;
 37             font-size: 50px;
 38             line-height: 200px;
 39             text-align: center;
 40             color: #fff;
 41         }
 42 </style>
 43 <script src="move.js"></script>
 44 <script>
 45 function enter(obj,oEvent){
 46     var l=oEvent.clientX-obj.offsetLeft;
 47     var r=obj.offsetLeft+obj.offsetWidth-oEvent.clientX;
 48     var t=oEvent.clientY-obj.offsetTop;
 49     var b=obj.offsetTop+obj.offsetHeight-oEvent.clientY;        
 50     switch(Math.min(l,r,t,b)){
 51         case l:
 52             return 'l';
 53             break;
 54         case r:
 55             return 'r';
 56             break;
 57         case t:
 58             return 't';
 59             break;
 60         case b:
 61             return 'b';
 62             break;
 63     }
 64 }
 65 window.onload=function(){
 66     var aLi=document.getElementsByTagName('li');
 67     var aSpan=document.getElementsByTagName('span');
 68     for(var i=0;i<aLi.length;i++){
 69         aLi[i].onmouseenter=function(ev){
 70             var oEvent=ev ||event;
 71             var dir=enter(this,oEvent);
 72             switch(dir){
 73                 case 'l':
 74                 this.children[0].style.left=-200+'px';
 75                 this.children[0].style.top=0+'px';
 76                 move(this.children[0],{left:0,top:0});
 77                 break;    
 78                 case 'r':
 79                 this.children[0].style.left=200+'px';
 80                 this.children[0].style.top=0+'px';
 81                 move(this.children[0],{left:0,top:0});
 82                 break;    
 83                 case 't':
 84                 this.children[0].style.left=0+'px';
 85                 this.children[0].style.top=-200+'px';
 86                 move(this.children[0],{left:0,top:0});
 87                 break;    
 88                 case 'b':
 89                 this.children[0].style.left=0+'px';
 90                 this.children[0].style.top=200+'px';
 91                 move(this.children[0],{left:0,top:0});
 92                 break;    
 93             }    
 94         };
 95         aLi[i].onmouseleave=function(ev){
 96             var oEvent=ev ||event;
 97             var dir=enter(this,oEvent);
 98             switch(dir){
 99                 case 'l':
100                 move(this.children[0],{left:-200,top:0});
101                 break;    
102                 case 'r':
103                 move(this.children[0],{left:200,top:0});
104                 break;    
105                 case 't':
106                 move(this.children[0],{left:-200,top:0});
107                 break;    
108                 case 'b':
109                 move(this.children[0],{left:200,top:0});
110                 break;    
111             }    
112         };        
113     }    
114 };
115 </script>
116 </head>
117 
118 <body>
119 <ul id="ul1">
120     <li><span>猫一</span><img src="cat/1.jpg"></li>
121     <li><span>猫二</span><img src="cat/2.jpg"></li>
122     <li><span>猫三</span><img src="cat/3.jpg"></li>
123     <li><span>猫四</span><img src="cat/4.jpg"></li>
124     <li><span>猫五</span><img src="cat/5.jpg"></li>
125     <li><span>猫六</span><img src="cat/6.jpg"></li>
126     <li><span>猫七</span><img src="cat/7.jpg"></li>
127     <li><span>猫八</span><img src="cat/8.jpg"></li>
128 </ul>
129 </body>
130 </html>

 

转载于:https://www.cnblogs.com/lixuekui/p/5808738.html

#include <iostream> #include <conio.h> #include <windows.h> #include <ctime> #include <deque> #include <cstdlib> using namespace std; const int WIDTH = 40; // 游戏区域宽度 const int HEIGHT = 20; // 游戏区域高度 // 定义方向枚举 enum Direction { STOP = 0, LEFT, RIGHT, UP, DOWN }; // 游戏状态结构体 struct GameState { int snakeX, snakeY; // 蛇头位置 int fruitX, fruitY; // 水果位置 int chestX, chestY; // 宝箱位置 int score; // 得分 Direction direction; // 当前方向 bool gameOver; // 游戏结束标志 deque<int> tailX; // 蛇身X坐标队列 deque<int> tailY; // 蛇身Y坐标队列 int tailLength; // 蛇身长度 bool chestActive; // 宝箱是否激活 int chestTimer; // 宝箱刷新计时器 const int CHEST_SPAWN_INTERVAL = 50; // 宝箱刷新间隔 bool mapFull = false; // 标志地图是否被填满 int speedLevel = 0; // 新增:速度等级 (0-初始速度, 1-加快, 2-更快等) }; // 设置控制台光标位置 void SetCursorPosition(short x, short y) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos = {x, y}; SetConsoleCursorPosition(hConsole, pos); } // 隐藏控制台光标 void HideCursor() { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cursorInfo; GetConsoleCursorInfo(hConsole, &cursorInfo); cursorInfo.bVisible = false; SetConsoleCursorInfo(hConsole, &cursorInfo); } // 检查位置是否被占用 bool IsPositionOccupied(const GameState& state, int x, int y) { // 如果是地图满状态,所有位置都被占用 if (state.mapFull) return true; // 检查蛇头 if (state.snakeX == x && state.snakeY == y) return true; // 检查水果 if (state.fruitX == x && state.fruitY == y) return true; // 检查宝箱 if (state.chestActive && state.chestX == x && state.chestY == y) return true; // 检查蛇身 for (int i = 0; i < state.tailLength; i++) { if (state.tailX[i] == x && state.tailY[i] == y) return true; } return false; } // 检查地图是否被完全占据 bool IsMapFull(const GameState& state) { // 总格子数 int totalCells = WIDTH * HEIGHT; // 如果蛇身长度+1(蛇头)小于总格子数,地图肯定没满 if (state.tailLength + 1 < totalCells) return false; // 创建一个虚拟地图,标记所有占据位置 bool occupied[HEIGHT][WIDTH] = {false}; // 标记蛇头位置 occupied[state.snakeY][state.snakeX] = true; // 标记蛇身位置 for (int i = 0; i < state.tailLength; i++) { occupied[state.tailY[i]][state.tailX[i]] = true; } // 遍历所有格子,如果发现任何空位,返回false for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { if (!occupied[y][x]) return false; // 发现空位,地图没满 } } return true; // 所有格子都被占满 } // 初始化游戏状态 void Setup(GameState& state) { state.gameOver = false; state.direction = STOP; state.snakeX = WIDTH / 2; state.snakeY = HEIGHT / 2; // 随机生成水果位置 state.fruitX = rand() % WIDTH; state.fruitY = rand() % HEIGHT; // 初始化宝箱 state.chestActive = false; state.chestTimer = 0; state.score = 0; state.tailLength = 0; state.mapFull = false; // 初始状态地图不满 state.speedLevel = 0; // 初始速度等级 state.tailX.clear(); state.tailY.clear(); } // 生成新水果(带安全机制) void GenerateFruit(GameState& state) { // 如果地图被填满,跳过生成水果 if (state.mapFull) { return; } const int MAX_ATTEMPTS = 100; // 最大尝试次数 int attempts = 0; do { state.fruitX = rand() % WIDTH; state.fruitY = rand() % HEIGHT; attempts++; // 防止无限循环,达到最大尝试次数后停止 if (attempts >= MAX_ATTEMPTS) { // 如果尝试了很多次仍失败,标记地图已满 state.mapFull = IsMapFull(state); break; } } while (IsPositionOccupied(state, state.fruitX, state.fruitY)); } // 生成新宝箱(带安全机制) void GenerateChest(GameState& state) { // 如果地图被填满,跳过生成宝箱 if (state.mapFull) { return; } const int MAX_ATTEMPTS = 100; // 最大尝试次数 int attempts = 0; do { state.chestX = rand() % WIDTH; state.chestY = rand() % HEIGHT; attempts++; // 防止无限循环 if (attempts >= MAX_ATTEMPTS) { state.chestActive = false; // 放弃生成宝箱 state.chestTimer = state.CHEST_SPAWN_INTERVAL / 2; // 设置半程计时器 state.mapFull = IsMapFull(state); // 检查地图是否已满 break; } } while (IsPositionOccupied(state, state.chestX, state.chestY)); if (attempts < MAX_ATTEMPTS) { state.chestActive = true; } } // 绘制游戏界面 void Draw(const GameState& state) { // 设置控制台位置到左上角 SetCursorPosition(0, 0); // 绘制上边框 for (int i = 0; i < WIDTH + 2; i++) cout << "#"; cout << endl; // 绘制游戏区域 for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { // 左边界 if (j == 0) cout << "#"; // 绘制蛇头 if (i == state.snakeY && j == state.snakeX) cout << "O"; // 绘制水果 else if (i == state.fruitY && j == state.fruitX) cout << "@"; // 绘制宝箱 else if (state.chestActive && i == state.chestY && j == state.chestX) cout << "$"; else { bool isTail = false; // 绘制蛇身 for (int k = 0; k < state.tailLength; k++) { if (state.tailX[k] == j && state.tailY[k] == i) { cout << "o"; isTail = true; break; } } if (!isTail) cout << " "; } // 右边界 if (j == WIDTH - 1) cout << "#"; } cout << endl; } // 绘制下边框 for (int i = 0; i < WIDTH + 2; i++) cout << "#"; cout << endl; // 显示游戏信息 cout << "得分: " << state.score << " "; cout << "蛇身长度: " << state.tailLength << " "; // 显示速度等级 cout << "速度: " << state.speedLevel + 1 << "级 "; if (state.mapFull) { cout << "地图已满! "; } else if (state.chestActive) { cout << "宝箱: ($) "; } else { int timeLeft = (state.CHEST_SPAWN_INTERVAL - state.chestTimer) / 5; cout << "下个宝箱: " << max(0, timeLeft) << " "; } cout << "按 'x'退出游戏" << endl; } // 处理用户输入 void ProcessInput(GameState& state) { // 检查是否有按键输入 if (_kbhit()) { switch (_getch()) { case 'a': case 'A': if (state.direction != RIGHT) state.direction = LEFT; break; case 'd': case 'D': if (state.direction != LEFT) state.direction = RIGHT; break; case 'w': case 'W': if (state.direction != DOWN) state.direction = UP; break; case 's': case 'S': if (state.direction != UP) state.direction = DOWN; break; case 'x': case 'X': state.gameOver = true; break; case 'p': case 'P': // 暂停功能 while (true) { if (_kbhit() && _getch() == 'p') break; Sleep(100); } break; } } } // 更新速度等级(基于蛇身长度) void UpdateSpeedLevel(GameState& state) { // 每5个长度提升一个速度等级 int newLevel = state.tailLength / 5; // 仅当等级提升时才显示提示 if (newLevel > state.speedLevel) { state.speedLevel = newLevel; // 在游戏界面显示速度提升提示 SetCursorPosition(0, HEIGHT + 3); cout << "蛇身长度达到 " << (state.speedLevel * 5) + 5 << ",速度提升!"; Sleep(800); // 短暂显示提示 // 清除提示 SetCursorPosition(0, HEIGHT + 3); cout << " "; SetCursorPosition(0, HEIGHT + 3); // 重置位置 } } // 更新游戏逻辑 void Update(GameState& state) { // 如果地图已满,直接游戏胜利 if (state.mapFull) { state.gameOver = true; return; } // 保存蛇头位置 int prevHeadX = state.snakeX; int prevHeadY = state.snakeY; // 更新蛇头位置 switch (state.direction) { case LEFT: state.snakeX--; break; case RIGHT: state.snakeX++; break; case UP: state.snakeY--; break; case DOWN: state.snakeY++; break; } // 处理穿墙效果 if (state.snakeX < 0) state.snakeX = WIDTH - 1; if (state.snakeX >= WIDTH) state.snakeX = 0; if (state.snakeY < 0) state.snakeY = HEIGHT - 1; if (state.snakeY >= HEIGHT) state.snakeY = 0; // 检查是否撞到自己 for (int i = 0; i < state.tailLength; i++) { if (state.tailX[i] == state.snakeX && state.tailY[i] == state.snakeY) state.gameOver = true; } // 如果蛇在移动,更新蛇身位置 if (state.direction != STOP) { // 添加新的头部位置 state.tailX.push_front(prevHeadX); state.tailY.push_front(prevHeadY); // 吃到水果 if (state.snakeX == state.fruitX && state.snakeY == state.fruitY) { // 增加分数 state.score += 10; state.tailLength++; // 检查速度等级是否提升 UpdateSpeedLevel(state); // 检查地图是否即将满 state.mapFull = IsMapFull(state); // 生成新水果位置 GenerateFruit(state); } // 吃到宝箱 else if (state.chestActive && state.snakeX == state.chestX && state.snakeY == state.chestY) { // 增加分数 state.score += 50; // 身体增加两个长度 state.tailLength += 2; // 检查速度等级是否提升 UpdateSpeedLevel(state); // 检查地图是否即将满 state.mapFull = IsMapFull(state); // 重置宝箱状态 state.chestActive = false; state.chestTimer = 0; } // 什么都没吃到,移除尾部 else { if (!state.tailX.empty()) { state.tailX.pop_back(); state.tailY.pop_back(); } } } // 更新宝箱逻辑 if (!state.chestActive) { state.chestTimer++; // 达到刷新时间 if (state.chestTimer >= state.CHEST_SPAWN_INTERVAL) { GenerateChest(state); } } } // 计算游戏速度(基于速度等级) int CalculateGameSpeed(const GameState& state) { // 速度等级对应的延迟毫秒数(越小越快) const int speedLevels[6] = { 150, // 等级0: 基础速度 (0-4长度) 120, // 等级1: 基础+1 (5-9长度) 90, // 等级2: 基础+2 (10-14长度) 70, // 等级3: 基础+3 (15-19长度) 50, // 等级4: 基础+4 (20-24长度) 30 // 等级5: 基础+5 (25+长度) }; // 获取当前速度等级对应的延迟 int levelIndex = min(state.speedLevel, 5); return speedLevels[levelIndex]; } int main() { // 设置随机种子 srand(static_cast<unsigned>(time(0))); // 隐藏光标 HideCursor(); // 设置控制台标题 SetConsoleTitle("贪吃蛇游戏 - 长度等级速度系统"); // 初始化游戏状态 GameState game; Setup(game); // 游戏主循环 while (!game.gameOver) { Draw(game); ProcessInput(game); Update(game); // 根据速度等级计算游戏速度 int speed = CalculateGameSpeed(game); Sleep(speed); } // 游戏结束处理 system("cls"); if (game.mapFull) { cout << "恭喜你通关了!整个地图都被蛇填满了!\n"; } else { cout << "游戏结束!\n"; } cout << "你的最终得分: " << game.score << endl; cout << "蛇身长度: " << game.tailLength << endl; cout << "最终速度等级: " << game.speedLevel + 1 << endl; cout << "按任意键退出..." << endl; _getch(); return 0; }帮我对上述代码每一行做出解释
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值