Python小游戏制作(简单试练版)

文章展示了两个使用Python编写的简单游戏:猜数字和猜谜语。猜数字游戏中,用户需在指定范围内猜一个随机数;猜谜语游戏则要求用户在限定时间内回答一系列预设的词语谜语。代码中包含了输入处理、循环逻辑以及时间计算等元素。
摘要由CSDN通过智能技术生成

目录

猜数字

例图

代码

猜谜语

例图

代码


猜数字

例图

代码

import random

bot=int(input('Set range bottom\n'))
top=int(input('Set range top\n'))
rand=random.randint(bot,top)
print ('Random number in ['+str(bot)+','+str(top)+'] generated!')
num=int(input('###Guess the number###\n'))
cnt=1
while (num!=rand):
    if (num<rand):
        print('*_* Lower than the answer')
    else:
        print('T_T Higher than the answer')
    num=int(input('###Guess the number###\n'))
    cnt=cnt+1
print('^_^ You get the answer with [%d] times'%cnt)

猜谜语

例图

代码

import time
num = int(input('有多少组玩家\n'))
guessWord = []
correct = []
#定义列表,num+10是不想让guessWord下标不合法
for i in range(0,num+10):
    guessWord.append(0)
    correct.append(0)
wordNum=10
guessWord[0] = ['打情骂俏','海绵宝宝','娇媚','金鸡独立','狼吞虎咽','睡眼朦胧','鹤立鸡群','手舞足蹈','卓别林','穿越火线']
guessWord[1] = ['眉飞色舞','英雄联盟','扭秧歌','偷看美女','大摇大摆','回眸一笑','市场营销','大眼瞪小眼','自恋','处女座']
guessWord[2] = ['狗急跳墙','捧腹大笑','目不转睛','愁眉苦脸','左顾右盼','宫保鸡丁','升国旗','暗恋','臭袜子','趁火打劫']
 
flag='n'
 
for i in range(0,num):
    start = time.time()
    for k in range(0,wordNum):
        #显示词语
        print (('%d.%s')%(k+1,guessWord[i][k]))
     
        flag = input('请答题,答对请输入y,跳过请输入任意键')
        end = time.time()
        sec = end-start
        #统计用时
        if (110<=sec<=120):
            print ('还有10秒钟')
        if (sec>=120):
            print ('时间到!游戏结束')
            break
         
        if (flag=='y'):
            correct[i]=correct[i]+1
            continue
        else:
            continue
    str_temp=('第%d组答对数目:%d') % (i+1,correct[i]) 
    print (str_temp)

喜欢就点赞支持一下吧

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值