贪吃蛇小游戏的简单源代码
贪吃蛇小游戏源码主要包括以下几个部分:1. 初始化游戏界面,设置蛇的初始位置和食物的位置;2. 监听用户输入,控制蛇的移动方向;3. 更新蛇的位置,判断是否吃到食物;4. 判断蛇是否撞到墙壁或自己,如果撞到则游戏结束;5. 更新游戏界面,显示蛇和食物的位置。以下是一个简单的贪吃蛇小游戏源码示例: ```python import pygame import sys import random pygame.init() # 设置窗口大小和标题 screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption("贪吃蛇") # 初始化蛇和食物的位置 snake_pos = [[100, 100], [90, 100], [80, 100]] food
<!DOCTYPEhtml><html><head><title>贪吃蛇游戏</title><style>#game-board{width:600px;height:600px;border:1pxsolid#000;position:relative;margin:0auto;}.snake-node{width:20px;height:20px;background-color:#000;position:absolute;}#food{width:20px;height:20px;background-color:red;position:absolute;}#score{text-align:center;margin-top:20px;}#pause-btn{display:block;margin:10pxauto;}#restart-btn{display:block;margin:10pxauto;}</style></head><body><h1style="text-align:center;">贪吃蛇游戏</h1><divid="game-board"></div><divid="score">得分:<spanid="score-value">0</span></div><buttonid="pause-btn">暂停</button><buttonid="restart-btn">重新开始</button><script>vargameBoard=document.getElementById('game-board');varscoreDisplay=document.getElementById('score-value');varpauseBtn=document.getElementById('pause-btn');varrestartBtn=document.getElementById('restart-btn');vargridSize=30;//网格大小varsnakeSize=20;//蛇身大小varsnakeSpeed=200;//蛇移动速度(毫秒)varsnakeDirection='right';//蛇移动方向varsnake=[{x:0,y:0}];//蛇的初始位置varfood=null;//食物的位置varscore=0;//得分vargameInterval=null;//游戏循环定时器//初始化游戏functioninitializeGame(){createSnake();generateFood();gameInterval=setInterval(moveSnake,snakeSpeed);pauseBtn.addEventListener('click',togglePause);restartBtn.addEventListener('click',restartGame);document.addEventListener('keydown',changeDirection);}//创建蛇functioncreateSnake(){for(vari=0;i<snake.length;i++){varnode=document.createElement('div');node.className='snake-node';node.style.left=snake[i].x+'px';node.style.top=snake[i].y+'px';gameBoard.appendChild(node);}}//生成食物functiongenerateFood(){varx=getRandomPosition();vary=getRandomPosition();while(isOverlapping(x,y)){x=getRandomPosition();y=getRandomPosition();}food={x:x,y:y};varfoodNode=document.createElement('div');foodNode.id='food';foodNode.style.left=food.x+'px';foodNode.style.top=food.y+'px';gameBoard.appendChild(foodNode);}//获取一个随机位置functiongetRandomPosition(){returnMath.floor(Math.random()*gridSize)*snakeSize;}//判断位置是否与蛇或食物重叠functionisOverlapping(x,y){for(vari=0;i<snake.length;i++){if(snake[i].x===x&&snake[i].y===y){returntrue;}}if(food&&food.x===x&&food.y===y){returntrue;}returnfalse;}//移动蛇functionmoveSnake(){varnewHead={x:snake[0].x,y:snake[0].y};switch(snakeDirection){case'right':newHead.x+=snakeSize;break;case'left':newHead.x-=snakeSize;break;case'up':newHead.y-=snakeSize;break;case'down':newHead.y+=snakeSize;break;}//判断是否吃到食物if(food&&food.x===newHead.x&&food.y===newHead.y){score++;scoreDisplay.textContent=score;gameBoard.removeChild(document.getElementById('food'));generateFood();}else{snake.pop();}//判断是否游戏结束if(newHead.x<0||newHead.x>=gridSize*snakeSize||newHead.y<0||newHead.y>=gridSize*snakeSize||isOverlapping(newHead.x,newHead.y)){clearInterval(gameInterval);alert('游戏结束!得分:'+score+'\n点击"重新开始"按钮重新开始游戏。');return;}snake.unshift(newHead);updateSnakePosition();}//更新蛇的位置functionupdateSnakePosition(){varsnakeNodes=document.getElementsByClassName('snake-node');while(snakeNodes.length>0){gameBoard.removeChild(snakeNodes[0]);}for(vari=0;i<snake.length;i++){varnode=document.createElement('div');node.className='snake-node';node.style.left=snake[i].x+'px';node.style.top=snake[i].y+'px';gameBoard.appendChild(node);}}//切换暂停/继续游戏functiontogglePause(){if(gameInterval){clearInterval(gameInterval);gameInterval=null;pauseBtn.textContent='继续';}else{gameInterval=setInterval(moveSnake,snakeSpeed);pauseBtn.textContent='暂停';}}//重新开始游戏functionrestartGame(){clearInterval(gameInterval);gameBoard.innerHTML='';snake=[{x:0,y:0}];score=0;scoreDisplay.textContent=score;snakeDirection='right';initializeGame();}//改变蛇的移动方向functionchangeDirection(event){switch(event.keyCode){case37:if(snakeDirection!=='right'){snakeDirection='left';}break;case38:if(snakeDirection!=='down'){snakeDirection='up';}break;case39:if(snakeDirection!=='left'){snakeDirection='right';}break;case40:if(snakeDirection!=='up'){snakeDirection='down';}break;}}//启动游戏initializeGame();</script></body></html>