python_内置游戏练习

guess_number.py

# 一个简单的猜年龄游戏  
import random  
guess_taken = 0  
print("who are you?")  
myname = input()  
  
number = random.randint(1,20)#randint(a, b) 返回随机整数 N 满足 a <= N <= b。  
print("oh," + myname + ".you look so younger?")  
  
for guess_taken in range(1,6):#设定最多5次猜测机会  
print("try a guess")  
guess = int(input())  
# guess = int(guess)  
  
if guess < number:  
print("too small!")  
  
elif guess > number:  
print("too large!")  
  
else:#猜对了当guess=number  
break#猜对了结束循环  
#五次机会用完也结束循环  
if guess == number:  
guess_taken = str(guess_taken)  
print("Great " + myname.title() +"!"+ guess_taken + " guesses is correct!")  
  
if guess != number:  
number = str(number)  
print("Don't guess! My age is "+ number)

random module

Python random 模块 | 菜鸟教程 (runoob.com)
Help on method randint in module random:

randint(a, b) method of random.Random instance
Return random integer in range [a, b], including both end points.

dargon_world.py

import random
import time

def display_intro():#显示简介
    print("There is a world of the dragon,You have to choose between two caves, one with a good dragon and one with a bad dragon\n")

def choose_cave():#选择洞穴
    cave = ""
    while cave != "1" and cave != "2":
        print("You choose cave which to enter? 1 or 2")
        cave = input()
    cave = int(cave)
    return cave
def check_cave(chosen_cave):
    print("你正在靠近洞穴...")
    time.sleep(2)
    print("十分黑暗...")
    time.sleep(2)
    print("突然跳出了一条大龙\n")
    time.sleep(2)

    friendly_cave = random.randint(1,2)
    if chosen_cave == friendly_cave:
        print("然后善良的龙给了你宝藏")
    else:
        print("然后恶龙一口把你吃了")

play_again = "yes"
while play_again == "yes":
    display_intro()
    cave_number = choose_cave()
    check_cave(int(cave_number))

    print("你还想再玩一次吗?(yes or no)")
    play_again = input()

code.py

#破解恺撒密码游戏的过程
letter_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

# 加密函数
def encrypt(plaintext, key):
    ciphertext=""#密文
    for ch in plaintext:#遍历明文
        if ch.isalpha():
        # 明文是否为字母,如果是,判断大小写,分别加密
            if ch.isupper():
                ciphertext += letter_list[(ord(ch)-65+key) % 26]
            else:
                ciphertext += letter_list[(ord(ch)-97+key) % 26].lower()
        else:
            # 如果不为字母,直接添加至密文字符里
            ciphertext += ch
    return ciphertext

# 解密函数
def decrypt(ciphertext, key):
    plaintext = ""#明文
    for ch in ciphertext:
        if ch.isalpha():
            if ch.isupper():
                plaintext += letter_list[(ord(ch)-65-key) % 26]
            else:
                plaintext += letter_list[(ord(ch)-97-key) % 26].lower()
        else:
            plaintext += ch
    return plaintext


user_input = input("加密按D,解密按E: ")
user_input = user_input.upper()
key = input("请输入密钥: ")
while(int(key.isdigit()==0)):
    key = input("输入有误,密钥为数字,请重新输入: ")

if user_input == "D":
    plaintext = input("请输入明文: ")
    ciphertext = encrypt(plaintext, int(key))
    print("密文为:\n%s" % ciphertext)
else:
    ciphertext = input("请输入密文: ")
    plaintext=decrypt(ciphertext, int(key))
    print("明文为:\n%s" % plaintext)

ASCII码

ASCII码一览表,ASCII码对照表 (biancheng.net)

你可能感兴趣的:(Pygame,python,游戏,开发语言)