Pascal小游戏 贪吃蛇

一段未完成的Pascal贪吃蛇

说这段代码未完成其实是没有源代码格式化,FP中一行最多只有255字符宽。

uses crt; 
const screenwidth=50; 
screenheight=24; wallchar=‘#‘; snakechar=‘*‘; ; type point=record x,y:integer; end; var snake:array [0..500] of point; map:array [0..screenwidth,0..screenheight] of 0..2; direct:0..3; score:integer; wallnum,foodnum:integer; procedure copyright; begin gotoxy(55,18); textcolor(yellow); writeln(‘Version a1.0‘); gotoxy(55,19); writeln(‘Coder:RedRooT|R.39‘); gotoxy(55,20); writeln(‘QQ:‘); gotoxy(55,21); writeln(‘E-mail:‘); gotoxy(60,22); writeln(); gotoxy(55,23); writeln( Dep.‘); end; procedure rc; begin gotoxy(1,1); end; function hitself(x,y:integer):boolean; var i:integer;ret:boolean; begin ret:=false; for i:=1 to snake[0].x do if ((snake[i].x=x) and (snake[i].y=y)) then begin ret:=true; exit; end; hitself:=ret; end; function hit:boolean; var t:Point; begin t:=snake[1]; if direct=0 then t.y:=t.y-1; if direct=1 then t.x:=t.x+1; if direct=2 then t.y:=t.y+1; if direct=3 then t.x:=t.x-1; if hitself(t.x,t.y) then hit:=true else if map[t.x,t.y]=2 then hit:=true else hit:=false; end; procedure outputxy(x,y:integer;c:char); begin gotoxy(x,y); write(c); rc; end; procedure drawscreen(diff:integer); var i,j:integer; begin clrscr;textcolor(blue); for i:=1 to screenwidth do for j:=1 to screenheight do map[i,j]:=0; for i:=1 to screenwidth do begin outputxy (i,2,wallchar); outputxy (i,screenheight,wallchar); map[i,2]:=2; map[i,screenheight]:=2; end; for i:=2 to screenheight do begin outputxy (1,i,wallchar); outputxy (screenwidth,i,wallchar); map[1,i]:=2; map[screenwidth,i]:=2; end; copyright; gotoxy (15,1);textcolor(blue); write (‘Greedy Snake Game a,1.0‘); textcolor(blue); gotoxy (57,3); write (‘Score:‘); gotoxy (57,5); write (‘Level:‘,diff,‘/20‘); gotoxy (57,7); write(‘**Game Application**‘); gotoxy(57,8); write(‘Arrow keys --> contral‘); gotoxy(65,9); write(‘P --> pause‘); gotoxy(64,10); write(‘ESC --> exit.‘); rc; end; procedure createfood; var i,j:integer; begin i:=random(screenwidth-1)+1; j:=random(screenheight-2)+2; while ((map[i,j]<>0) or (hitself(i,j))) do begin i:=random(screenwidth-1)+1; j:=random(screenheight-2)+2; end; outputxy (i,j,foodchar); map[i,j]:=1; end; procedure createwall; var p,q:integer; begin p:=random(screenwidth-1)+1; q:=random(screenheight-2)+2; while ((map[p,q]<>0) or (hitself(p,q))) do begin p:=random(screenwidth-1)+1; q:=random(screenheight-2)+2; end; outputxy (p,q,wallchar); map[p,q]:=2; end; procedure initgame(foodnum,wallnum:integer); var i,j:integer; begin snake[0].x:=1; snake[1].x:=screenwidth div 2; snake[1].y:=screenheight div 2; outputxy (snake[1].x,snake[1].y,snakechar); for i:=1 to foodnum do createfood; textcolor(red); for i:=1 to wallnum do createwall; textcolor(green); score:=0; direct:=0; outputxy (65,3,‘0‘); end; procedure die; begin rc; gotoxy(22,13); write(‘Game Over‘); Delay(60000); Delay(60000); Delay(60000); clrscr; gotoxy(30,4); write(‘Greedy Snake a1.0‘); gotoxy (20,12); write(‘Your snake has been dead.Your final score is:‘,score); window(15,19,65,23); gotoxy(1,1); textbackground(black); textcolor(red); clrscr; writeln(‘ Buite by RedRooT|R39‘); writeln(‘ QQ: Email:[email protected]‘); gotoxy(51,3); delay(60000); gotoxy(1,1); rc; clrscr; halt; end; procedure walk(diff:integer); var t:Point; food:boolean; i:integer; begin if hit then die; t:=snake[1]; if direct=0 then t.y:=t.y-1; if direct=1 then t.x:=t.x+1; if direct=2 then t.y:=t.y+1; if direct=3 then t.x:=t.x-1; if map[t.x,t.y]=1 then food:=true else food:=false; if food then snake[0].x:=snake[0].x+1; if (not food) then outputxy (snake[snake[0].x].x,snake[snake[0].x].y,‘ ‘); for i:=snake[0].x downto 2 do snake[i]:=snake[i-1]; snake[1]:=t; outputxy (t.x,t.y,snakechar); if food then begin map[t.x,t.y]:=0; score:=score+10*diff; gotoxy(65,3); write (score); rc; createfood; end; end; var i,diff,speed:integer; key:char; begin clrscr; window(1,1,80,25); textbackground(black); textcolor(blue); gotoxy(28,2);write(‘######################‘); gotoxy(28,3);write(‘# #‘); gotoxy(28,5);write(‘# #‘); gotoxy(28,6);write(‘######################‘); gotoxy(30,4); write(‘Greedy Snake a1.0‘); gotoxy(22,11); write(‘Please input the difficulty (1-20): ‘); readln(diff); while ((diff<1) or (diff>20)) do begin clrscr; gotoxy(28,2);write(‘######################‘); gotoxy(28,3);write(‘# #‘); gotoxy(28,5);write(‘# #‘); gotoxy(28,6);write(‘######################‘); gotoxy(30,4); write(‘Greedy Snake a1.0‘); gotoxy(22,11); write(‘Please input the difficulty (1-20): ‘); readln(diff); end; speed:=50 div trunc(sqrt(diff*2)); foodnum:=1;{22-diff;} wallnum:=diff*4; randomize; drawscreen(diff); initgame(foodnum,wallnum); while (true) do begin if keypressed then key:=readkey; if ord(key)=0 then key:=readkey; delay (speed*1000); if ((key=‘K‘) and (direct<>1)) then direct:=3; if ((key=‘P‘) and (direct<>0)) then direct:=2; if ((key=‘H‘) and (direct<>2)) then direct:=0; if ((key=‘M‘) and (direct<>3)) then direct:=1; if (key=‘p‘) then while (not keypressed) do; if (ord(key)=27) then begin clrscr; halt; end; walk(diff); end; end.

