当前位置:   article > 正文

python编程游戏代码大全,python简单的小游戏代码_python小游戏代码

python小游戏代码

大家好,本文将围绕python编程一个最简单游戏代码展开说明,20行python代码的入门级小游戏是一个很多人都想弄明白的事情,想搞清楚python游戏编程入门游戏代码需要先了解以下几个事情。

一、石头剪刀布游戏

目标:创建一个命令行游戏,游戏者可以在石头、剪刀和布之间进行选择,与计算机PK。如果游戏者赢了,得分就会添加,直到结束游戏时,最终的分数会展示给游戏者伪原创工具

提示:接收游戏者的选择,并且与计算机的选择进行比较。计算机的选择是从选择列表中随机选取的。如果游戏者获胜,则增加1分。

  1. import random
  2. choices = ["Rock", "Paper", "Scissors"]
  3. computer = random.choice(choices)
  4. player = False cpu_score = 0 player_score = 0
  5. while True: player = input("Rock, Paper or Scissors?").capitalize()
  6. # 判断游戏者和电脑的选择
  7. if player == computer:
  8. print("Tie!") elif player == "Rock":
  9. if computer == "Paper":
  10. print("You lose!", computer, "covers", player) cpu_score+=1
  11. else:
  12. print("You win!", player, "smashes", computer) player_score+=1 elif player == "Paper":
  13. if computer == "Scissors":
  14. print("You lose!", computer, "cut", player) cpu_score+=1
  15. else:
  16. print("You win!", player, "covers", computer) player_score+=1 elif player == "Scissors":
  17. if computer == "Rock":
  18. print("You lose...", computer, "smashes", player) cpu_score+=1
  19. else:
  20. print("You win!", player, "cut", computer) player_score+=1 elif player=='E':
  21. print("Final Scores:") print(f"CPU:{cpu_score}") print(f"Plaer:{player_score}")
  22. break else:
  23. print("That's not a valid play. Check your spelling!")
  24. computer = random.choice(choices)

二、自动发送邮件

目的:编写一个Python脚本,可以使用这个脚本发送电子邮件。

提示:email库可用于发送电子邮件。

  1. import smtplib from email.message
  2. import EmailMessage
  3. email = EmailMessage() ## Creating a object for EmailMessage
  4. email['from'] = 'xyz name' ## Person who is sending
  5. email['to'] = 'xyz id' ## Whom we are sending
  6. email['subject'] = 'xyz subject' ## Subject of email
  7. email.set_content("Xyz content of email") ## content of email
  8. with smtlib.SMTP(host='smtp.gmail.com',port=587) as smtp:
  9. ## sending request to server
  10. smtp.ehlo() ## server object
  11. smtp.starttls() ## used to send data between server and client
  12. smtp.login("email_id","Password") ## login id and password of gmail
  13. smtp.send_message(email) ## Sending email
  14. print("email send") ## Printing success message

三、Hangman

目的:创建一个简单的命令行hangman游戏。

提示:创建一个密码词的列表并随机选择一个单词。现在将每个单词用下划线“_”表示,给用户提供猜单词的机会,如果用户猜对了单词,则将“_”用单词替换。

  1. import time
  2. import random
  3. name = input("What is your name? ")
  4. print ("Hello, " + name, "Time to play hangman!")
  5. time.sleep(1)
  6. print ("Start guessing...\n")
  7. time.sleep(0.5) ## A List Of Secret
  8. Words words = ['python','programming','treasure','creative','medium','horror']
  9. word = random.choice(words)
  10. guesses = ' '
  11. turns = 5
  12. while turns > 0:
  13. failed = 0
  14. for char in word:
  15. if char in guesses:
  16. print (char,end="")
  17. else:
  18. print ("_",end=""),
  19. failed += 1
  20. if failed == 0: print ("\nYou won")
  21. break
  22. guess = input("\nguess a character:")
  23. guesses += guess
  24. if guess not in word:
  25. turns -= 1
  26. print("\nWrong")
  27. print("\nYou have", + turns, 'more guesses')
  28. if turns == 0:
  29. print ("\nYou Lose")

更多项目源码,请继续关注小编。如果大家在学习中遇到困难,想找一个python学习交流环境,可以加入我们的Python学习Q群249180188,领取python学习资料,会节约很多时间,减少很多遇到的难题。

四、闹钟

目的:编写一个创建闹钟的Python脚本。

提示:你可以使用date-time模块创建闹钟,以及playsound库播放声音。


 

  1. from datetime import datetime
  2. from playsound import playsound
  3. alarm_time = input("Enter the time of alarm to be set:HH:MM:SS\n")
  4. alarm_hour=alarm_time[0:2]
  5. alarm_minute=alarm_time[3:5]
  6. alarm_seconds=alarm_time[6:8]
  7. alarm_period = alarm_time[9:11].upper()
  8. print("Setting up alarm..")
  9. while True:
  10. now = datetime.now()
  11. current_hour = now.strftime("%I")
  12. current_minute = now.strftime("%M")
  13. current_seconds = now.strftime("%S")
  14. current_period = now.strftime("%p")
  15. if(alarm_period==current_period):
  16. if(alarm_hour==current_hour):
  17. if(alarm_minute==current_minute):
  18. if(alarm_seconds==current_seconds):
  19. print("Wake Up!") playsound('audio.mp3') ## download the alarm sound from link break

五、天气应用

目的:编写一个Python脚本,接收城市名称并使用爬虫获取该城市的天气信息。

提示:你可以使用Beautifulsoup和requests库直接从谷歌主页爬取数据。

安装:requests,BeautifulSoup

  1. from datetime import datetime
  2. from playsound import playsound
  3. alarm_time = input("Enter the time of alarm to be set:HH:MM:SS\n")
  4. alarm_hour=alarm_time[0:2]
  5. alarm_minute=alarm_time[3:5]
  6. alarm_seconds=alarm_time[6:8]
  7. alarm_period = alarm_time[9:11].upper()
  8. print("Setting up alarm..")
  9. while True:
  10. now = datetime.now()
  11. current_hour = now.strftime("%I")
  12. current_minute = now.strftime("%M")
  13. current_seconds = now.strftime("%S")
  14. current_period = now.strftime("%p")
  15. if(alarm_period==current_period):
  16. if(alarm_hour==current_hour):
  17. if(alarm_minute==current_minute):
  18. if(alarm_seconds==current_seconds):
  19. print("Wake Up!") playsound('audio.mp3') ## download the alarm sound from link break

在这里还是要推荐下我自己建的Python学习Q群:249029188,群里都是学Python的,如果你想学或者正在学习Python ,欢迎你加入,大家都是软件开发党,不定期分享干货(只有Python软件开发相关的),包括我自己整理的一份2021最新的Python进阶资料和零基础教学,欢迎进阶中和对Python感兴趣的小伙伴加入!

文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树首页概览377731 人正在系统学习中
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/30079
推荐阅读
相关标签
  

闽ICP备14008679号