|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Minecraft程序猿 于 2021-2-13 15:00 编辑
昨天晚上闲来无事写了一个小游戏,今天改进了一下,按WASD,wasd移动小人,按1~5切换小人样式,按Q,q退出程序,有3个随机运动敌人,碰撞检测系统,分数(步数)统计,特殊颜色高亮,Game Over界面,游戏结束标题改变,游戏暂停(按P,p),部分功能简化,写了一下午,编译时需要如果是gcc加上 -lpthread 参数, -std=c99 参数,仅限Windows,附上截图和源代码(268行),不得不说Virsual Studio Code真的好用,编译环境:gcc(MinGW-w64)#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <pthread.h>
unsigned int x = 0, y = 0, w = 0, step = 0;
unsigned int x1 = 12, x2 = 32, x3 = 8;
unsigned int *xp = &x, *yp = &y;
unsigned int y1_ = 12, y2 = 10, y3 = 6;
unsigned int join1, join2;
unsigned char key, line;
unsigned char *custom[] = {"*(^_^)*", "*_*", "@^@", "QwQ", "QAQ"};
unsigned char *peoc = "$.$";
HANDLE handle;
CONSOLE_CURSOR_INFO cursor;
void gotoxy(unsigned int x, unsigned int y)
{
COORD coord = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void move()
{
system("cls");
gotoxy(0, y+25);
printf("Step:%u", step);
gotoxy(0, y+27);
printf("Player\tx:%u, y:%u\n", x, y);
gotoxy(0, y+29);
printf("Mob-1\tx:%u, y:%u", x1, y1_);
gotoxy(0, y+30);
printf("Mob-2\tx:%u, y:%u", x2, y2);
gotoxy(0, y+31);
printf("Mob-3\tx:%u, y:%u", x3, y3);
gotoxy(x, 0);
gotoxy(x, y);
SetConsoleTextAttribute(handle, 0xA);
printf("%s", custom[w]);
SetConsoleTextAttribute(handle, 0xF);
gotoxy(x1, y1_);
printf("%s", peoc);
gotoxy(x2, y2);
printf("%s", peoc);
gotoxy(x3, y3);
printf("%s", peoc);
}
void left(unsigned int *x)
{
if (*x > 0)
{
(*x)--;
}
}
void right(unsigned int *x)
{
(*x)++;
}
void up(unsigned int *y)
{
if (*y > 0)
{
(*y)--;
}
}
void down(unsigned int *y)
{
(*y)++;
}
void peo_move(void)
{
time_t t;
static unsigned int *px, *py;
srand((unsigned)time(&t));
for(int num = 0; num < 3; num++)
{
switch (num)
{
case 0: px = &x1; py = &y1_; break;
case 1: px = &x2; py = &y2; break;
case 2: px = &x3; py = &y3;
}
switch (rand() % 4 + 0)
{
case 0: up(py); break;
case 1: down(py); break;
case 2: left(px); break;
case 3: right(px);
}
}
}
void gameover(void)
{
SetConsoleTitle("Game Over...");
system("cls");
printf("\n\
###################################\n\
#+-------------------------------+#\n\
#| |#\n\
#| |#\n\
#| GAME OVER |#\n\
#| Step:%d \t |#\n\
#| |#\n\
#| |#\n\
#+-------------------------------+#\n\
###################################\n\n", step);
Sleep(2000);
SetConsoleTextAttribute(handle, 0x7);
system("pause && cls");
exit(0);
}
void player(void)
{
while (1)
{
peo_move();
gotoxy(0, 0);
key = getch();
switch (key)
{
case 'W':
case 'w':
step++;
up(yp);
break;
case 'S':
case 's':
step++;
down(yp);
break;
case 'A':
case 'a':
step++;
left(xp);
break;
case 'D':
case 'd':
step++;
right(xp);
break;
case '1': w = 0;break;
case '2': w = 1;break;
case '3': w = 2;break;
case '4': w = 3;break;
case '5': w = 4;break;
case 'P':
case 'p':
system("cls");
printf("\n\
###################################\n\
#+-------------------------------+#\n\
#| |#\n\
#| |#\n\
#| GAME PAUSE |#\n\
#| Step:%d \t |#\n\
#| |#\n\
#| |#\n\
#+-------------------------------+#\n\
###################################\n\n", step);
system("pause");
break;
case 'Q':
case 'q':
system("cls");
SetConsoleTextAttribute(handle, 0x7);
exit(0);
}
gotoxy(0, 0);
move();
if (((y == y1_) && (line = 1)) || ((y == y2) && (line = 2)) || ((y == y3) && (line = 3)))
{
for (int i = 0; i < strlen(custom[w]); i++)
{
for (int j = 0; j < 3; j++)
{
if (((x+i == x1+j) && (line == 1)) || ((x+i == x2+j) && (line == 2)) || ((x+i == x3+j) && (line == 3)))
{
gameover();
}
}
}
}
}
}
void HideCursor(void)
{
handle = GetStdHandle(STD_OUTPUT_HANDLE);
cursor.bVisible = FALSE;
cursor.dwSize = sizeof(cursor);
SetConsoleCursorInfo(handle, &cursor);
}
int main(void)
{
SetConsoleTitle("Game Fun *(^_^)*");
system("cls");
HideCursor();
gotoxy(0, 0);
SetConsoleTextAttribute(handle, 0x1);
printf("Game Fun *(^_^)*\nCopyright 2021 The Little Box Technology\nBy Logic\n2021.2.12(Fri) 15:54\n\n");
SetConsoleTextAttribute(handle, 0xA);
printf(" W");
SetConsoleTextAttribute(handle, 0xF);
printf("\n1. ");
SetConsoleTextAttribute(handle, 0xA);
printf("A S D");
SetConsoleTextAttribute(handle, 0xD);
printf("\tMove the Role\n\n");
SetConsoleTextAttribute(handle, 0xF);
printf("2. ");
SetConsoleTextAttribute(handle, 0xA);
printf(" Q");
SetConsoleTextAttribute(handle, 0xD);
printf("\t\tQuit the Game\n\n");
SetConsoleTextAttribute(handle, 0xF);
printf("3. ");
SetConsoleTextAttribute(handle, 0xA);
printf(" P");
SetConsoleTextAttribute(handle, 0xD);
printf("\t\tPause the Game\n\n");
SetConsoleTextAttribute(handle, 0xF);
printf("4. ");
SetConsoleTextAttribute(handle, 0xA);
printf("1~5\t");
SetConsoleTextAttribute(handle, 0xD);
printf("\tSwitch Roles\n\n\n");
SetConsoleTextAttribute(handle, 0x7);
printf("_________________________________________\nPress Any Key To");
SetConsoleTextAttribute(handle, 0xF);
printf(" Start the Game...");
pthread_t people, playerM;
int thread1, playerT;
thread1 = pthread_create(&people, NULL, (void *)&peo_move, NULL);
playerT = pthread_create(&playerM, NULL, (void *)&player, NULL);
if ((thread1 != 0) && (playerT != 0))
{
printf("线程无法正常创建...");
Sleep(1500);
exit(1);
}
join1 = pthread_join(people, NULL);
join2 = pthread_join(playerM, NULL);
if ((join1 != 0) && (join2 != 0))
{
printf("线程无法正常运行...");
Sleep(1500);
exit(1);
}
gotoxy(0, 0);
printf("%s", custom[w]);
return 0;
}
|
|