Pascal小游戏 贪吃蛇,布布扣,bubuko.com

时间: 2024-10-10 15:34:30

Pascal小游戏 贪吃蛇的相关文章

控制台小游戏-贪吃蛇,c++和c#版

说是c++版,其实只是用到了c++的cout和cin而已.这是我做的第二个控制台游戏,基本上每一行代码都加上了注释. 游戏嘛,我觉得重要的是了解他的思想,所以后期学了面向对象之后这个游戏的代码我也没有重新封装. 下面请看图 代码如下:我是用dev c++写的 1 //注释. ---星辰 2 3 #include <iostream> 4 #include<Windows.h> 5 #include<ctime> 6 #include<cstdlib> 7 #

java小游戏贪吃蛇大作战:来一起回顾童年回忆!

Java小项目之贪吃蛇 跪求关注,祝关注我的人都:身体健康,财源广进,福如东海,寿比南山,早上贵子,从不掉发! 贪吃蛇,绝对是很多朋友的童年记忆.几乎没有人会说没玩过这款小游戏,因为不管在小灵通手机,直板手机,还是半智能手机,甚至是现在的5G手机,都能看见这款游戏的身影.虽然游戏一直在进化,画面,风格,操作方式越来越精细,但是游戏本质的玩法是没变的,例如蛇不断变大,碰到墙壁会死亡等设定.这篇文章来分享java小游戏贪吃蛇:部分代码展示:这个贪吃蛇小游戏,玩法和操控,都是比较完善的,大家感兴趣的可

