A c language game made by myself-eating gold

Source: Internet
Author: User

In this week's Data Structure Course Design, brother picked a simple question and wrote a graphic gold-eating game in C language.

The basic idea is:

Defines an array that maps data to the screen coordinates.

Then, use some drawing functions to draw the gold taojiner (red circle) and gold (yellow circle ).

The upper, lower, left, and right keys on the keyboard are used to control the upper, lower, and left movements of gold miners.

The game interface will be refreshed every six seconds, and the gold will change its position.

If you are a master and get 20 in a minute, win. If you hit the wall, game over.

This game is incredibly frustrating and belongs to version 1. If you are interested, you can play well. Of course, there is no beauul ul made by flash. Haha.

Below are the source code and some comments:

/*
Version: V1.0
Author: xufeiyang
Datetime: 2010.12.13

IDE: Turbo C 2.0
*/
/************************************/
# Include "graphics. H"/* header file */
# Include "time. H"
# Include "stdlib. H"
# Include "bios. H"
# Include "Dos. H"
# Include "stdio. H"
# Define ESC 0x11b/* Keyboard Scan Code */
# Define up 0x4800
# Define down 0x5000
# Define left 0x4b00
# Define F1 0x3b00
# Define right 0x4d00
# Define Yes 0x1579
# Define no 0x0000e
# Define restart 0x1372
/*************************************** */
Time_t start, end;
Int diff;
Int oldtime;
Int goldnum = 0;
Int bord [20] [20] =
{
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
};
Typedef struct {/* Hunger */
Int cirx;
Int ciry;
Int cirradius;
} Hunger, gold;

Hunger hunger;
Gold gold;

Int oldgoldx;
Int oldgoldy;

Void initbord ();
Void drawhunger ();
Void updatehunger ();
Void drawgold ();
Void updategold ();
Void randombean ();
Void printsum ();
Void delsum ();
Int justify ();
Void movedown ();
Void moveup ();
Void moveright ();
Void moveleft ();
Void wingame ();
Void losegame ();

Int main ()
{
Int X, Y;
Int key = 0;
Int result;
Time_t st;

Hunger. cirradius = 9;/* initialize the radius of gold buyers and gold */
Gold. cirradius = 5;

Initbord ();

While (1)
{
End = Time (null );
While (! Kbhit () |! (Key = bioskey (0 )))
{
End = Time (null );
Diff = difftime (end, start );
If (diff % 6 = 0)
{
Updategold ();
Randombean ();
Break;
}
}
If (key)
{
Switch (key)
{
Case down:
Movedown ();
Break;
Case up:
Moveup ();
Break;
Case left:
Moveleft ();
Break;
Case right:
Moveright ();
Break;
Case ESC:
Sleep (2 );
Closegraph ();
Exit (1 );
Case restart:
Closegraph ();
Initbord ();
Break;
}/* Switch */
Delsum ();
Printsum ();
Key = 0;
}/* If */
/* If (59-diff) % 4 = 0)
{
Updategold ();
Randombean ();
}
*/
Result = justify ();
If (result = 1)
{
Wingame ();
Break;
}
Else if (result = 0)
{
Losegame ();
Break;
}
Else
Continue;
}
Sleep (2 );
Closegraph ();
Return 0;
}
/*************************************** ******/
Void initbord ()
{
Int GD = VGA, errorcode;
Int GM = vgahi;
Int X, Y;
Srand (unsigned INT) Time (null ));
Initgraph (& GD, & GM ,"");
Errorcode = graphresult ();
If (errorcode <0)
{
Printf ("graphics error: % s/n", grapherrormsg (errorcode ));
Printf ("press any key to halt :");
Exit (1 );
}
Cleardevice ();
Start = Time (null );

Do
{
X = 1 + rand () % 18;
Y = 1 + rand () % 18;
} While (Bord [x] [Y] = 2 );
Setbkcolor (9 );
Hunger. cirx = X;
Hunger. ciry = y;
Drawhunger ();
Printsum ();
Randombean ();
Setcolor (blue );
MoveTo (39, 39 );
Lineto (39,439 );
Linerel (400, 0 );
Linerel (0,-400 );
Linerel (-400, 0 );
MoveTo (59, 59 );
Lineto (59,419 );
Linerel (360, 0 );
Linerel (0,-360 );
Linerel (-360, 0 );

