Selasa, 10 Maret 2015

coba c++(game ular)

masukan kode:
#include <stdlib .h="">
#include <stdio .h="">
#include <conio .h="">
/* prototypes */
void draw_line(int col, int row);
void show_score();
void add_segment();

void setup_level();
/* constants */
const int maxrow=15, maxcol=77;
const int snake_start_col=33,snake_start_row=7;
const char up_key='a', down_key='z', left_key='o', right_key='p';
const int pause_length=500000;
/* global variables */
int score, snake_length, speed, obstacles, level, firstpress, high_score=0;
char screen_grid[maxrow][maxcol];
char direction = right_key;
struct snake_segment
{
int row,col;
}
snake[100];
void main()
{
/* Variable declarations within main() only */

char keypress;
do /* restart game loop */
{
obstacles=4; level=1; score=0; speed=14;
randomize();
/* Ensure random seed initiated */
setup_level();
/* main loop */
do
{
for (int i=0;i&lt;(speed*pause_length);i++) int  j=1+i; /*pause*/
/* If key has been hit, then check it is a  direction key - if so,
change direction */
if (kbhit())
{
keypress=(char)getch();
if((keypress==right_key)|| (keypress==left_key)||

(keypress==up_key)|| (keypress==down_key))
direction = keypress;
}
/* Add a segment to the end of the snake */
add_segment();
/* Blank last segment of snake */
gotoxy(snake[0].col,snake[0].row);
cprintf(" ");
/* ... and remove it from the array */
for (int i=1;i&lt;=snake_length;i++)
snake[i-1]=snake[i];
/* Display snake in yellow */
textcolor(YELLOW);
for (int i=0;i&lt;=snake_length;i++)
{
gotoxy(snake[i].col,snake[i].row);
cprintf("O");
}
/* keeps cursor flashing in one place instead
 of following snake */
gotoxy(1,1);
/* If first press on each level, pause until a  key is pressed */
if (firstpress) { while(!kbhit()); firstpress = 0; }
/* Collision detection - walls (bad!) */
if ((snake[snake_length-1].row&gt;maxrow+1)|| (snake[snake_length-1].row&lt;=1)||
(snake[snake_length-1].col&gt;maxcol+1)|| (snake[snake_length-1].col&lt;=1)||
/* Collision detection - obstacles (bad!) */
(screen_grid[snake[snake_length-1].row- 2][snake[snake_length-1].col-2]=='x'))
keypress='x'; /* i.e. exit loop - game over */
/* Collision detection - snake (bad!) */
for (int i=0;i<snake_length-1 -="" 2="" add="" add_segment="" and="" any="" break="" certain="" check="" col-2="" col="" collision="" detection="" exit="" food="" game="" good="" i.e.="" i="" if="" increase="" keypress="x" length="" level="" loop="" more="" need="" next="" no="" obstacles="" of="" onto="" over="" reaches="" row-="" row="" score="" screen_grid="" segments="" show_score="" size="" snake="" snake_length-1="" snake_length="=(level+3)*2)" speed="" to="">1)) speed--; /*
 increase speed every 5 levels */
setup_level(); /* display next level */
}
}
} while (keypress!='x');
/* game over message */
if (score &gt; high_score) high_score = score;
show_score();
gotoxy(30,6); textcolor(LIGHTRED); cprintf("G  A M E   O V E R");
gotoxy(30,9); textcolor(YELLOW);  cprintf("Another Game (y/n)? ");
do keypress=getch(); while((keypress!='y')&amp;&amp; (keypress!='n'));
} while (keypress=='y');
}
void setup_level()
{
/* variables local to setup_level() */
int row,col;

/* Set up global variables for new level */
snake_length=level+4; direction = right_key;
firstpress = 1;
/* Fill grid with blanks */
for(row=0;row</snake_length-1></conio></stdio></stdlib>

Tidak ada komentar :

Posting Komentar