用VB做石头剪刀布游戏

用VB做石头剪刀布游戏,第1张

这是一个很简单的游戏。大致流程如下:在点击下picture的时候,生成一个1-3随机数(也就是电脑出的),可以把石头剪刀、布分别看作0、1、2然后用if来进行判断,最后记下胜负。

代码如下:

请在程序目录下放上三个jpg图片名为“剪刀”、“石头”、“布”picture123的图片请自己在属性列表中设置,pictue4 有电脑加载

Option Explicit'全局变量声明

Dim YouChoose'记录你的选择

Dim ComputerChoose'记录电脑选择

'0 石头 1剪刀 2布

Private Sub Picture1_Click()'答祥选了石头

YouChoose = 0'记录下石头

Randomize'随机数生成函数

ComputerChoose = Int(Rnd() * 3)'电脑选择,并记录下载

display'显示电脑的选择(这是个用户函数)

ToGo'判断胜负(这是个用户函数)

End Sub

Private Sub Picture2_Click()'基本同上,选择剪刀

Randomize

YouChoose = 1

ComputerChoose = Int(Rnd() * 3)

display

ToGo

End Sub

Private Sub Picture3_Click()'基本同上,选择石头

Randomize

YouChoose = 2

ComputerChoose = Int(Rnd() * 3)

display

ToGo

End Sub

Sub ToGo()'判断胜负函数

If YouChoose = ComputerChoose Then'一样就平

MsgBox "平"

ElseIf (YouChoose = 0 And ComputerChoose = 1) Or (YouChoose = 1 And ComputerChoose = 2) Or (YouChoose = 2 And ComputerChoose = 0) Then'出现三种获胜可能之一就判定获胜

MsgBox "你赢"

Else'其他则输

MsgBox "电脑赢"

End If

End Sub

Sub display()'显示电脑的选拆昌择

On Error Resume Next'碰到错旅举扒误则继续

If ComputerChoose = 0 Then'如果电脑选石头

Picture4.Picture = LoadPicture(App.Path + "石头.jpg")'加载石头图片

MsgBox "石头"'对话框d出石头

ElseIf ComputerChoose = 1 Then'同上

Picture4.Picture = LoadPicture(App.Path + "剪刀.jpg")

MsgBox "剪刀"

ElseIf ComputerChoose = 2 Then

Picture4.Picture = LoadPicture(App.Path + "布.jpg")

MsgBox "布"

End If

End Sub

核心部分代码如下:

剩下的自己编即可。都挺简单的,我仅提供一个简单的思路,但并不一定是最后的。你可以尝试用下别的方法。

Private Sub Command1_Click()

'0表示剪刀,1表示石头,2表示布

Randomize

Label1.Caption = word(Rnd * 2)

Label2.Caption = word(Rnd * 2)

Dim result 胡胡启As String

result = ""

'分成六种情况

If Label1.Caption = "石头" And Label2.Caption = "剪刀" Then

  result = "甲赢"

End If

If Label1.Caption = "剪刀" And Label2.Caption = "石头" Then

   result = "乙赢"

End If

If Label1.Caption = "布" And Label2.Caption = "石头" Then

  result = "甲赢"

End If

If Label1.Caption = "石头" And Label2.Caption = "布" Then

  result = "乙赢"

End If

If Label1.Caption = "剪刀" And Label2.Caption = "布" Then

   result = "甲赢"

End If

If Label1.Caption = "布" And Label2.Caption = "剪刀" Then

   result = "乙赢"

End If

If result = ""裤如 Then result = "平手"

Label3.Caption = result '显示结果

End Sub

Function word(n As Integer) As String

Select Case 做昌n

Case 0:

word = "剪刀"

Case 1:

word = "石头"

Case 2:

word = "布"

End Select

End Function

界面如下:

你可以将文字换成图片,那就更加形象了,当然还可以添加其他功能,自己思考吧

#include <stdio.h>

#include <time.h>

#include <stdlib.h>

void main()

{

int com,user

char c

srand((unsigned)time(NULL))//初始化随机数,如果没有这条语句消芦,每次运行电脑产生的随机数序列都是一样的

printf("进入游戏\n")

do{

printf("请出拳(1.剪刀、2.石头、3.布):")

scanf("%d",&user)

com=rand()%3+1//产生1-3范围内的随机数

printf("电脑出%s\n",com==1?"剪刀":com==2?"石头":"拿逗带布")

switch(com-user) //用电脑产生的数和用户输入的数的差值来判断胜负

{

case 0:

printf("平手\n")

break

case 1:

case -2:

printf("电脑赢了\n")

break

case -1:

case 2:

printf("你赢了\n")

}

printf("继续游戏按Y键、其指扮它键退出")

getchar()

c=getchar()

}while(c=='y' || c=='Y')

printf("已经退出游戏")

}

扩展资料

C++编程: 石头剪子布

#include<cstdio>

#include<iostream>

using namespace std

int main()

{

int n

char a[10],b[10]

cin>>n

int c[n+3]

for(int i=1i<=ni++)

{

cin>>a>>b

if(a[0]=='R')

{

if(b[0]=='R')

{

c[i]=0

}

if(b[0]=='S')

{

c[i]=1

}

if(b[0]=='P')

{

c[i]=2

}

}

else if(a[0]=='S')

{

if(b[0]=='R')

{

c[i]=2

}

if(b[0]=='S')

{

c[i]=0

}

if(b[0]=='P')

{

c[i]=1

}

}

else if(a[0]=='P')

{

if(b[0]=='R')

{

c[i]=1

}

if(b[0]=='S')

{

c[i]=2

}

if(b[0]=='P')

{

c[i]=0

}

}

}

for(int i=1i<=ni++)

switch(c[i])

{

case 0:

{

printf("Tie\n")break

}

case 1:

{

printf("Player1\n")break

}

case 2:

{

printf("Player2\n")break

}

}

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12529225.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存