📄 c15_2f.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
#include "C15_2f.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
NewClick(NULL);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormResize(TObject *Sender)
{
Width = 315+28;
Height = 315+65+StatusBar1->Height;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
int i;
for(i=0;i<16;i++)
{
Canvas->MoveTo(10, 10+i*21);
Canvas->LineTo(10+21*15, 10+i*21);
Canvas->MoveTo(10+i*21, 10);
Canvas->LineTo(10+i*21, 10+21*15);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
int TopNum = (ShipA->Top - 11) / 21;
int LeftNum = (ShipA->Left -11) /21;
if(TopNum *21 == ShipA->Top - 11 && LeftNum *21 == ShipA->Left - 11)
{ // 判断我舰转弯
CurrentDir = NewDir;
switch(CurrentDir){
case KeyLeft:
ShipA->Picture = Imagea1->Picture;
break;
case KeyBottom:
ShipA->Picture = Imagea2->Picture;
break;
case KeyRight:
ShipA->Picture = Imagea3->Picture;
break;
case KeyTop:
ShipA->Picture = Imagea4->Picture;
break;
}
}
switch(CurrentDir){
// 移动我舰
case KeyLeft:
if(ShipA->Left <= 10+21*14)
ShipA->Left += 1;
break;
case KeyTop:
if(ShipA->Top > 11)
ShipA->Top -=1;
break;
case KeyBottom:
if(ShipA->Top <= 10+21*14)
ShipA->Top +=1;
break;
case KeyRight:
if(ShipA->Left > 11)
ShipA->Left -=1;
break;
}
switch(TpDir){
// 移动我鱼雷
case KeyLeft:
if(ShipAtp->Left <= 10+21*14)
ShipAtp->Left += 2;
else
ShipAtp->Visible = false;
break;
case KeyTop:
if(ShipAtp->Top > 11)
ShipAtp->Top -=2;
else
ShipAtp->Visible = false;
break;
case KeyBottom:
if(ShipAtp->Top <= 10+21*14)
ShipAtp->Top +=2;
else
ShipAtp->Visible = false;
break;
case KeyRight:
if(ShipAtp->Left > 11)
ShipAtp->Left -=2;
else
ShipAtp->Visible = false;
break;
}
TopNum = (ShipB->Top - 11) / 21;
LeftNum = (ShipB->Left -11) /21;
if(TopNum *21 == ShipB->Top - 11 && LeftNum *21 == ShipB->Left - 11)
{ // 判断敌舰转弯
if(CurrentDir == KeyLeft || CurrentDir == KeyRight)
{
if(ShipA->Top > ShipB->Top)
ShipDir = KeyBottom;
else if(ShipA->Top < ShipB->Top)
ShipDir = KeyTop;
else
if(ShipA->Left > ShipB->Left)
ShipDir = KeyLeft;
else
ShipDir = KeyRight;
}
else
{
if(ShipA->Left > ShipB->Left)
ShipDir = KeyLeft;
else if(ShipA->Left < ShipB->Left)
ShipDir = KeyRight;
else
if(ShipA->Top > ShipB->Top)
ShipDir = KeyBottom;
else
ShipDir = KeyTop;
}
switch(ShipDir){
case KeyLeft:
ShipB->Picture = Imageb1->Picture;
break;
case KeyBottom:
ShipB->Picture = Imageb2->Picture;
break;
case KeyRight:
ShipB->Picture = Imageb3->Picture;
break;
case KeyTop:
ShipB->Picture = Imageb4->Picture;
break;
}
}
switch(ShipDir){
// 移动敌舰
case KeyLeft:
if(ShipB->Left <= 10+21*14)
ShipB->Left += 1;
break;
case KeyTop:
if(ShipB->Top > 11)
ShipB->Top -=1;
break;
case KeyBottom:
if(ShipB->Top <= 10+21*14)
ShipB->Top +=1;
break;
case KeyRight:
if(ShipB->Left > 11)
ShipB->Left -=1;
break;
}
if(!ShipBtp->Visible)
{ // 判断发射导弹
switch(ShipDir)
{
case KeyLeft:
if(ShipB->Top == ShipA->Top && ShipB->Left < ShipA->Left
&& (CurrentDir == KeyLeft || CurrentDir == KeyRight) ||
ShipB->Top == ShipAtp->Top && ShipB->Left < ShipAtp->Left
&& TpDir == KeyRight && ShipAtp->Visible)
{
ShipTpDir = ShipDir;
ShipBtp->Top = ShipB->Top;
ShipBtp->Left = ShipB->Left;
ShipBtp->Picture = Image1->Picture;
ShipBtp->Visible = true;
}
break;
case KeyRight:
if(ShipB->Top == ShipA->Top && ShipB->Left > ShipA->Left
&& (CurrentDir == KeyLeft || CurrentDir == KeyRight) ||
ShipB->Top == ShipAtp->Top && ShipB->Left > ShipAtp->Left
&& TpDir == KeyLeft && ShipAtp->Visible)
{
ShipTpDir = ShipDir;
ShipBtp->Top = ShipB->Top;
ShipBtp->Left = ShipB->Left;
ShipBtp->Picture = Image1->Picture;
ShipBtp->Visible = true;
}
break;
case KeyTop:
if(ShipB->Left == ShipA->Left && ShipB->Top > ShipA->Top
&& (CurrentDir == KeyTop || CurrentDir == KeyBottom) ||
ShipB->Left == ShipAtp->Left && ShipB->Top > ShipAtp->Top
&& TpDir == KeyBottom && ShipAtp->Visible)
{
ShipTpDir = ShipDir;
ShipBtp->Top = ShipB->Top;
ShipBtp->Left = ShipB->Left;
ShipBtp->Picture = Image2->Picture;
ShipBtp->Visible = true;
}
break;
case KeyBottom:
if(ShipB->Left == ShipA->Left && ShipB->Top < ShipA->Top
&& (CurrentDir == KeyTop || CurrentDir == KeyBottom) ||
ShipB->Left == ShipAtp->Left && ShipB->Top < ShipAtp->Top
&& TpDir == KeyTop && ShipAtp->Visible)
{
ShipTpDir = ShipDir;
ShipBtp->Top = ShipB->Top;
ShipBtp->Left = ShipB->Left;
ShipBtp->Picture = Image2->Picture;
ShipBtp->Visible = true;
}
break;
}
}
switch(ShipTpDir)
{ // 移动敌鱼雷
case KeyLeft:
if(ShipBtp->Left <= 10+21*14)
ShipBtp->Left += 2;
else
ShipBtp->Visible = false;
break;
case KeyTop:
if(ShipBtp->Top > 11)
ShipBtp->Top -=2;
else
ShipBtp->Visible = false;
break;
case KeyBottom:
if(ShipBtp->Top <= 10+21*14)
ShipBtp->Top +=2;
else
ShipBtp->Visible = false;
break;
case KeyRight:
if(ShipBtp->Left > 11)
ShipBtp->Left -=2;
else
ShipBtp->Visible = false;
break;
}
// 以下判断输赢
UDRect IntRect, RectA, RectB, RectAtp, RectBtp;
//
RectA.Left = ShipA->Left;
RectA.Top = ShipA->Top;
RectA.Right = RectA.Left + ShipA->Width;
RectA.Bottom = RectA.Top + ShipA->Height;
RectB.Left = ShipB->Left;
RectB.Top = ShipB->Top;
RectB.Right = RectB.Left + ShipB->Width;
RectB.Bottom = RectB.Top + ShipB->Height;
RectAtp.Left = ShipAtp->Left;
RectAtp.Top = ShipAtp->Top;
RectAtp.Right = RectAtp.Left + ShipAtp->Width;
RectAtp.Bottom = RectAtp.Top + ShipAtp->Height;
RectBtp.Left = ShipBtp->Left;
RectBtp.Top = ShipBtp->Top;
RectBtp.Right = RectBtp.Left + ShipBtp->Width;
RectBtp.Bottom = RectBtp.Top + ShipBtp->Height;
if(IntersectRect((RECT*)&IntRect, (RECT*)&RectA, (RECT*)&RectBtp)
&& ShipBtp->Visible)
{ // 敌鱼雷命中我舰
Timer1->Enabled = false;
ShipBtp->Visible = false;
ShipB->Visible = false;
ShipAtp->Visible = false;
ShipA->Visible = false;
StatusBar1->Panels->Items[2]->Text = "游戏结束,你输了!";
}
if(IntersectRect((RECT*)&IntRect, (RECT*)&RectBtp, (RECT*)&RectAtp)
&& ShipAtp->Visible && ShipBtp->Visible)
{ // 鱼雷相撞
ShipAtp->Visible = false;
ShipBtp->Visible = false;
}
if(IntersectRect((RECT*)&IntRect, (RECT*)&RectB, (RECT*)&RectAtp)
&& ShipAtp->Visible)
{ // 我鱼雷命中敌舰
ShipAtp->Visible = false;
ShipB->Top = 11 + 14 * 21;
ShipB->Left = 11 + 14 * 21;
ShipDir = KeyTop;
AddOne(); // 记录加1
}
if(IntersectRect((RECT*)&IntRect, (RECT*)&RectA, (RECT*)&RectB))
{ // 两舰相撞
Timer1->Enabled = false;
ShipBtp->Visible = false;
ShipAtp->Visible = false;
ShipA->Visible = false;
ShipB->Visible = false;
AddOne();
StatusBar1->Panels->Items[2]->Text = "游戏结束,你输了!";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if(Key == KeyLeft || Key == KeyTop || Key == KeyBottom || Key == KeyRight)
NewDir = Key;
if(Key == KeySpace && !ShipAtp->Visible)
{ // 发射鱼雷
if(CurrentDir == KeyLeft || CurrentDir == KeyRight)
ShipAtp->Picture = Image3->Picture;
else
ShipAtp->Picture = Image4->Picture;
ShipAtp->Top = ShipA->Top;
ShipAtp->Left = ShipA->Left;
ShipAtp->Visible = true;
TpDir = CurrentDir;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NewClick(TObject *Sender)
{
ShipA->Picture = Imagea1->Picture;
CurrentDir = NewDir = KeyLeft;
ShipAtp->Visible = false;
ShipA->Top = 11;
ShipA->Left = 11;
ShipA->Visible = true;
ShipB->Picture = Imageb4->Picture;
ShipDir = KeyTop;
ShipB->Top = 11 + 21 * 14;
ShipB->Left = 11 + 21 * 14;
ShipB->Visible = true;
Timer1->Enabled = true;
StatusBar1->Panels->Items[1]->Text = " 0";
StatusBar1->Panels->Items[2]->Text = "用方向键控制方向,空格键发射鱼雷。";
}
//---------------------------------------------------------------------------
void TForm1::AddOne()
{
int i = atoi(StatusBar1->Panels->Items[1]->Text.c_str());
char buf[20];
sprintf(buf, " %d", ++i);
StatusBar1->Panels->Items[1]->Text = buf;
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -