资源描述
车辆通过十字路口的演示程序
学 号:
姓 名:
指导老师:
2010年12月
目录
1.需求分析…………………………………………………3
2.概要设计…………………………………………………5
3.测试 ……………………………………………………6
4.用户手册…………………………………………………6
5.总结提高…………………………………………………6
6.程序代码 …………………………………………………7
1 需求分析
1.1 功能与数据需求
1.11 基本功能
(1)基本功能只考虑东西(或南北)方向有车辆,暂不考虑交叉方向的车辆。
(2)显示十字路口和交通灯的图案。
(3)按F1键开始出现车辆;按ESC键程序结束。
(4)按规律定时切换交通灯。
(5)各方向(即东西方向)在路的尾端随机出现车辆,出现的车辆逐渐进入屏幕,
车辆大小随机设置;车辆达到屏幕另一端时逐渐消失。
(6)各方向(即东西方向)的每辆车按自己的速度匀速运动,遇红灯或黄灯停车,
绿灯前行。
(7)各个车辆不能相撞。
1.12 拓展功能
(1)车辆的速度随机产生(产生随机速度后,则保持此速度),以确保车辆的速度
不相尽同。
(2)可以通过鼠标点击按钮的方式或按键的方式将当前车辆行驶状况和交通灯的状况
保存到文件中,并可从文件中恢复储存前的状态。
(3)在四个方向都有车。
1.13 数据分析
1.2 界面需求
界面的背景色是深灰色。因为只考虑了南北向通车,所以没有开启东西向的红绿灯,所以改为灰色以示关闭。车辆的大小,颜色,均是随机的。
1.3 界面展示
1.4 开发与运行环境需求
本课设是在TC3.0的图形模式环境下开发的,得通过TC3.0(或者是TC2.0)来运行这个程序。
2 概要设计
2.1 主要数据结构
这个程序的主要数据就是小车的数据。
根据教材和辅导教师的指导,我利用该结构体构建链表。
typedef struct car{
int len; /*小车的长度*/
int wei; /*小车的宽度*/
int x, y; /*小车的位置*/
int color; /*小车的颜色*/
int speed; /*小车的速度*/
struct car* next; /*用于指向下一个小车结构体*/
} CAR;
2.2 程序总体结构
总体结构大体分为:
#include<>/*声明函数*/
typedef struct car/*小车结构体*/
void setbk()/*设置背景*/
void changud(int c)/*设置红绿灯变化*/
struct car* newcar()/*新出现的小车的结构体*/
void drawcar(struct car* c,int color)/*画出新出现的小车*/
struct car* drawutd(struct car* t,int lt)/*北通向南小车的链表*/
struct car* drawdtu(struct car* t,int lt)/*南通向北小车的链表*/
void play()/*利用键盘操作开始与结束及灯的变化*/
main()/*主函数*/
3 测试
程序开始的测试
测试用例:在程序开始的之前按的键不是F1或者ESC;
测试结果:没有任何反应,不会对其他按键产生回应。
4 用户手册
用户可以通过按F1键开始程序,按ESC键结束程序。
5 总结提高
5.1 课程设计总计总结
这次程序设计对于像我这种接触c语言不久的学生而言实在是太具有挑战性。在设计的过程中屡次出现让我束手无策的问题。例如我多次试用do while 循环,想要使新出现的小车的颜色与背景颜色不同,然而无论我怎么更改,都还是会出现与背景色相同的小车。最终迫使我只能减少可出现的颜色,才貌似解决了问题。
总而言之,这次课设颇让人费尽脑筋,但也使我觉得,其实编程并非一件极其遥远的事。
5.2 对本课程的意见和建议
对于初次接触C语言的学生而言,还是太具难度。虽然知道教师资源有限,但还是希望能够有人辅导。
6 程序代码
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <graphics.h>
#include <dos.h>
#include <conio.h>
#include<bios.h>
#define F1 0x3b00
#define ESC 0x011b
typedef struct car{
int len;
int wei;
int x, y;
int color;
int speed;
struct car* next;
} CAR;
void setbk()
{
setbkcolor(DARKGRAY);
setlinestyle(0, 0, 1);
setfillstyle(SOLID_FILL, 7);
gotoxy(5,1);
printf("F1 = start");
gotoxy(20,1);
printf("ESC = quit");
line(0, 190, 270, 190);
line(0, 290, 270, 290);
line(370, 190, 680, 190);
line(370, 290, 680, 290);
line(270, 190, 270, 0);
line(370, 190, 370, 0);
line(270, 290, 270, 480);
line(370, 290, 370, 480);
setlinestyle(1, 0, 1);
line(0, 240, 270, 240);
line(370, 240, 640, 240);
line(320, 0, 320, 190);
line(320, 290, 320, 480);
circle(260, 180, 10);
floodfill(260, 180, 15);
circle(240, 180, 10);
floodfill(240, 180, 15);
circle(220, 180, 10);
floodfill(220, 180, 15);
circle(390, 180, 10);
floodfill(390, 180, 15);
circle(390, 160, 10);
floodfill(390, 160, 15);
circle(390, 140, 10);
floodfill(390, 140, 15);
circle(380, 300, 10);
floodfill(380, 300, 15);
circle(400, 300, 10);
floodfill(400, 300, 15);
circle(420, 300, 10);
floodfill(420, 300, 15);
circle(260, 300, 10);
floodfill(260, 300, 15);
circle(260, 320, 10);
floodfill(260, 320, 15);
circle(260, 340, 10);
floodfill(260, 340, 15);
setlinestyle(0, 0, 1);
line(275, 190, 310, 190);
line(330, 290, 365, 290);
}
void changud(int c)
{
setwritemode(0);
setfillstyle(SOLID_FILL, 8);
circle(380, 300, 10);
floodfill(380, 300, 15);
circle(400, 300, 10);
floodfill(400, 300, 15);
circle(420, 300, 10);
floodfill(420, 300, 15);
circle(260, 180, 10);
floodfill(260, 180, 15);
circle(240, 180, 10);
floodfill(240, 180, 15);
circle(220, 180, 10);
floodfill(220, 180, 15);
switch (c) {
case GREEN:
setfillstyle(SOLID_FILL, GREEN);
circle(380, 300, 10);
floodfill(380, 300, 15);
circle(260, 180, 10);
floodfill(260, 180, 15);
break;
case YELLOW:
setfillstyle(SOLID_FILL, YELLOW);
circle(400, 300, 10);
floodfill(400, 300, 15);
circle(240, 180, 10);
floodfill(240, 180, 15);
break;
case RED:
setfillstyle(SOLID_FILL, RED);
circle(420, 300, 10);
floodfill(420, 300, 15);
circle(220, 180, 10);
floodfill(220, 180, 15);
break;
}
}
struct car* newcar()
{
CAR *c;
c = (CAR*)malloc(sizeof(CAR));
c->wei = 50 + random(10);
c->len = 30 +random(10);
c->x = 0;
c->y = 0;
c->color = random(15);
if(c->color==DARKGRAY)
c->color==BLACK;
c->speed = 8 + random(10);
c->next =NULL;
return c;
}
void drawcar(struct car* c,int color)
{
setfillstyle(1, color);
bar(c->x, c->y, c->x+c->len, c->y+c->wei);
}
struct car* drawutd(struct car* t,int lt)
{
CAR *p = t;
int forecar = 600;
int i = random(50);
if(t == NULL)
{
t = newcar();
}
else if(i == 1)
{
while(p->next != NULL)
p = p->next;
if(p->y > 30)
{
p->next = newcar();
}
}
while(t->y+t->speed >= 480)
{
p = t;
t = t->next;
drawcar(p, 0);
free(p);
}
for(p = t; p != NULL; p=p->next)
{
if(p->x == 0)
{
p->x = 275;
p->y = -p->wei;
drawcar(p, p->color);
forecar = p->y-10;
}
else if (p->y+p->speed+p->wei >= forecar)
{
drawcar(p, 0);
p->y = forecar - p->wei ;
drawcar(p, p->color);
forecar = p->y - 10;
}
else if (lt != GREEN && p->y+p->wei <= 185&&p->y+p->speed+p->wei >185 )
{
drawcar(p, 0);
p->y = 185 - p->wei;
drawcar(p, p->color);
forecar = p->y - 10;
}
else
{
drawcar(p, 0);
p->y += p->speed;
drawcar(p, p->color);
forecar = p->y - 10;
}
}
setlinestyle(0,0,1);
line(275, 190, 310, 190);
return t;
}
struct car* drawdtu(struct car* t,int lt)
{
CAR *p = t;
int forecar = -100;
int i = random(50);
if(t == NULL)
{
t = newcar();
t->y = 480;
}
else if(i == 1)
{
while(p->next != NULL)
p = p->next;
if(p->y < 450)
{
p->next = newcar();
p->next->y = 480;
}
}
while(t->y-t->speed+t->wei <= 0)
{
p = t;
t = t->next;
drawcar(p, 0);
free(p);
}
for(p = t; p != NULL; p=p->next)
{
if(p->x == 0)
{
p->x = 325;
drawcar(p, p->color);
forecar = p->y+p->wei+10;
}
else if (p->y-p->speed <= forecar)
{
drawcar(p, 0);
p->y = forecar ;
drawcar(p, p->color);
forecar = p->y+p->wei+10;
}
else if (lt != GREEN && p->y >= 295&&p->y-p->speed<295)
{
drawcar(p, 0);
p->y = 295;
drawcar(p, p->color);
forecar = p->y+p->wei+10;
}
else
{
drawcar(p, 0);
p->y -= p->speed;
drawcar(p, p->color);
forecar = p->y+p->wei+10;
}
}
line(330, 290, 365, 290);
return t;
}
void play()
{
FILE* fp;
int udLight, i=0, key, t, udLt;
struct car *utdHead = newcar();
struct car *dtuHead = newcar();
dtuHead->y = 480;
do{
key = bioskey(0);
}while(key != ESC && key != F1);
while(!kbhit())
{
if(i == 1)
{
udLight = RED;
changud(udLight);
}
else if(i == 151)
{
udLight = GREEN;
changud(udLight);
}
else if(i == 271)
{
udLight = YELLOW;
changud(udLight);
}
utdHead = drawutd(utdHead, udLight);
dtuHead = drawdtu(dtuHead, udLight);
delay(100);
i++;
if(i == 301) i= 1;
if(key == ESC) exit(0);
}
key = bioskey(0);
}
main()
{
int graphdrive=DETECT,graphmode;
initgraph(&graphdrive,&graphmode,"c: \\tc30\\bgi");
randomize();
setbk();
play();
closegraph();
return 0;
}
14
展开阅读全文