#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
int high, width;
int position_x, position_y;
int bullet_x, bullet_y;
int enemy_x,enemy_y;
int score;
void HideCursor()
{
_CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void starup()
{
high = 18;
width = 30;
position_x = high / 2;
position_y = width / 2;
bullet_x = -1;
bullet_y = position_y;
enemy_x = 0;
enemy_y = width / 2;
HideCursor();
}
void show()
{
gotoxy(0, 0);
int i, j;
for (i = 0; i < high; i++) {
for (j = 0; j < width; j++) {
if ((i == position_x) && (j == position_y))
printf("*");
else if ((i == enemy_x) && (j == enemy_y))
printf("@");
else if ((i == bullet_x) && (j == bullet_y))
printf("|");
else
printf(" ");
}
printf("\n");
}
printf("得分 %d\n", score);
}
void UpdateWithoutInput()
{
if ((bullet_x == enemy_x) && (bullet_y == enemy_y))
{
score++;
enemy_x = 0;
enemy_y = rand() % width;
bullet_x = -1;
}
static int speed = 0;
if (speed < 10)
speed++;
if(bullet_x > -1)
bullet_x--;
if (enemy_x > high)
{
enemy_x = 0;
enemy_y = rand() % width;
}
else {
if (speed == 10) {
enemy_x++;
speed = 0;
}
}
}
void UpdateWithInput()
{
char input;
if (_kbhit())
{
input = _getch();
if (input == 'a')
position_y--;
if (input == 'd')
position_y++;
if (input == 'w')
position_x--;
if (input == 's')
position_x++;
if (input == ' ')
{
bullet_x = position_x - 1;
bullet_y = position_y;
}
}
}
int main()
{
starup();
while (1) {
show();
UpdateWithoutInput();
UpdateWithInput();
}
return 0;
}
生命周期游戏代码
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define Hight 40
#define Width 70
int cells[Hight][Width];
void HideCursor()
{
_CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void starup()
{
int i, j;
for (i = 0; i < Hight; i++)
for (j = 0; j < Width; j++)
cells[i][j] = rand() % 2;
HideCursor();
}
void show()
{
gotoxy(0, 0);
int i, j;
for (i = 0; i < Hight; i++)
{
for (j = 0; j < Width; j++)
{
if (cells[i][j] == 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
void UpdateWithoutInput()
{
int i, j;
int tempCells[Hight][Width];
for (i = 0; i < Hight; i++)
for (j = 0; j < Width; j++)
tempCells[i][j] = cells[i][j];
int NeibourNumber = 0;
for (i = 1; i < Hight - 1; i++)
{
for (j = 1; j < Width - 1; j++)
{
NeibourNumber = cells[i - 1][j - 1] + cells[i - 1][j] + cells[i - 1][j + 1]
+ cells[i][j - 1] + cells[i][j] + cells[i][j + 1]
+ cells[i + 1][j - 1] + cells[i + 1][j] + cells[i + 1][j + 1];
if (NeibourNumber == 3)
tempCells[i][j] = 1;
else if (NeibourNumber == 2 || NeibourNumber == 4)
tempCells[i][j] = cells[i][j];
else
tempCells[i][j] = 0;
}
}
for (i = 0; i < Hight; i++)
for (j = 0; j < Width; j++)
cells[i][j] = tempCells[i][j];
Sleep(150);
}
void UpdateWithInput()
{
}
int main()
{
starup();
while (1) {
show();
UpdateWithoutInput();
UpdateWithInput();
}
return 0;
}
飞机大战1.0
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define High 18
#define Width 40
#define EnemyNum 5
int canvas[High][Width] = { 0 };
int position_x, position_y;
int enemy_x[EnemyNum], enemy_y[EnemyNum];
int score;
void HideCursor()
{
_CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void starup()
{
position_x = High / 2;
position_y = Width / 2;
canvas[position_x][position_y] = 1;
int k;
for (k = 0; k < EnemyNum; k++)
{
enemy_x[k] = rand() % 3;
enemy_y[k] = rand() % Width;
canvas[enemy_x[k]][enemy_y[k]] = 3;
}
score = 0;
HideCursor();
}
void show()
{
gotoxy(0, 0);
int i, j;
for (i = 0; i < High - 1; i++) {
for (j = 0; j < Width - 1; j++) {
if (canvas[i][j] == 1)
printf("*");
else if (canvas[i][j] == 2)
printf("|");
else if (canvas[i][j] == 3)
printf("@");
else
printf(" ");
}
printf("\n");
}
printf("得分 %d\n", score);
}
void UpdateWithoutInput()
{
int i, j, k;
for (i = 0; i < High - 1; i++) {
for (j = 0; j < Width - 1; j++) {
if (canvas[i][j] == 2)
{
for (k = 0; k < EnemyNum; k++)
{
if (i == enemy_x[k] && j == enemy_y[k])
{
canvas[enemy_x[k]][enemy_y[k]] = 0;
enemy_x[k] = 0;
enemy_y[k] = rand() % Width;
canvas[enemy_x[k]][enemy_y[k]] = 3;
score++;
}
}
canvas[i][j] = 0;
if (i > 0)
canvas[i - 1][j] = 2;
}
}
}
for (k = 0; k < EnemyNum; k++)
{
if (enemy_x[k] > High)
{
canvas[enemy_x[k]][enemy_y[k]] = 0;
enemy_x[k] = 0;
enemy_y[k] = rand() % Width;
canvas[enemy_x[k]][enemy_y[k]] = 3;
score--;
}
}
static int speed = 0;
if (speed < 10)
speed++;
if (speed == 10)
{
for (k = 0; k < EnemyNum; k++)
{
canvas[enemy_x[k]][enemy_y[k]] = 0;
enemy_x[k]++;
canvas[enemy_x[k]][enemy_y[k]] = 3;
}
speed = 0;
}
}
void UpdateWithInput()
{
char input;
if (_kbhit())
{
input = _getch();
if (input == 'a') {
canvas[position_x][position_y] = 0;
position_y--;
canvas[position_x][position_y] = 1;
}
if (input == 'd') {
canvas[position_x][position_y] = 0;
position_y++;
canvas[position_x][position_y] = 1;
}
if (input == 'w') {
canvas[position_x][position_y] = 0;
position_x--;
canvas[position_x][position_y] = 1;
}
if (input == 's') {
canvas[position_x][position_y] = 0;
position_x++;
canvas[position_x][position_y] = 1;
}
if (input == ' ')
{
canvas[position_x - 1][position_y] = 2;
}
}
}
int main()
{
starup();
while (1) {
show();
UpdateWithoutInput();
UpdateWithInput();
}
return 0;
}