今天我给大家 创作了两款最简单的C++游戏:猜拳游戏及数字游戏下面是代码:复制
猜拳游戏:
#include <bits/stdc++.h>
#include <ctime>//时间头文件
int main(void)
{
//定义变量表示用户出拳
int user;
//定义表示电脑出拳
int NPC;
//定义两者分数
int u=0,n=0;
//定义游戏局数
int num=1;
//定义是否继续游戏
char c;
ffg:
//原样输出游戏规则
system("color 05");
printf(" ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n");
printf("☆※※※※※※※※※※猜※拳※游※戏※※※※※※※※※※※※☆\n");
printf(" ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n");
printf(" code38制作\n");
printf("_________________________________________________________________________________\n");
printf("【游戏规则】\n");
printf(" 1.游戏分为5局,每局您可以猜拳一次,分胜丶负丶平三种结果。\n");
printf(" 2.游戏中您可以输入123分别代表出石头丶剪刀丶布。\n");
printf(" 3.石头比剪刀大,剪刀比布大,布比石头大。\n");
printf(" 4.五局结束后您可以选择再来一次或直接退出游戏。\n");
printf("【游戏选项】\n");
printf(" 1.【石头】\n");
printf(" 2.【剪刀】\n");
printf(" 3.【布】\n\n\n");
//判断输赢
do{
fflush(stdin);
do{
fflush(stdin);
printf("【第%d局】\n",num);
printf("请出拳:");
scanf("%d", &user);
if (user !=1 && user !=2 && user!=3)
{
fflush(stdin);
printf("抱歉,您的出拳出错,请您按照规则出拳\n");
system("pause");
system("cls");
printf(" ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n");
printf("☆※※※※※※※※※※猜※拳※游※戏※※※※※※※※※※※※☆\n");
printf(" ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n");
printf(" 高宇涵制作\n");
printf("_________________________________________________________________________________\n");
printf("【游戏规则】\n");
printf(" 1.游戏分为5局,每局您可以猜拳一次,分胜丶负丶平三种结果。\n");
printf(" 2.游戏中您可以输入123分别代表出石头丶剪刀丶布。\n");
printf(" 3.石头比剪刀大,剪刀比布大,布比石头大。\n");
printf(" 4.五局结束后您可以选择再来一次或直接退出游戏。\n");
printf("【游戏选项】\n");
printf(" 1.【石头】\n");
printf(" 2.【剪刀】\n");
printf(" 3.【布】\n\n\n");
continue;
}
srand(time(NULL));
NPC = rand() % 3 + 1;
if (user == 1)
{
switch (NPC)
{
case 1:
printf("这一局,您出的是【石头】,电脑出的是【石头】,这局是平局 ̄▽ ̄\n");
break;
case 2:
printf("这一局,您出的是【石头】,电脑出的是【剪刀】,这局您赢了︿( ̄︶ ̄)︿\n");
u++;
break;
case 3:
printf("这一局,您出的是【石头】,电脑出的是【布】,这局您输了╮(╯﹏╰)╭\n");
n++;
break;
}
}
else if (user == 2)
{
switch (NPC)
{
case 1:
printf("这一局,您出的是【剪刀】,电脑出的是【石头】,这局您输了╮(╯﹏╰)╭\n");
n++;
break;
case 2:
printf("这一局,您出的是【剪刀】,电脑出的是【剪刀】,这局是平局 ̄▽ ̄\n");
break;
case 3:
printf("这一局,您出的是【剪刀】,电脑出的是【布】,这局您赢了︿( ̄︶ ̄)︿\n");
u++;
break;
}
}
else if (user == 3)
{
switch (NPC)
{
case 1:
printf("这一局,您出的是【布】,电脑出的是【石头】,这局您赢了︿( ̄︶ ̄)︿\n");
u++;
break;
case 2:
printf("这一局,您出的是【布】,电脑出的是【剪刀】,这局您输了╮(╯﹏╰)╭\n");
n++;
break;
case 3:
printf("这一局,您出的是【布】,电脑出的是【布】,这局是平局 ̄▽ ̄\n");
break;
}
}
num++;
user = 0;
} while (num <= 5);
//判断总游戏结果
printf("-------------------------------------------------------------------------");
if (n > u)
{
printf("\n电脑赢了%d局,您赢了%d局,平局%d局,\n很遗憾,您输给了电脑!╮(╯﹏╰)╭\n", n,u,5-n-u);
}
else if (n < u)
{
printf("\n电脑赢了%d局,您赢了%d局,平局%d局,\n恭喜,您赢了!︿( ̄︶ ̄)︿\n", n, u, 5 - n - u);
}
else if (n == u)
{
printf("\n电脑赢了%d局,您赢了%d局,平局%d局,\n最后的游戏结果是平局 ̄▽ ̄\n", n, u, 5 - n - u);
}
printf("-------------------------------------------------------------------------");
n = 0;
u = 0;
fflush(stdin);
num = 1;
ff:
for (;;){
fflush(stdin);
printf("\n您是否还要继续游戏?(y or n)\n");
scanf("%c", &c);
if (c=='y')
{
system("cls");
goto ffg;
break;
}
else if (c == 'n')
{
break;
}
else
{
printf("输入错误,请重新输入!( ̄▽ ̄)\n");
goto ff;
}
}
} while (c=='y');
}
还有数字游戏:
#define _CRT_STCURE_NO_DEPRECATE
#include<bits/stdc++.h>
using namespace std;
void Start ();//游戏开始
void GetResults ();
int i, j, life, maxrand;
char c;
void Start (){
i = 0;
j = 0;
life = 0;
maxrand = 0;
cout << "选择难度等级:\n"; // 难度等级选择;
cout << "1 : 初级 答案在0-15之间\n";
cout << "2 : 中级 答案在0-30之间\n";
cout << "3 : 高级 答案在0-50之间\n";
cout << "你也可以按其他键退出\n";
c = 30;
cin >> c; // 输入难度等级
cout << "\n";
switch (c){
case '1' : maxrand = 15; // 定义最高数
break;
case '2' : maxrand = 30;
break;
case '3' : maxrand = 50;
break;
default : exit(0);
break;
}
life = 5; // 初始化命数;
srand( (unsigned)time( NULL ) );
j = rand() % maxrand; // 0~maxramd随机整数
GetResults();
}
void GetResults (){
if (life <= 0)
// 输出输的信息
{
cout << "你输了!\n\n";
system("pause");//暂停游戏;
system("cls");//清除输出
cout << "\t\t** 数字游戏 **\n";
cout << "这个游戏的目标是猜一个数字。\n";
cout << "你会被要求输入一个数字 (你有 5 次猜测机会)\n";
cout << "系统会告诉你,这个数字太大还是太小\n\n";
Start();
}
cout << "输一个数: \n";
cin >> i; // 输入一个数
if ((i>maxrand) || (i<0)){ // 如果数字超过范围…
cout << "错误:数字是0到" << maxrand<<"之间的数!"<<endl;
GetResults();
}
if (i == j){
cout << "你赢了!\n\n"; // 输出赢的信息
system("pause");//暂停游戏;
system("cls");//清除输出
cout << "\t\t** 数字游戏 **\n";
cout << "这个游戏的目标是猜一个数字。\n";
cout << "你会被要求输入一个数字 (你有 5 次猜测机会)\n";
cout << "系统会告诉你,这个数字太大还是太小\n\n";
Start();
}
else if (i>j){
cout << "太大了!\n";
life--; // 命数-1
cout << "你还剩" << life << "条命 \n\n";//输出命数
GetResults();
}
else if (i<j){
cout << "太小了!\n";
life--;
cout << "你还剩" << life << "条命 \n\n";
GetResults();
}
}
int main (){//主函数来也
cout << "\t\t** 数字游戏 **\n";
cout << "这个游戏的目标是猜一个数字。\n";
cout << "你会被要求输入一个数字 (你有 5 次猜测机会)\n";
cout << "系统会告诉你,这个数字太大还是太小\n\n";
Start();//开始游戏;
return 0;
}
还有JavaScript游戏:
<body></body>
<script src="/three.js"></script>
<script src="/physi.js"></script>
<script src="/spe.js"></script>
<script src="/scoreboard.js"></script>
<script src="/sounds.js"></script>
<script>
// Physics settings
Physijs.scripts.ammo = '/ammo.js';
Physijs.scripts.worker = '/physijs_worker.js';
// The "scene" is where stuff in our game will happen:
var scene = new Physijs.Scene();
scene.setGravity(new THREE.Vector3( 0, -100, 0 ));
var flat = {flatShading: true};
var light = new THREE.AmbientLight('white', 0.2);
scene.add(light);
// The "camera" is what sees the stuff:
var aspectRatio = window.innerWidth / window.innerHeight;
var camera = new THREE.PerspectiveCamera(75, aspectRatio, 1, 10000);
camera.position.z = 500;
scene.add(camera);
// The "renderer" draws what the camera sees onto the screen:
var renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
camera.position.set(0, 100, 200);
camera.lookAt(new THREE.Vector3(0, 0, 0));
renderer.shadowMap.enabled = true;
// ******** START CODING ON THE NEXT LINE ********
var scoreboard = new Scoreboard();
scoreboard.score(0);
scoreboard.countdown(50);
scoreboard.help(
' 用上下左右方向键控制板子倾斜度'+
'让球掉进洞里'+
'游戏结束前,用R键让球返回原位'+
'游戏结束后,用R键重新开始'
);
scoreboard.onTimeExpired(timeExpired);
function timeExpired(){
scoreboard.message(" 游戏结束!");
}
var ball = addBall();
var lights = addLights();
var board = addBoard();
var goal = addGoal();
reset();
addGoalLight();
function addLights() {
var lights = new THREE.Object3D();
var light1 = new THREE.PointLight('white', 0.4);
light1.position.set(50, 50, -100);
light1.castShadow = true;
lights.add(light1);
var light2 = new THREE.PointLight('white', 0.5);
light2.position.set(-50, 50, 175);
light2.castShadow = true;
lights.add(light2);
scene.add(lights);
return lights;
}
function addBall() {
var shape = new THREE.SphereGeometry(10, 25, 21);
var cover = new THREE.MeshPhongMaterial({color: 'blue'});
cover.specular.setRGB(0.6, 0.6, 0.6);
var ball = new Physijs.SphereMesh(shape, cover);
ball.castShadow = true;
scene.add(ball);
return ball;
}
function addBoard() {
var cover = new THREE.MeshPhongMaterial({color: 'goldenrod'});
cover.specular.setRGB(0.9, 0.9, 0.9);
var shape = new THREE.CubeGeometry(50, 2, 200);
var beam1 = new Physijs.BoxMesh(shape, cover, 0);
beam1.position.set(-37, 0, 0);
beam1.receiveShadow = true;
var beam2 = new Physijs.BoxMesh(shape, cover, 0);
beam2.position.set(75, 0, 0);
beam2.receiveShadow = true;
beam1.add(beam2);
shape = new THREE.CubeGeometry(200, 2, 50);
var beam3 = new Physijs.BoxMesh(shape, cover, 0);
beam3.position.set(40, 0, -40);
beam3.receiveShadow = true;
beam1.add(beam3);
var beam4 = new Physijs.BoxMesh(shape, cover, 0);
beam4.position.set(40, 0, 40);
beam4.receiveShadow = true;
beam1.add(beam4);
beam1.rotation.set(0.1, 0, 0);
scene.add(beam1);
return beam1;
}
function addGoal() {
shape = new THREE.CubeGeometry(100, 2, 100);
cover = new THREE.MeshNormalMaterial({wireframe: true});
var goal = new Physijs.BoxMesh(shape, cover, 0);
goal.position.y = -50;
scene.add(goal);
return goal;
}
function reset() {
if (scoreboard.getTimeRemaining() == 0) return;
ball.__dirtyPosition = true;
ball.__dirtyRotation = true;
ball.position.set(-33, 200, -65);
ball.setLinearVelocity(new THREE.Vector3(0, 0, 0));
ball.setAngularVelocity(new THREE.Vector3(0, 0, 0));
board.__dirtyRotation = true;
board.rotation.set(0.1, 0, 0);
}
function r(max){
if (max) return max * Math.random();
return Math.random();
}
function rColor(){
new THREE.Color(r(),r(),r());
}
var fire, goalFire;
function addGoalLight(){
var material = new THREE.TextureLoader().load('/textures/spe/star.png');
fire = new SPE.Group({texture: {value: material}});
goalFire = new SPE.Emitter({particleCount: 1000, maxAge: {value: 4}});
fire.addEmitter(goalFire);
scene.add(fire.mesh);
goalFire.velocity.value = new THREE.Vector3(0, 75, 0);
goalFire.velocity.spread = new THREE.Vector3(10, 7.5, 5);
goalFire.acceleration.value = new THREE.Vector3(0, -15, 0);
goalFire.position.spread = new THREE.Vector3(25, 0, 0);
goalFire.size.value = 25;
goalFire.size.spread = 10;
goalFire.color.value = [new THREE.Color(r(),r(),r()), new THREE.Color(r(),r(),r())];
goalFire.disable();
}
function 加分(){
if (scoreboard.getTimeRemaining() == 0) return;
scoreboard.addPoints(0.8333333333333333333);
Sounds.bubble.play();
}
function win(flashCount) {
加分();
if (scoreboard.getTimeRemaining() == 0) return;
if (!flashCount) flashCount = 0;
goalFire.enable();
flashCount++;
if (flashCount > 10) {
reset();
goalFire.disable();
return;
}
setTimeout(win, 500, flashCount);
}
goal.addEventListener('collision', win);
function addBackground() {
var cover = new THREE.PointsMaterial({color: 'white', size: 2});
var shape = new THREE.Geometry();
var distance = 500;
for (var i = 0; i < 2000; i++) {
var ra = 2 * Math.PI * Math.random();
var dec = 2 * Math.PI * Math.random();
var point = new THREE.Vector3();
point.x = distance * Math.cos(dec) * Math.cos(ra);
point.y = distance * Math.sin(dec);
point.z = distance * Math.cos(dec) * Math.sin(ra);
shape.vertices.push(point);
}
var stars = new THREE.Points(shape, cover);
scene.add(stars);
}
function reset1(){
if (scoreboard.getTimeRemaining() > 0) return;
scoreboard.score(0);
scoreboard.countdown(50);
scoreboard.message('');
}
// Animate motion in the game
var clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
var dt = clock.getDelta();
fire.tick(dt);
lights.rotation.y = lights.rotation.y + dt/2;
}
animate();
// Run physics
function gameStep() {
if (ball.position.y < -500) reset(ball);
scene.simulate();
// Update physics 60 times a second so that motion is smooth
setTimeout(gameStep, 1000/60);
}
gameStep();
document.addEventListener("keydown", sendKeyDown);
function sendKeyDown(event){
var code = event.code;
if (code == 'ArrowLeft') left();
if (code == 'ArrowRight') right();
if (code == 'ArrowUp') up();
if (code == 'ArrowDown') down();
if (code == 'KeyR') reset();
if (code == 'KeyR') reset1();
}
function left() { tilt('z', 0.02); }
function right() { tilt('z', -0.02); }
function up() { tilt('x', -0.02); }
function down() { tilt('x', 0.02); }
function tilt(dir, amount) {
board.__dirtyRotation = true;
board.rotation[dir] = board.rotation[dir] + amount;
}
function addBackground() {
var cover = new THREE.PointsMaterial({color: 'white', size: 2});
var shape = new THREE.Geometry();
var distance = 500;
for (var i = 0; i < 2000; i++) {
var ra = 2 * Math.PI * Math.random();
var dec = 2 * Math.PI * Math.random();
var point = new THREE.Vector3();
point.x = distance * Math.cos(dec) * Math.cos(ra);
point.y = distance * Math.sin(dec);
point.z = distance * Math.cos(dec) * Math.sin(ra);
shape.vertices.push(point);
}
var stars = new THREE.Points(shape, cover);
scene.add(stars);
}
addBackground();
</script>
如果没有登陆复制不了,方法可见
https://www.cnblogs.com/art-geek/p/15880387.html
JavaScript代码复制到那里?
3DE代码编辑器
详见上一期。
记得关注我,有更多代码、编程方法等你!
求关注,求点赞!!