MoveTo (449, 39 );
Lineto (589, 39 );
Linerel (0,400 );
Linerel (-140, 0 );
Linerel (0,-400 );
}
Void wingame ()
{
Setcolor (red );
Setfillstyle (solid_fill, red );
Outtextxy (getmaxx ()/2 + 20, getmaxy ()/2, "Y o u w I n! ");
}
Void losegame ()
{
Setcolor (red );
Setfillstyle (solid_fill, red );
Outtextxy (getmaxx ()/2 + 20, getmaxy ()/2, "Game over! ");
}
Int justify ()
{
If (hunger. ciry> = 19 | hunger. ciry <= 0
| Hunger. cirx> = 19 | hunger. cirx <= 0
| (Diff> 60 & goldnum <30 ))
{
Cleardevice ();
Return 0;/* target audience */
}
Else if (diff <= 60 & goldnum> = 30)
{
Cleardevice ();
Return 1;
}
Else
Return 2;
}
Void drawhunger ()
{
Bord [hunger. cirx] [hunger. ciry] = 3;
Setcolor (red );
Circle (69 + (hunger. cirx-1) * 20, 69 + (hunger. ciry-1) * 20, hunger. cirradius );
Setfillstyle (solid_fill, red );
Floodfill (69 + (hunger. cirx-1) * 20-1, 69 + (hunger. ciry-1) * 20-1, red );
}
Void updatehunger ()
{
Bord [hunger. cirx] [hunger. ciry] = 1;
Setcolor (0 );
Circle (69 + (hunger. cirx-1) * 20, 69 + (hunger. ciry-1) * 20, hunger. cirradius );
Setfillstyle (solid_fill, 0 );
Floodfill (69 + (hunger. cirx-1) * 20-1, 69 + (hunger. ciry-1) * 20 );
}
Void randombean ()
{
Int X, Y;
Do
{
X = 1 + rand () % 18;
Y = 1 + rand () % 18;
} While (Bord [x] [Y] = 3 );
Gold. cirx = X;
Gold. ciry = y;
Drawgold ();
}
Void drawgold ()
{
Oldgoldx = gold. cirx;
Oldgoldy = gold. ciry;
Bord [gold. cirx] [gold. ciry] = 0;
Setcolor (yellow );
Circle (69 + (gold. cirx-1) * 20, 69 + (gold. ciry-1) * 20, Gold. cirradius );
Setfillstyle (solid_fill, yellow );
Floodfill (69 + (gold. cirx-1) * 20-2, 69 + (gold. ciry-1) * 20-2, yellow );
}
Void updategold ()
{
Bord [gold. cirx] [gold. ciry] = 1;
Setcolor (0 );
Circle (69 + (gold. cirx-1) * 20, 69 + (gold. ciry-1) * 20, Gold. cirradius );
Setfillstyle (solid_fill, 0 );
Floodfill (69 + (gold. cirx-1) * 20-2, 69 + (gold. ciry-1) * 20-2, 0 );
}
Void printsum ()
{
Char C [10], a [10];
Memset (C, 0x00, sizeof (c ));
Memset (A, 0x00, sizeof ());
Oldtime = 60-diff;
Sprintf (C, "% d", goldnum );
Sprintf (a, "% d", 60-diff );
/* ITOA (goldnum, C, sizeof (c ));
ITOA (60-diff, A, sizeof ());
*/
Setcolor (11 );
Outtextxy (480,120, "60 s eat 30 ");
Outtextxy (453,140, "> eat bean game <");
Outtextxy( 510,160, C );
Outtextxy (480,200, "second left ");
Setcolor (4 );
Outtextxy( 500,220, );
}
Void delsum ()
{
Char C [10], a [10];
Memset (C, 0x00, sizeof (c ));
Memset (A, 0x00, sizeof ());
Sprintf (C, "% d", goldnum-1 );
Sprintf (a, "% d", oldtime );

Setcolor (0 );
Outtextxy( 510,160, C );

Setcolor (0 );
Outtextxy( 500,220, );
}
Void movedown ()
{
Updatehunger ();
Hunger. ciry ++;
If (Bord [hunger. cirx] [hunger. ciry] = 0)
{
Goldnum ++;
Updategold ();
Randombean ();
}
Drawhunger ();
}
Void moveup ()
{
Updatehunger ();
Hunger. ciry --;
If (Bord [hunger. cirx] [hunger. ciry] = 0)
{
Goldnum ++;
Updategold ();
Randombean ();
}
Drawhunger ();
}
Void moveright ()
{
Updatehunger ();
Hunger. cirx ++;
If (Bord [hunger. cirx] [hunger. ciry] = 0)
{
Goldnum ++;
Updategold ();
Randombean ();
}
Drawhunger ();
}
Void moveleft ()
{
Updatehunger ();
Hunger. cirx --;
If (Bord [hunger. cirx] [hunger. ciry] = 0)
{
Goldnum ++;
Updategold ();
Randombean ();
}
Drawhunger ();
}

I defined it as a global function without passing parameters. It is estimated that the siblings will be a little dizzy after reading it. Well, you must think about optimization before you do it next time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.