贪吃蛇小游戏,功能不多,仅供参考。

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "Unit3.h"
#include "Unit2.h"
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int shechang=40;//StrToInt(Form3->Edit3->Text);
int a=0x27,b=0,c=0,i=0;
int chang=2;
int level=1;//StrToInt(Form3->Edit2->Text);
int ti[8]={500,400,300,200,100,50,20,10};
int score=0;
String abc="";
struct weizhi
{int x;int y;} wz[100],food;

//---------------------------------------------------------------------

//----------------------------------------------------------------------
void can(int x,int y,bool z=1)
{
if(z)
Form1->Canvas->Brush->Color=clRed;
else
Form1->Canvas->Brush->Color=clYellow;
Form1->Canvas->Rectangle(x*15,y*15,(x+1)*15,(y+1)*15);
}
//--------------------------------------------------------------------------
void chu()
{
  int  x=rand()%18+1;
  int  y=rand()%38+1;
  for(int i=0;i<shechang;i++)
  if(wz[i].x!=x&&wz[i].y!=y)
 { food.x=x;food.y=y;
 }
  else
  {food.x=(x+shechang)%20;food.y=(y+shechang)%40;}
}
//------------------------------------------------------------------------
bool chi(int x,int y)
{
   return (x==food.x&&y==food.y);
}
//------------------------------------------------------------------------
void gameover()
{
Form1->Timer1->Enabled=false;
 if(Application->MessageBoxA("try again???","game over",129)==1)
 {Form1->Caption="level:1      score:0";
 Form1->Timer1->Interval=ti[0];
 chang=2;
 wz[0].x=0;
 level=1;
 score=0;
wz[0].y=39;
  Form1->Timer1->Enabled=true;
   a=0x27;i=0; chu();
 } else {Form1->Close();}
}
 //----------------------------------------------------------------------
 void biti()
 {
abc+="level:";
abc+=level;
abc+="      score:";
abc+=score;
Form1->Caption=abc;
abc="";
 }
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    randomize();chu(); if(Application->MessageBoxA("音乐否?","提示",129)==1)
  { OpenDialog1->Execute();
   MediaPlayer1->FileName=  OpenDialog1->FileName;
   MediaPlayer1->Open();
   MediaPlayer1->Play();}
   Timer1->Enabled=true;
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
    Repaint();
    can(food.x,food.y,0);
    i%=shechang;
    int temp=(i+shechang-1)%shechang;
    switch(a)
    {
       case 0x25:
           {wz[i].x=wz[temp].x-1;wz[i].y=wz[temp].y;break; }
       case 0x26:
           {wz[i].y=wz[temp].y-1;wz[i].x=wz[temp].x;break; }
       case 0x27:
           {wz[i].x=wz[temp].x+1;wz[i].y=wz[temp].y;break; }
       case 0x28:
           {wz[i].y=wz[temp].y+1;wz[i].x=wz[temp].x;break; }
       default:break;
    }
    for(int j=1;j<chang;j++)
    {
       can(wz[(i-j+shechang)%shechang].x,wz[(i-j+shechang)%shechang].y);
       if(wz[i].x==wz[(i-j+shechang)%shechang].x&&wz[i].y==wz[(i-j+shechang)%shechang].y)
           { gameover(); }
    }
    Form1->Canvas->Brush->Color=clBlue;
    Form1->Canvas->Rectangle(wz[i].x*15,wz[i].y*15,(wz[i].x+1)*15,(wz[i].y+1)*15);
    if(chi(wz[i].x,wz[i].y))
    {
       chang++;
       score+=10;
       chu();
       Beep();
       biti();
       if(chang>shechang)
       {   Timer1->Enabled=false;
           Application->MessageBoxA("继续.....","升级!",128);
           level++;
           Timer1->Interval=ti[level-1];
           chang=2;
           biti();
           wz[0].x=0;
           wz[0].y=39;
           a=0x27;
           i=0;
           chu();
           Timer1->Enabled=true;
           if(level>8)
                gameover();
       }
    }
    if(wz[i].x>19||wz[i].x<0||wz[i].y>39||wz[i].y<0)
    {
         gameover();
     }
     i++;
}
//------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{ c=a;
 switch(Key)
 {case 0x25:
 case 0x26:
 case 0x27:
 case 0x28:
 {a=Key;if(level<7) Timer1->Interval=20;break;}
 case 0x20:
 {a=c;
if(Timer1->Enabled==false)
Timer1->Enabled=true;
else
Timer1->Enabled=false;break;}
default:{a=c;break;}
 }
}
//---------------------------------------------------------------------------