这是现代软件工程课结对作业 ——小游戏“贪吃蛇”

Github代码链接 https://github.com/liangjianming/jieduizuoye 实验简介: 本次实验为合作实验 实验分工: 本次选择了经典的游戏贪吃蛇,相信大家都玩过,这次实验分工是这样的: 实验的逻辑框架为讨论之后得出: 如果顺利吃掉食物即为成功,并输出成功,如果撞到自己和墙即为失败,输出游戏失败,添加了一些简单的游戏界面,游戏监听器和线程等, 定义食物是随机的,由 random() 决定, 并控制了窗口大小坐标: 十位*60为横坐标 weix=(int)(fo

小游戏●贪吃蛇1(利用二维数组制作)

利用二维数组编写简单贪吃蛇小游戏,由于是初学C#,用的是单线程,所以蛇不会自动前进 代码及简要分析如下: 1 //定义地图,0为空,1为墙,2为蛇,3为食物 2 int[,] map = new int[15, 15]{ 3 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 4 {1,2,0,0,0,0,0,0,0,0,0,0,0,0,1}, 5 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 6 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},

手把手教学h5小游戏 - 贪吃蛇

简单的小游戏制作,代码量只有两三百行.游戏可自行扩展延申. 源码已发布至github,喜欢的点个小星星,源码入口:game-snake 游戏已发布,游戏入口:http://snake.game.yanjd.top 第一步 - 制作想法 游戏如何实现是首要想的,这里我的想法如下: 利用canvas进行绘制地图(格子装). 利用canvas绘制蛇,就是占用地图格子.让蛇移动,即:更新蛇坐标,重新绘制. 创建四个方向按钮,控制蛇接下来的方向. 随机在地图上绘制出果子,蛇移动时"吃"到果子,增

JS的小游戏&quot;贪吃蛇&quot;

贪吃蛇儿时的回忆,今天刚好学习到这了,就刚好做了一个,也是学习了吧,需要掌握的知识: 1,JS函数的熟练掌握, 2,JS数组的应用, 3,JS小部分AJAX的学习 4,JS中的splice.shift等一些函数的应用, 基本上就这些吧,下面提重点部分: 前端的页面,这里可自行布局,我这边提供一个我自己的布局: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org

Java小游戏贪吃蛇

package snake; import java.awt.BorderLayout;import java.awt.Canvas;import java.awt.Color;import java.awt.Container;import java.awt.Graphics;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.util.Arrays;import java.util.Itera

python pygame做的小游戏(贪吃蛇)

# pygame游戏库,sys操控python运行的环境 import pygame, sys, random # 这个模块包含所有pygame所使用的常亮 from pygame.locals import * # 1,定义颜色变量 # 0-255 0黑色 255白色 redColor = pygame.Color(255, 0, 0) # 背景为黑色 blackColor = pygame.Color(0, 0, 0) # 贪吃蛇为白色 whiteColor = pygame.Color(2

控制台小游戏——贪吃蛇

网上看到的一个贪吃蛇程序,修复了一些bug(如可以直接转反方向,苹果与蛇重叠,撞到自身不会死亡等),下面是源代码. -------------------------------------------------------------------------------------------------- 1 #include<iostream> 2 #include<windows.h> 3 #include<time.h> 4 #include<stdl