python小游戏源代码20行 (python简单小游戏编程代码讲解)

经过了一段时间的学习,想必有的人已经开始动摇了,因为慢慢的出现了枯燥,乏味的状态,还有的一部分人出现了模糊不清楚,不想继续的念头,为了激励大人还有*引勾**起孩子继续学习下去的动力,我们今天发布几个游戏小脚本,可以重新*引勾**起孩子学习的念头,快来试试!

下面的脚本都是围绕着循环来写的,可以帮助你加深对循环的印象。

python游戏最简单的源代码教程,python简单小游戏的完整编程代码

打开百度App,看更多图片

game.py

相信大家都玩过或者知道摇色子的游戏,就是说色盅里面有三个色子,我们来摇动色子,猜大小的游戏。说白了就是*场赌**里面的一个小游戏。

先上代码

#!/usr/bin/python2

import random

import sys

import time

result = []

while True:

result.append(int(random.uniform(1,7)))

result.append(int(random.uniform(1,7)))

result.append(int(random.uniform(1,7)))

print result

count = 0

index = 2

pointStr = ""

while index >= 0:

currPoint = result[index]

count += currPoint

index -= 1

pointStr += " "

pointStr += str(currPoint)

if count <= 10:

sys.stdout.write(pointStr + " -> " + "small" + "\n")

time.sleep( 1 )

else:

sys.stdout.write(pointStr + " -> " + "big" + "\n")

time.sleep( 1 )

result = []

python游戏最简单的源代码教程,python简单小游戏的完整编程代码

输出结果如下:脚本用的是个无限循环模式,会一直自动摇色子,自动判断大小。

xatedhtst{xxxxx}157> game.py

[2, 5, 4]

4 5 2 -> big

[2, 5, 4]

4 5 2 -> big

[4, 3, 1]

1 3 4 -> small

[3, 5, 2]

2 5 3 -> small

[3, 5, 2]

2 5 3 -> small

python游戏最简单的源代码教程,python简单小游戏的完整编程代码

guess_qty.py

这个是一个心里测试游戏,可以帮你猜出对方心里想的是什么数字,快和好朋友或者孩子一起玩玩,他会觉得你很厉害,很神奇!

二话不说先上代码:

#!/usr/bin/python2

# -*- coding: UTF-8 -*-

import random

s = int(random.uniform(1,100))

#print(s)

m = int(input(’please input the int :’))

while m != s:

if m > s:

print (’big’)

m = int(input(’please input the int:’))

if m < s:

print (’small’)

m = int(input(’please input the int:’))

if m == s:

print (’OK’)

break;

python游戏最简单的源代码教程,python简单小游戏的完整编程代码

运行结果: 系统会自动提是你你猜的数字比他默认的是大还是小,快来玩玩看看谁用的次数最少就能猜中!

xatedhtst{xxxxx}163> guess_qty.py

please input the int :50

big

please input the int:25

big

please input the int:12

big

please input the int:6

big

please input the int:3

small

please input the int:4

OK