1、计算机实习报告学院:班级:学号:姓名: 1、题目要求:作一个两辆赛车比赛的游戏,要求可以用A,S,D,W和小键盘的上下左右键控制小汽车的运行方向进行比赛。设计方案 :所用软件为Adobe Flash CS5设置控制键,通过按键对小车进行控制,在控制的同时判断小车的位置使其行驶在跑道上,当有一辆赛车跑完三圈时,比赛结束,并输出比赛结果。开始流程图:方向键控制对两小车是否碰到跑道进行判断小车停止Y N判断比赛是否完成NY显示比赛的输赢结束设计过程:1. 首先要让赛车能够动起来。让赛车运动不是最难的一部分首先在defs的图层里打开actions窗口,设定好赛车的加速度,减速度,最大速度,以及圈数等基
2、本常量值。 Flash中使用的的是经典的直角坐标系,所以我们在计算赛车实际的速度时要把速度分解到X轴和Y轴上,得到X分量和Y分量(如下图)。计算上述分量就要知道角度,Flash中对角度和弧度要进行转化:angle_radians = angle_degrees *(PI/180)。再加上函数,就可以让我们的车动起来。这里我用了两个类似的函数来分别控制两辆赛车,他们只有控制方向的按键不同的。2还需要处理碰撞的问题。碰撞时整个赛车游戏中十分重要的一部分,因为我们必须把赛车限制在跑道内,并且让玩家可以在最快的时间内完成比赛。在车的四边分别设置一个点,用来检测它是否碰到了不可进入的区域。如果碰到了赛道
3、,那么赛车的速度将会降低,并且赛车的方向会得到纠正。3处理圈数和计时的问题。设置了两个函数来分别计算总的比赛时间setTimes和单圈最好成绩setBestLap。当赛车连续经过两次检查点checkpoint时则完成一圈,当完成三圈时游戏结束,显示游戏结果。源代码:Defs:car1.code = player;car2.code = playertotalLaps = 3;acceleration = 0.4;speedDecay = 0.96;rotationStep = 10;maxSpeed = 10;backSpeed = 1;currentCheckpoint1 = 1;curre
4、ntCheckpoint2 = 1;currentLap1 = 0;currentLap2 = 0;checkpoints = 2;currentLapTXT = 1/3;Actions:function step(who) if (_rootcar+who.code = player) if (thisspeed+who0.3) thisspeed+who *= _root.speedDecay; else thisspeed+who = 0;/赛车控制按键的设置/加速if (Key.isDown(Key.UP) & thisspeed+who0.3) _rootcar+who._rotat
5、ion -= _root.rotationStep*(thisspeed+who/_root.maxSpeed);/右转 if (Key.isDown(Key.RIGHT) & Math.abs(thisspeed+who)0.3) _rootcar+who._rotation += _root.rotationStep*(thisspeed+who/_root.maxSpeed);thisrotation+who = _rootcar+who._rotation;/计算赛车X方向和Y方向的速度分量thisspeedx+who = Math.sin(thisrotation+who*(Math
6、.PI/180)*thisspeed+who;thisspeedy+who = Math.cos(thisrotation+who*(Math.PI/180)*thisspeed+who*-1;/让这两个分量具体的作用到赛车的位置上_rootcar+who._x += thisspeedx+who;_rootcar+who._y += thisspeedy+who;/碰撞/定义四个碰撞点的位置_rootcar+who.pointLeft = x:-20, y:0;_rootcar+who.localToGlobal(_rootcar+who.pointLeft);_rootcar+who.po
7、intRight = x:20, y:0;_rootcar+who.localToGlobal(_rootcar+who.pointRight);_rootcar+who.pointFront = x:0, y:-25;_rootcar+who.localToGlobal(_rootcar+who.pointFront);_rootcar+who.pointBack = x:0, y:25;_rootcar+who.localToGlobal(_rootcar+who.pointBack);/简写上述变量thislpx+who = _rootcar+who.pointLeft.x;thislp
8、y+who = _rootcar+who.pointLeft.y;thisrpx+who = _rootcar+who.pointRight.x;thisrpy+who = _rootcar+who.pointRight.y;thisfpx+who = _rootcar+who.pointFront.x;thisfpy+who = _rootcar+who.pointFront.y;thisbpx+who = _rootcar+who.pointBack.x;thisbpy+who = _rootcar+who.pointBack.y;/检查是否发生碰撞if (_root.terrain.hi
9、tTest(thislpx+who, thislpy+who, true) _rootcar+who._rotation += 5;thisspeed+who *= 0.85;if (_root.terrain.hitTest(thisrpx+who, thisrpy+who, true) _rootcar+who._rotation -= 5;thisspeed+who *= 0.85;if (_root.terrain.hitTest(thisfpx+who, thisfpy+who, true) thisspeed+who = -1;if (_root.terrain.hitTest(t
10、hisbpx+who, thisbpy+who, true) thisspeed+who = 1;/阴影的位置 _rootshadow+who._x = _rootcar+who._x-4;_rootshadow+who._y = _rootcar+who._y+2;_rootshadow+who._rotation = _rootcar+who._rotation;/检查点if (_rootcar+who.hitTest(_rootcheckpoint+_rootcurrentCheckpoint+who) /if the current checkpoint is the start li
11、ne - increase the lap numberif (_rootcurrentCheckpoint+who = 1) if (_rootcurrentLap+who != 0) _root.setBestLap();if (_rootcurrentLap+who = _root.totalLaps) _root.gotoAndStop(finish); else _rootcurrentLap+who+;_root.currentLapTXT = _rootcurrentLap+who+/3;_rootcurrentCheckpoint+who+;if (_rootcurrentCh
12、eckpoint+who_root.checkpoints) _rootcurrentCheckpoint+who = 1;if (_rootcar+who.code = computer) function step2(who) if (_rootcar+who.code = player) if (thisspeed+who0.3) thisspeed+who *= _root.speedDecay; else thisspeed+who = 0;/赛车控制按键的设置/加速if (Key.isDown(87) & thisspeed+who0.3) _rootcar+who._rotati
13、on -= _root.rotationStep*(thisspeed+who/_root.maxSpeed);/右转 if (Key.isDown(68) & Math.abs(thisspeed+who)0.3) _rootcar+who._rotation += _root.rotationStep*(thisspeed+who/_root.maxSpeed);thisrotation+who = _rootcar+who._rotation;/计算速度的X分量和Y分量thisspeedx+who = Math.sin(thisrotation+who*(Math.PI/180)*thi
14、sspeed+who;thisspeedy+who = Math.cos(thisrotation+who*(Math.PI/180)*thisspeed+who*-1;/让这两个分量具体的作用到赛车的位置上_rootcar+who._x += thisspeedx+who;_rootcar+who._y += thisspeedy+who;/碰撞/定义四个碰撞点的位置_rootcar+who.pointLeft = x:-20, y:0;_rootcar+who.localToGlobal(_rootcar+who.pointLeft);_rootcar+who.pointRight = x
15、:20, y:0;_rootcar+who.localToGlobal(_rootcar+who.pointRight);_rootcar+who.pointFront = x:0, y:-25;_rootcar+who.localToGlobal(_rootcar+who.pointFront);_rootcar+who.pointBack = x:0, y:25;_rootcar+who.localToGlobal(_rootcar+who.pointBack);/简写上述变量thislpx+who = _rootcar+who.pointLeft.x;thislpy+who = _roo
16、tcar+who.pointLeft.y;thisrpx+who = _rootcar+who.pointRight.x;thisrpy+who = _rootcar+who.pointRight.y;thisfpx+who = _rootcar+who.pointFront.x;thisfpy+who = _rootcar+who.pointFront.y;thisbpx+who = _rootcar+who.pointBack.x;thisbpy+who = _rootcar+who.pointBack.y;/检查是否发生碰撞if (_root.terrain.hitTest(thislp
17、x+who, thislpy+who, true) _rootcar+who._rotation += 5;thisspeed+who *= 0.85;if (_root.terrain.hitTest(thisrpx+who, thisrpy+who, true) _rootcar+who._rotation -= 5;thisspeed+who *= 0.85;if (_root.terrain.hitTest(thisfpx+who, thisfpy+who, true) thisspeed+who = -1;if (_root.terrain.hitTest(thisbpx+who,
18、thisbpy+who, true) thisspeed+who = 1;/阴影的位置 _rootshadow+who._x = _rootcar+who._x-4;_rootshadow+who._y = _rootcar+who._y+2;_rootshadow+who._rotation = _rootcar+who._rotation;/检查点if (_rootcar+who.hitTest(_rootcheckpoint+_rootcurrentCheckpoint+who) /if the current checkpoint is the start line - increas
19、e the lap numberif (_rootcurrentCheckpoint+who = 1) if (_rootcurrentLap+who != 0) _root.setBestLap();if (_rootcurrentLap+who = _root.totalLaps) _root.gotoAndStop(finish); else _rootcurrentLap+who+;_root.currentLapTXT = _rootcurrentLap+who+/3;_rootcurrentCheckpoint+who+;if (_rootcurrentCheckpoint+who
20、_root.checkpoints) _rootcurrentCheckpoint+who = 1;if (_rootcar+who.code = computer) function setTimes() timeElapsed = getTimer()-_root.initialTime;milliseconds = timeElapsed;seconds = Math.floor(milliseconds/1000);minutes = Math.floor(seconds/60);minutesTXT = minutes;secondsTXT = seconds-minutes*60;
21、tensTXT = Math.round(milliseconds-seconds*1000)/10);if (minutesTXT10) minutesTXT = 0+minutesTXT;if (secondsTXT10) secondsTXT = 0+secondsTXT;if (tensTXTmilliseconds | oldMilliseconds = null) oldMilliseconds = milliseconds;seconds = Math.floor(milliseconds/1000);minutes = Math.floor(seconds/60);minute
22、sTXT = minutes;secondsTXT = seconds-minutes*60;tensTXT = Math.round(milliseconds-seconds*1000)/10);if (minutesTXT10) minutesTXT = 0+minutesTXT;if (secondsTXT10) secondsTXT = 0+secondsTXT;if (tensTXTLoadIcon(IDR_MAINFRAME);void CCalculatorDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(p
23、DX);/AFX_DATA_MAP(CCalculatorDlg)DDX_Text(pDX, IDC_EDIT1, m_EDIT);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)/AFX_MSG_MAP(CCalculatorDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON0, OnButton0)ON_BN_CLICKED(IDC_BUTTON1, OnButton1)ON_BN_CLICKED(IDC_BUTTON2
24、, OnButton2)ON_BN_CLICKED(IDC_BUTTON3, OnButton3)ON_BN_CLICKED(IDC_BUTTON4, OnButton4)ON_BN_CLICKED(IDC_BUTTON5, OnButton5)ON_BN_CLICKED(IDC_BUTTON6, OnButton6)ON_BN_CLICKED(IDC_BUTTON7, OnButton7)ON_BN_CLICKED(IDC_BUTTON8, OnButton8)ON_BN_CLICKED(IDC_BUTTON9, OnButton9)ON_BN_CLICKED(IDC_BUTTONA, On
25、Buttona)ON_BN_CLICKED(IDC_BUTTONB, OnButtonb)ON_BN_CLICKED(IDC_BUTTONC, OnButtonc)ON_BN_CLICKED(IDC_BUTTOND, OnButtond)ON_BN_CLICKED(IDC_BUTTONE, OnButtone)ON_BN_CLICKED(IDC_BUTTONF, OnButtonf)ON_BN_CLICKED(IDC_BTN_BACK, OnBtnBack)ON_BN_CLICKED(IDC_BTN_DOT, OnBtnDot)ON_BN_CLICKED(IDC_BTN_AC, OnBtnAc
26、)ON_BN_CLICKED(IDC_BTN_ADD, OnBtnAdd)ON_BN_CLICKED(IDC_BTN_DECREASE, OnBtnDecrease)ON_BN_CLICKED(IDC_BTN_MULTI, OnBtnMulti)ON_BN_CLICKED(IDC_BTN_DIV, OnBtnDiv)ON_BN_CLICKED(IDC_BTN_EQUAL, OnBtnEqual)ON_BN_CLICKED(IDC_BTN_SIGN, OnBtnSign)ON_BN_CLICKED(IDC_BTN_HEX, OnBtnHex)ON_BN_CLICKED(IDC_BTN_DEC,
27、OnBtnDec)ON_BN_CLICKED(IDC_BTN_OCT, OnBtnOct)ON_BN_CLICKED(IDC_BTN_BIN, OnBtnBin)/AFX_MSG_MAPON_EN_CHANGE(IDC_EDIT1, &CCalculatorDlg:OnEnChangeEdit1)END_MESSAGE_MAP()/ CCalculatorDlg message handlers/初始化对话框BOOL CCalculatorDlg:OnInitDialog()CDialog:OnInitDialog();/ Add About. menu item to system menu
28、./ IDM_ABOUTBOX must be in the system command range.ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);/ Set the icon for this dialog. The framework does this automatically/ when the applications main
29、window is not a dialogSetIcon(m_hIcon, TRUE);/ Set big iconSetIcon(m_hIcon, FALSE);/ Set small icon/ TODO: Add extra initialization here form=D;point=false;GetDlgItem(IDC_BUTTONA)-EnableWindow(0);GetDlgItem(IDC_BUTTONB)-EnableWindow(0);GetDlgItem(IDC_BUTTONC)-EnableWindow(0);GetDlgItem(IDC_BUTTOND)-
30、EnableWindow(0);GetDlgItem(IDC_BUTTONE)-EnableWindow(0);GetDlgItem(IDC_BUTTONF)-EnableWindow(0);return TRUE; / return TRUE unless you set the focus to a controlvoid CCalculatorDlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDialog:
31、OnSysCommand(nID, lParam);/ If you add a minimize button to your dialog, you will need the code below/ to draw the icon. For MFC applications using the document/view model,/ this is automatically done for you by the framework.void CCalculatorDlg:OnPaint() if (IsIconic()CPaintDC dc(this); / device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);/ Center icon in clie