用Python制作双色球游戏

双色球模拟游戏----(Python)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from random import choice
# from pathlib import Path
# 添加音乐模块
import pygame
# 统计历史出现次数,添加计数模块
import collections

# 添加音乐
url = 'bg_jz.mp3'
pygame.mixer.init()
pygame.mixer.music.load(url)


# 统计号码出现次数函数
def analyze_ball_appearances(numbers):
# numbers需要是一个列表
# Counter返回的是一个字典
counter = collections.Counter(numbers)
# 通过值对字典排序
sorted_keys = sorted(
counter.keys(), key=lambda k: counter[k], reverse=True)
# 遍历排序号的字典
for key in sorted_keys:
value = counter[key]
print(f"{key}出现了{value}次.", end=" ")

max_value = max(counter.values()) # 寻找字典最大值
min_value = min(counter.values()) # 寻找字典最小值
max_keys = [key for key, value in counter.items() if value ==
max_value] # 找到对应最大值的键
min_keys = [key for key, value in counter.items() if value ==
min_value] # 找到对应最小值的键
print()
print("---" * 10)
# path = Path("tongji.txt")
# xie = ''
print("出现次数最多的是:")
for key in max_keys:
# xie += f"{key}出现了{max_value}次."
# path.write_text(f'{xie}', encoding='utf-8')
print(f"{key}出现了{max_value}次.")
print("出现次数最少的是:")
for key in min_keys:
print(f"{key}出现了{min_value}次.")


# 抽奖测试
# 双色球测试


def shuangseqiu(money: float = 2):
"""
玩法说明
输入6个红球(1-32)和一个蓝球号码(1-16)
"""
# 我的抽奖号码
my_red = set()
my_bule = set()
# 默认抽奖次数
count = 0
# 中奖次数统计
zj_01,zj_02,zj_03,zj_04,zj_05,zj_06 = 0,0,0,0,0,0
# 统计红球出现的历史次数
red_number = []
# 统计篮球出现的历史次数
bule_number = []
# 红球列表
lis = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
# 蓝球列表
lis_01 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
# 中奖号码
hongqiu = set()
lanqiu = set()

print(
f"我们设置了6个等级的奖项,分别是:一等奖{money * (1 + 49999)}元,二等奖{money * (1 + 19999)}元,三等奖{money * (1 + 1499)}元,四等奖{money * (1 + 99)}元,五等奖{money * (1 + 4)}元,六等奖{money * (1 + 1.5)}元")
print("玩一次的费用是{:.1f}元钱".format(money))
print("双色球娱乐,请按规则输入号码")
try:
q = int(input("请先设置要玩几个红球的游戏:"))
except ValueError:
print("你输入有误或没有输入数字, 默认设置了6个球")
q = 6
while True:
if q < 1 or q > 6:
print("只能玩1至6个球的游戏")
try:
q = int(input("请先设置要玩几个红球的游戏:"))
except ValueError:
q = 6
print(f"你输入有误或没有输入数字, 默认设置了{q}个球")
else:
break
# 循环输入红球并添加控制
while len(hongqiu) != q:
try:
red = int(input(f"请输入第{len(hongqiu) + 1}个红球(1-32)号码:"))
except ValueError:
red = choice(lis)
print(f"你输入有误或没有输入数字, 随机选择了: {red}")
if red not in hongqiu and 32 >= red >= 1:
hongqiu.add(red)
lis.remove(red)
else:
print("你输入了错误的号码,请重新输入.")
# 输入蓝球并添加控制
try:
bule_01 = int(input("请输入蓝球(1-16)号码:"))
except ValueError:
bule_01 = choice(lis_01)
print(f"你输入有误或没有输入数字, 随机选择了: {bule_01}")
while True:
if bule_01 > 16 or bule_01 < 1:
bule_01 = int(input("超出范围了,请重新输入蓝球(1-16)号码:"))
else:
lanqiu.add(bule_01)
lis_01.remove(bule_01)
break
try:
n = int(input("请输入抽多少次奖:"))
except ValueError:
n = 10
print(f"你输入有误或没有输入数字, 默认是{n}次.")

