DEVC++实现街头篮球(爷青回)

这是一篇关于创建一个简单的街头篮球游戏的博客。作者用C++编写了仅138行代码的游戏,玩家通过键盘操作角色移动、闪现和投篮。游戏中,玩家需避开AI控制的防守者,成功上篮即可获胜。游戏界面使用控制台显示,并包含开始、游戏过程和结束界面。
摘要由CSDN通过智能技术生成

最近没事,做了个街头篮球游戏,只要138行,还不算多(我的狼人杀1000多行)

实现效果:

 按任意键开始

游戏界面(小写ad移动,小写w闪现,小写s投篮) 

结束界面

#include<algorithm>
#include<iostream>
#include<stdio.h>
#include<iostream>
#include<windows.h>
#include<conio.h>
#include <time.h>
#include <stdlib.h>
int a=1;
int in=0;
using namespace std;
int g_map[5][10];
int mx;
int my;
int dx;
int dy;
//kbhit()
//shuru=getch();
int first() {
	system("color F0");
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
	cout<<"					街头篮球"<<endl;
	cout<<"规则:w闪现,ad移动,s投篮。(请打开英文输入法)"<<endl;
	while(true) {
		if(kbhit()) {
			break;
		}
	}
	for(int i=0; i<5; i++) {
		for(int j=0; j<10; j++) {
			g_map[i][j]=0;
		}
	}
	for(int i=0; i<10; i++) {
		g_map[4][i]=8;
	}
	g_map[3][0]=6;
	mx=3;
	my=0;
	g_map[3][5]=9;
	dx=3;
	dy=5;
	g_map[1][9]=1;
}
int out() {
	system("color F0");
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
	g_map[3][my]=6;
	g_map[3][my]=6;
	for(int i=0; i<4; i++) {
		for(int j=0; j<10; j++) {
			if(i==dx&&j==dy){
				cout<<9;
			}
			else if(i==mx&&j==my){
				cout<<6;
			}
			else if(i==1&&j==9){
				cout<<1;
			}
			else{
				cout<<0;
			}
		}
		cout<<endl;
	}
}
int robot() {
	if(my>dy&&dy<9) {
		dy++;
	}
	if(my<dy&&dy>0) {
		dy--;
	}
	for(int i=0; i<5; i++) {
		for(int j=0; j<5; j++) {
			if(g_map[i][j]==9) {
				g_map[i][j]=0;
			}
		}
	}
	g_map[3][dy]=9;
}
int play() {
	char ch;
	if(kbhit()) {
		ch=getch();
		if(ch=='w'&&my<9-5) {
			my+=5;
		}
		if(ch=='a'&&my>0) {
			my--;
		}
		if(ch=='d'&&my<9) {
			my++;
		}
		if(ch=='s') {
			if(my>=5) {
				in=1;
			}
		}
	}
	for(int i=0; i<5; i++) {
		for(int j=0; j<5; j++) {
			if(g_map[i][j]==6) {
				g_map[i][j]=0;
			}
		}
	}
	g_map[3][my]=6;
	g_map[3][my]=6;
}
int main() {
	first();
	while(true) {
		a++;
		system("cls");
		out();
		robot();
		play();
		Sleep(500);
		if(my==dy&&mx==dx) {
			system("cls");
			system("color F0");
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED	); 
			cout<<"球被抢了,失败";
			return 0;

		}
		if(in==1) {
			system("cls");
			system("color F0");
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN);
			cout<<"上篮成功,胜利";
			return 0;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值