C Language Pinball Games

Source: Internet
Author: User

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include <mmsystem.h>
#pragma comment (lib, "WINMM. LIB ")

#define BRICK_NUM 100
Shape type
#define SQUARE 1
#define STAR 2
#define DIAMOND 3

/*
Establish the corresponding model: 20/80 Program: Core code 20% non-core code 80%
1. Bricks: Description Bricks
2. Ball
3, Bar Wood
*/
typedef struct POINT
{
int x;
int y;
} point;
Ball Data Model
typedef struct BALL
{
Coordinate
Point Point;
Shape, common fireball Laser
int shape;
Horizontal direction r:1 l:-1: ' l ' r '
int hdir;
Vertical direction U:1 D:-1
int vdir;
Speed
int speed;
} ball;

Plank
typedef struct BOARD
{
Coordinate
Point Point;
Length
int length;
Direction left (-1) Right (1)
int dir;
Speed
int speed;
} Board;

Build types, Touch bricks
typedef struct BRICK
{
Coordinate
Point Point;
Shape: 1: Square (plain brick 10) 2 (Round 20) 3:
int shape;
} Brick;


Bricks
Brick Bricks[brick_num];

Build a Ball
Ball ball;
Build a plank
Board Board;

Counter
int count=0;
Total Score
int totalscore=0;

void Init ()
{
Srand (Time (0));
Initialize Ball
ball.point.x=36;
ball.point.y=22;
ball.shape=1;
ball.speed=200;
ball.vdir=1;//up u down D left L right R
Ball.hdir=-1;
Initializing planks
board.point.x=32;
board.point.y=23;
board.length=5;
board.speed=300;
Board.dir=-1;
Initializing Bricks
Initbrick ();
}

/*
Function: Positioning cursor (output) position
*/
void SetPosition (Point point)
{
User coordinate information is converted into internal coordinate information
COORD coord= {POINT.X,POINT.Y};
SetConsoleCursorPosition (GetStdHandle (std_output_handle), coord);
}

/*
Visual display of planks, balls
*/
void Showball ()
{
SetPosition (Ball.point);
Color: R g B
Setconsoletextattribute (GetStdHandle (std_output_handle), Foreground_green | foreground_red | foreground_intensity);
printf ("");
}

/*
* Visual display of wood (reconstruction)--"evolution--"
*/
void Showborad ()
{
int index=0;
SetPosition (Board.point);
Setconsoletextattribute (GetStdHandle (std_output_handle), foreground_red | foreground_intensity);
for (index=0; index<board.length; index++)
{
printf ("the");
}
}

/*
Random numbers
*/
int Createran ()
{
return rand ()%3+1;//0 12
}

/*
Initializing Brick Information
*/
void Initbrick ()
{
Determine the first brick position x, y program logic will cause modifications
int x=10;
int y=2;
int i=0;
Save initial coordinates
int index=0;
100=25*4
for (index=0; index<brick_num; index++)
{
Determine the starting coordinates of each line
if (index!=0 && index%25==0)//25 50 75
{
Line break
X=bricks[i*25].point.x;
y=bricks[i*25].point.y+1;
i++;
}
Specific
Bricks[index].point.x=x;
Bricks[index].point.y=y;
Shape
Bricks[index].shape=createran ();
x=x+2;
}
}

/*
* Graphic Bricks
*/
void Showbrick ()
{
int index=0;
for (index=0; index<brick_num; index++)
{
SetPosition (Bricks[index].point);
Switch (bricks[index].shape)
{
Case SQUARE:
Setconsoletextattribute (GetStdHandle (std_output_handle), foreground_red | foreground_intensity);
printf ("");
Break
Case STAR:
Setconsoletextattribute (GetStdHandle (Std_output_handle),
Foreground_green | foreground_intensity);

printf ("★");
Break
Case DIAMOND:
Setconsoletextattribute (GetStdHandle (std_output_handle), Foreground_blue | foreground_intensity);
printf ("");
Break
}
}
}


/*
Check if the ball hits the plank.
1 touches
0 didn't touch
*/
int Balltoboard ()
{
Start x value of the board
int bxs=board.point.x;
int bxe=board.point.x+board.length*2;
if ((Ball.point.x>=bxs && ball.point.x<=bxe) && ball.point.y==board.point.y-1)
{
return 1;
}
return 0;
}


void Seldir () {
Function: Asynchronous
if (Getasynckeystate (Vk_left)) {
Board.dir=-1;
}else if (getasynckeystate (vk_right)) {
Board.dir=1;
}
}

/*
Moving Planks
*/
void Moveboard ()
{
int index=0;
SetPosition (Board.point);
Clear
for (index=0; index<board.length; index++)
{
printf ("");
}
New coordinates:
Board.point.x+=2*board.dir;
Re -
SetPosition (Board.point);
Setconsoletextattribute (GetStdHandle (std_output_handle), foreground_red | foreground_intensity);
for (index=0; index<board.length; index++)
{
printf ("the");
}
}

/*
Check if the ball touches bricks.
0
1
2
3
*/
int Balltobrick () {
int index=0;
for (index=0;index<brick_num;index++) {
if (bricks[index].point.x==ball.point.x && bricks[index].point.y==ball.point.y) {
return bricks[index].shape;;
}
}
return 0;
}

/*
Moving the ball
*/
void Moveball ()
{
int left=4;
int right=64;
int top=2;
int bottom=24;
Horizontal, vertical increment
int stepx=2;
int Stepy=1;
int shape=0;
Point p={76,10};
Get the ball to its original position
Point Oldpoint=ball.point;
Srand (Time (0));
Original position Graphics Clear
SetPosition (Oldpoint);
printf ("");

Check the horizontal direction
if (ball.point.x<left)
{
Change the horizontal direction
Ball.hdir=1;
}
else if (ball.point.x>right)
{
Ball.hdir=-1;
}

Check the vertical direction
if (ball.point.y<top)
{
Ball.vdir=-1;
}
else if (ball.point.y>bottom)
{
Ball.vdir=1;
}
else if (Balltoboard () ==1)
{
Ball.vdir=1;
}

Determine the new position of the ball
Ball.point.x=oldpoint.x+stepx*ball.hdir;
Ball.point.y=oldpoint.y-stepy*ball.vdir;

Judging if it touches bricks 0 1 2 3
Shape=balltobrick ();
Calculate how many bricks you touched.
count+=shape>0?1:0;

SetPosition (P);
totalscore+=shape*10;
printf ("Ball hit%d bricks, score:%d", count,totalscore);
Redraw
SetPosition (Ball.point);
Setconsoletextattribute (GetStdHandle (std_output_handle), Foreground_green | foreground_red | foreground_intensity);
printf ("");
}

/*
Function module: Draw fence 1b=8bit a:1b Chinese: 2B
*/
void Paintwall ()
{
int index=0;
Point Point;
Setconsoletextattribute (GetStdHandle (Std_output_handle), 11);
Top. Below
for (index=0; index<70; index=index+2)
{
Point.x=index;
point.y=0;
SetPosition (point);
printf ("");
point.y=26;
SetPosition (point);
printf ("");
}
Left, right akes[0].x=oldx+2;
for (index=1; index<=26; index++)
{
point.x=0;
Point.y=index;
SetPosition (point);
printf ("");
point.x=68;
SetPosition (point);
printf ("");
}
}

void Show ()
{
Paintwall ();
Showbrick ();
Showball ();
Showborad ();
}

int main ()
{

Init ();
Show ();
while (1)
{
Moveball ();
Seldir ();
Moveboard ();
Speed
Sleep (Ball.speed);
}
return 0;
}

C Language Pinball Games

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.