# 循环抽奖
while my_bule != lanqiu or my_red != hongqiu or n >= count:
list_red = []
list_bule = []
lis = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
lis_01 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
print(f'你买的号码是---红球:{hongqiu} 蓝球:{lanqiu}')
print(f'这是第{count + 1}次。')
# 红球
for i in range(len(hongqiu)):
num_red = choice(lis[:len(lis) + 1 - i])
print(f'第{i + 1}个是:', num_red)
list_red.append(num_red)
red_number.append(num_red)
lis.remove(num_red)
# 蓝球
for i in range(len(lanqiu)):
num_bule = choice(lis_01[:len(lis_01) + 1 - i])
print('蓝球是:', num_bule)
list_bule.append(num_bule)
bule_number.append(num_bule)
lis_01.remove(num_bule)
# 得到的号码列表赋值给集合
my_red = set(list_red)
my_bule = set(list_bule)
# 打印彩票号码
print("开奖号码是:红球 ", end="")
for i in my_red:
print(i, end=" ")
print("蓝球 ", end="")
for i in my_bule:
print(i, end=" ")
print(f"\n抽了{count + 1}次奖了")
# 判断中奖与否
if my_bule != lanqiu or len(my_red.intersection(hongqiu)) <= 4:
print("\n很遗憾,你没有中奖")
else:
if my_bule == lanqiu:
zj_06 += 1
print("\n恭喜你中了六等奖,奖金{:.1f}元人民币".format((money * (1 + 1.5))))
# 通过intersection()方法查找我的号码和开奖号码的--并集
if my_bule == lanqiu and len(my_red.intersection(hongqiu)) >= 3 or len(my_red.intersection(hongqiu)) >= 4:
zj_05 += 1
print("\n恭喜你中了五等奖,奖金{:.1f}元人民币".format((money * (1 + 4))))
if my_bule == lanqiu and len(my_red.intersection(hongqiu)) >= 4 or len(
my_red.intersection(hongqiu)) >= 5:
zj_04 += 1
print("\n恭喜你中了四等奖,奖金{:.1f}元人民币".format((money * (1 + 99))))
if my_bule == lanqiu and len(my_red.intersection(hongqiu)) >= 5:
zj_03 += 1
print("\n恭喜你中了三等奖,奖金{:.1f}元人民币".format(
(money * (1 + 1499))))
if len(my_red.intersection(hongqiu)) >= 6:
zj_02 += 1
print("\n恭喜你中了二等奖,奖金{:.1f}元人民币".format((money * (1 + 19999))))
if my_bule == lanqiu:
zj_01 += 1
print("\n恭喜你中了一等奖,奖金{:.1f}元人民币".format(
(money * (1 + 49999))))
count += 1
print("---" * 10)

# 判断是否达到游玩次数
if count >= n:
print("红球历史出现次数")
analyze_ball_appearances(red_number)
print("---" * 10)
print("蓝球历史出现次数")
analyze_ball_appearances(bule_number)
print("---" * 10)

print(
f"你总共的中奖次数为:\n一等奖:{zj_01}次\n二等奖:{zj_02}次\n三等奖:{zj_03}次\n四等奖:{zj_04}次\n五等奖:{zj_05}次\n六等奖:{zj_06}次")
print("你总共用了{:.1f}元钱。".format(n * money))
print(
f"合计奖金为:{zj_01 * (money * (1 + 49999)) + zj_02 * (money * (1 + 19999)) + zj_03 * (money * (1 + 1499)) + zj_04 * (money * (1 + 99)) + zj_05 * (money * (1 + 4)) + zj_06 * (money * (1 + 1.5))}元.")

return


def lianxu():
shuangseqiu()
y = input("\n退出输入(q):")
if y == "q":
# 停止播放背景音乐
pygame.mixer.music.stop()
return
else:
lianxu()


# 播放背景音乐 -1 代表无线循环
pygame.mixer.music.play(loops=-1)
lianxu()


用Python制作双色球游戏
http://example.com/2024/10/03/2024-10-03-双色球游戏/
作者
zqten
发布于
2024年10月3日
许可协议