6.4 KiB
6.4 KiB
贪吃蛇
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#include <process.h>
#define WIDTH 50 //宽度
#define HEIGHT 30 //高度
#define SNAKE_LENGHT 100 //蛇的最大长度
#define UP 1
#define DOWN 2
#define LEFT 3
#define RGIHT 4
char map[HEIGHT][WIDTH];//地图字符数组
int sankeX[SNAKE_LENGHT], sankeY[SNAKE_LENGHT];//蛇的坐标
int foodX, foodY;//食物的坐标
int size = 1;//当前蛇的长度
int gameStatus = 1;
int direction = RGIHT;
/*
_kbhit()用来判断输入的是否是空格
_getch()用来判断键盘输入的内容
*/
void draw(void* param){
int i, j;
char buffer[(HEIGHT+1)*(WIDTH+1)];
int k;
while(gameStatus){
system("cls");
k=0;
for(i=0;i<HEIGHT;i++){
for(j=0;j<WIDTH;j++){
buffer[k++] = map[i][j];
}
buffer[k++] = '\n';
}
buffer[k] = '\0';
printf("%s\n", buffer);
Sleep(200);
}
}
//打印地图
void changeMap(void* param){
int i, j, k;
int isFill;
while(gameStatus){
for(i=0;i<HEIGHT;i++){
for(j=0;j<WIDTH;j++){
if(i==0 || i == HEIGHT-1 || j==0 || j == WIDTH - 1){
map[i][j] = '#';
}else if(i == foodY && j == foodX){
map[i][j] = '$';
}else if(i == sankeY[0] && j == sankeX[0]){
map[i][j] = '0';
}else{
isFill = 0;
for(k=1;k<size;k++){
if(i == sankeY[k] && j == sankeX[k]){
map[i][j] = 'o';
isFill = 1;
}
}
if(!isFill){
map[i][j] = ' ';
}
}
}
}
}
}
void changeSnake(void* param){
//蛇的移动
while(gameStatus){
int sankeXpre = sankeX[0], sankeYpre=sankeY[0];
int sankeXpre2, sankeYpre2;
int i;
Sleep(400-size*2);
sankeX[0] += (direction == RGIHT)?1:(direction == LEFT)?-1:0;
sankeY[0] += (direction == DOWN)?1:(direction == UP)?-1:0;
for(i=1;i<size;i++){
sankeXpre2 = sankeX[i];
sankeYpre2 = sankeY[i];
sankeX[i] = sankeXpre;
sankeY[i] = sankeYpre;
sankeXpre = sankeXpre2;
sankeYpre = sankeYpre2;
}
//判断蛇是否吃到食物
if(sankeX[0] == foodX && sankeY[0] == foodY){
size++;
foodX = rand()%(WIDTH-5)+3;
foodY = rand()%(HEIGHT-5)+3;
}
//边界检测、以及是否撞到自己
for(i=1;i<size;i++){
if(sankeX[0] == sankeX[i] && sankeY[0] == sankeY[i]){
gameStatus = 0;
}
}
if(sankeX[0] == 0 || sankeX[0] == WIDTH || sankeY[0]==0 || sankeY[0] == HEIGHT){
gameStatus = 0;
}
}
}
void input(void* param){
char key;
while(gameStatus){
if(_kbhit()){
key = _getch();
switch(key){
case 'w':direction=UP;break;
case 's':direction=DOWN;break;
case 'a':direction=LEFT;break;
case 'd':direction=RGIHT;break;
case 'q':gameStatus=0;break;
}
}
}
}
int main() {
system("color 02");
srand((unsigned)time(NULL));
sankeX[0] = WIDTH/2;
sankeY[0] = HEIGHT/2;
foodX = rand()%(WIDTH-5)+3;
foodY = rand()%(HEIGHT-5)+3;
_beginthread(changeMap, 0, NULL);
_beginthread(draw, 0, NULL);
_beginthread(input, 0, NULL);
_beginthread(changeSnake, 0, NULL);
while(gameStatus){
}
printf("游戏结束您的得分是%d\n", size-1);
return 0;
}


飞机大战
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <process.h>
#include <Windows.h>
#define HEIGHT 40
#define WIDTH 30
char map[HEIGHT][WIDTH];
int score = 0;
int gameStatus = 1;
int planeX = 12;
int planeY = 36;
#define bulletNum 1000
int bulletX[bulletNum];
int bulletY[bulletNum];
int bulletRear = 0;//最后的子弹
int bulletFront = 0;//最前面的子弹
char* plane[6] = {" * ", "*****", " * * "};
void changeMap(void* param){
int i, j;
while(gameStatus){
for(i=0;i<HEIGHT;i++){
for(j=0;j<WIDTH;j++){
if(i == HEIGHT-1){
map[i][j] = '~';
}else if(j == 0 || j == WIDTH-1){
map[i][j] = '|';
}else{
map[i][j] = ' ';
}
}
}
for(i = 0;i<3;i++){
for(j = 0;j<5;j++){
map[planeY+i][planeX+j] = plane[i][j];
}
}
i=bulletFront;
while(i!=bulletRear){
map[bulletY[i]][bulletX[i]] = '*';
i=(i+1)%bulletNum;
}
Sleep(100);
}
}
void darwMap(void* param){
int i, j, k;
char buffer[100000];
while(gameStatus){
k=0;
for(i=0;i<HEIGHT;i++){
for(j=0;j<WIDTH;j++){
buffer[k++] = map[i][j];
}
buffer[k++] = '\n';
}
buffer[k] = '\0';
system("cls");
printf(buffer);
printf("您的得分:%d\n", score);
printf("操作说明:WSAD分别对印前后左右移动\n**空格是发出子弹**\n");
}
}
void changeBullets(void* param){
int i;
while(gameStatus){
Sleep(500);
i=bulletFront;
while(i!=bulletRear){
bulletY[i]--;
if(bulletY[i] < 0){
bulletFront=(bulletFront+1)%bulletNum;
}
i=(i+1)%bulletNum;
}
}
}
void shootBullets(){
// planeY+1
bulletX[bulletRear] = planeX+2;
bulletY[bulletRear++] = planeY-1;
}
void input(void* param){
char key;
while(gameStatus){
if(_kbhit()){
key = _getch();
switch(key){
case 'q':gameStatus = 0;break;
case 'w':planeY--;break;
case 's':planeY++;break;
case 'a':planeX--;break;
case 'd':planeX++;break;
case 32:shootBullets();break;
}
}
}
}
int main(void){
system("color 02");
_beginthread(changeMap, 0, NULL);
_beginthread(darwMap, 0, NULL);
_beginthread(input, 0, NULL);
_beginthread(changeBullets, 0, NULL);
while(gameStatus){
}
return 0;
}