void __fastcall TForm1::N1Click(TObject *Sender)
{
Timer1->Enabled=false;
Form1->Enabled=false;
Form2->Show();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::N2Click(TObject *Sender)
{
/*Timer1->Enabled=false;
Form1->Enabled=false;
Form3->Show();   */
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
Timer1->Interval=ti[level-1];
}
//---------------------------------------------------------------------------

void __fastcall TForm1::MediaPlayer1Notify(TObject *Sender)
{
if   (MediaPlayer1->Position==MediaPlayer1->Length  
                  &&   MediaPlayer1->NotifyValue==nvSuccessful)  
          {  
                  MediaPlayer1->Notify   =   true;  
                  MediaPlayer1->Position   =   0;  
                  MediaPlayer1->Play();  
          }  


}
//---------------------------------------------------------------------------

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的贪吃蛇小游戏的 C 语言代码,仅供参考: ```c #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #include <time.h> #define WIDTH 60 #define HEIGHT 20 int x, y; // 贪吃蛇头部的坐标 int fruit_x, fruit_y; // 水果的坐标 int tail_x[100], tail_y[100]; // 贪吃蛇身体的坐标 int nTail; // 贪吃蛇身体的长度 enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; // 贪吃蛇的移动方向 enum eDirection dir; int score; // 得分 void Setup() { // 初始化游戏 dir = STOP; x = WIDTH / 2; y = HEIGHT / 2; srand(time(NULL)); fruit_x = rand() % (WIDTH - 2) + 1; fruit_y = rand() % (HEIGHT - 2) + 1; nTail = 0; score = 0; } void Draw() { // 绘制游戏界面 system("cls"); for (int i = 0; i < WIDTH + 2; i++) printf("#"); printf("\n"); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { if (j == 0) printf("#"); if (i == y && j == x) printf("O"); else if (i == fruit_y && j == fruit_x) printf("F"); else { int bDraw = 0; for (int k = 0; k < nTail; k++) { if (tail_x[k] == j && tail_y[k] == i) { printf("o"); bDraw = 1; } } if (!bDraw) printf(" "); } if (j == WIDTH - 1) printf("#"); } printf("\n"); } for (int i = 0; i < WIDTH + 2; i++) printf("#"); printf("\n"); printf("Score: %d\n", score); } void Input() { // 处理用户输入 if (_kbhit()) { switch (_getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': dir = STOP; break; case 'q': exit(0); break; } } } void Logic() { // 处理游戏逻辑 int prev_tail_x = tail_x[0]; int prev_tail_y = tail_y[0]; int prev2_tail_x, prev2_tail_y; tail_x[0] = x; tail_y[0] = y; for (int i = 1; i < nTail; i++) { prev2_tail_x = tail_x[i]; prev2_tail_y = tail_y[i]; tail_x[i] = prev_tail_x; tail_y[i] = prev_tail_y; prev_tail_x = prev2_tail_x; prev_tail_y = prev2_tail_y; } switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; } if (x == 0 || x == WIDTH - 1 || y == 0 || y == HEIGHT - 1) // 碰到边缘 { printf("\a"); // 发出警告声 Sleep(1000); Setup(); } for (int i = 0; i < nTail; i++) // 碰到自己的身体 { if (tail_x[i] == x && tail_y[i] == y) { printf("\a"); // 发出警告声 Sleep(1000); Setup(); } } if (x == fruit_x && y == fruit_y) // 吃到水果 { score += 10; nTail++; srand(time(NULL)); fruit_x = rand() % (WIDTH - 2) + 1; fruit_y = rand() % (HEIGHT - 2) + 1; } } int main() { Setup(); while (true) { Draw(); Input(); Logic(); Sleep(50); } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值