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

这篇具有很好参考价值的文章主要介绍了python编程游戏代码大全,python简单的小游戏代码。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

小游戏编程代码,python

小游戏编程代码,python

一、石头剪刀布游戏

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

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

import random

choices = ["Rock", "Paper", "Scissors"]

computer = random.choice(choices)

player = False cpu_score = 0 player_score = 0

while True: player = input("Rock, Paper or Scissors?").capitalize()



# 判断游戏者和电脑的选择



if player == computer:

print("Tie!") elif player == "Rock":



if computer == "Paper":

print("You lose!", computer, "covers", player) cpu_score+=1



else:

print("You win!", player, "smashes", computer) player_score+=1 elif player == "Paper":



if computer == "Scissors":

print("You lose!", computer, "cut", player) cpu_score+=1



else:

print("You win!", player, "covers", computer) player_score+=1 elif player == "Scissors":



if computer == "Rock":

print("You lose...", computer, "smashes", player) cpu_score+=1



else:

print("You win!", player, "cut", computer) player_score+=1 elif player=='E':

print("Final Scores:") print(f"CPU:{cpu_score}") print(f"Plaer:{player_score}")



break else:

print("That's not a valid play. Check your spelling!")

computer = random.choice(choices)

二、自动发送邮件

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

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

import smtplib from email.message

import EmailMessage



email = EmailMessage() ## Creating a object for EmailMessage

email['from'] = 'xyz name' ## Person who is sending

email['to'] = 'xyz id' ## Whom we are sending

email['subject'] = 'xyz subject' ## Subject of email

email.set_content("Xyz content of email") ## content of email



with smtlib.SMTP(host='smtp.gmail.com',port=587) as smtp:

## sending request to server



smtp.ehlo() ## server object

smtp.starttls() ## used to send data between server and client

smtp.login("email_id","Password") ## login id and password of gmail

smtp.send_message(email) ## Sending email



print("email send") ## Printing success message

三、Hangman

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

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

import time

import random



name = input("What is your name? ")



print ("Hello, " + name, "Time to play hangman!")

time.sleep(1)

print ("Start guessing...\n")

time.sleep(0.5) ## A List Of Secret



Words words = ['python','programming','treasure','creative','medium','horror']

word = random.choice(words)

guesses = ' '

turns = 5

while turns > 0:

failed = 0

for char in word:

if char in guesses:

print (char,end="")

else:

print ("_",end=""),

failed += 1



if failed == 0: print ("\nYou won")

break

guess = input("\nguess a character:")

guesses += guess

if guess not in word:

turns -= 1

print("\nWrong")

print("\nYou have", + turns, 'more guesses')

if turns == 0:

print ("\nYou Lose")

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

四、闹钟

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

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


 

from datetime import datetime

from playsound import playsound

alarm_time = input("Enter the time of alarm to be set:HH:MM:SS\n")

alarm_hour=alarm_time[0:2]

alarm_minute=alarm_time[3:5]

alarm_seconds=alarm_time[6:8]

alarm_period = alarm_time[9:11].upper()

print("Setting up alarm..")

while True:

now = datetime.now()

current_hour = now.strftime("%I")

current_minute = now.strftime("%M")

current_seconds = now.strftime("%S")

current_period = now.strftime("%p")

if(alarm_period==current_period):

if(alarm_hour==current_hour):

if(alarm_minute==current_minute):

if(alarm_seconds==current_seconds):

print("Wake Up!") playsound('audio.mp3') ## download the alarm sound from link break

五、天气应用

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

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

安装:requests,BeautifulSoup

from datetime import datetime

from playsound import playsound

alarm_time = input("Enter the time of alarm to be set:HH:MM:SS\n")

alarm_hour=alarm_time[0:2]

alarm_minute=alarm_time[3:5]

alarm_seconds=alarm_time[6:8]

alarm_period = alarm_time[9:11].upper()

print("Setting up alarm..")

while True:

now = datetime.now()

current_hour = now.strftime("%I")

current_minute = now.strftime("%M")

current_seconds = now.strftime("%S")

current_period = now.strftime("%p")

if(alarm_period==current_period):

if(alarm_hour==current_hour):

if(alarm_minute==current_minute):

if(alarm_seconds==current_seconds):

print("Wake Up!") playsound('audio.mp3') ## download the alarm sound from link break

小游戏编程代码,python

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

小游戏编程代码,python

一、石头剪刀布游戏

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

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

import random

choices = ["Rock", "Paper", "Scissors"]

computer = random.choice(choices)

player = False cpu_score = 0 player_score = 0

while True: player = input("Rock, Paper or Scissors?").capitalize()



# 判断游戏者和电脑的选择



if player == computer:

print("Tie!") elif player == "Rock":



if computer == "Paper":

print("You lose!", computer, "covers", player) cpu_score+=1



else:

print("You win!", player, "smashes", computer) player_score+=1 elif player == "Paper":



if computer == "Scissors":

print("You lose!", computer, "cut", player) cpu_score+=1



else:

print("You win!", player, "covers", computer) player_score+=1 elif player == "Scissors":



if computer == "Rock":

print("You lose...", computer, "smashes", player) cpu_score+=1



else:

print("You win!", player, "cut", computer) player_score+=1 elif player=='E':

print("Final Scores:") print(f"CPU:{cpu_score}") print(f"Plaer:{player_score}")



break else:

print("That's not a valid play. Check your spelling!")

computer = random.choice(choices)

二、自动发送邮件

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

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

import smtplib from email.message

import EmailMessage



email = EmailMessage() ## Creating a object for EmailMessage

email['from'] = 'xyz name' ## Person who is sending

email['to'] = 'xyz id' ## Whom we are sending

email['subject'] = 'xyz subject' ## Subject of email

email.set_content("Xyz content of email") ## content of email



with smtlib.SMTP(host='smtp.gmail.com',port=587) as smtp:

## sending request to server



smtp.ehlo() ## server object

smtp.starttls() ## used to send data between server and client

smtp.login("email_id","Password") ## login id and password of gmail

smtp.send_message(email) ## Sending email



print("email send") ## Printing success message

三、Hangman

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

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

import time

import random



name = input("What is your name? ")



print ("Hello, " + name, "Time to play hangman!")

time.sleep(1)

print ("Start guessing...\n")

time.sleep(0.5) ## A List Of Secret



Words words = ['python','programming','treasure','creative','medium','horror']

word = random.choice(words)

guesses = ' '

turns = 5

while turns > 0:

failed = 0

for char in word:

if char in guesses:

print (char,end="")

else:

print ("_",end=""),

failed += 1



if failed == 0: print ("\nYou won")

break

guess = input("\nguess a character:")

guesses += guess

if guess not in word:

turns -= 1

print("\nWrong")

print("\nYou have", + turns, 'more guesses')

if turns == 0:

print ("\nYou Lose")

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

四、闹钟

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

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


 

from datetime import datetime

from playsound import playsound

alarm_time = input("Enter the time of alarm to be set:HH:MM:SS\n")

alarm_hour=alarm_time[0:2]

alarm_minute=alarm_time[3:5]

alarm_seconds=alarm_time[6:8]

alarm_period = alarm_time[9:11].upper()

print("Setting up alarm..")

while True:

now = datetime.now()

current_hour = now.strftime("%I")

current_minute = now.strftime("%M")

current_seconds = now.strftime("%S")

current_period = now.strftime("%p")

if(alarm_period==current_period):

if(alarm_hour==current_hour):

if(alarm_minute==current_minute):

if(alarm_seconds==current_seconds):

print("Wake Up!") playsound('audio.mp3') ## download the alarm sound from link break

五、天气应用

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

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

安装:requests,BeautifulSoup

from datetime import datetime

from playsound import playsound

alarm_time = input("Enter the time of alarm to be set:HH:MM:SS\n")

alarm_hour=alarm_time[0:2]

alarm_minute=alarm_time[3:5]

alarm_seconds=alarm_time[6:8]

alarm_period = alarm_time[9:11].upper()

print("Setting up alarm..")

while True:

now = datetime.now()

current_hour = now.strftime("%I")

current_minute = now.strftime("%M")

current_seconds = now.strftime("%S")

current_period = now.strftime("%p")

if(alarm_period==current_period):

if(alarm_hour==current_hour):

if(alarm_minute==current_minute):

if(alarm_seconds==current_seconds):

print("Wake Up!") playsound('audio.mp3') ## download the alarm sound from link break

小游戏编程代码,python

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

到了这里,关于python编程游戏代码大全,python简单的小游戏代码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • python简单小游戏代码教程,python编程小游戏代码

    大家好,本文将围绕一些简单好玩的python编程游戏展开说明,python编写的入门简单小游戏是一个很多人都想弄明白的事情,想搞清楚python简单小游戏代码教程需要先了解以下几个事情。 Source code download: 本文相关源码 大家好,我是辣条。 今天给大家带来30个py小游戏,一定要

    2024年02月03日
    浏览(17)
  • python简单小游戏代码教程,python编程小游戏简单的

    大家好,小编来为大家解答以下问题,一些简单好玩的python编程游戏,python编写的入门简单小游戏,今天让我们一起来看看吧! 哈喽铁子们 表弟最近在学Python,总是跟我抱怨很枯燥无味,其实,他有没有认真想过,可能是自己学习姿势不对? 比方说,可以通过打游戏来学编

    2024年04月23日
    浏览(12)
  • python简单小游戏代码教程,python小游戏编程100例

    大家好,小编为大家解答一些简单好玩的python编程游戏的问题。很多人还不知道python编写的入门简单小游戏,现在让我们一起来看看吧! Source code download: 本文相关源码 哈喽铁子们 表弟最近在学Python,总是跟我抱怨很枯燥无味,其实,他有没有认真想过,可能是自己学习姿势

    2024年01月22日
    浏览(20)
  • 输入代码即可玩的小游戏,python简单编程小游戏

    大家好,本文将围绕python编写的入门简单小游戏有哪些展开说明,python编写的入门简单小游戏教程是一个很多人都想弄明白的事情,想搞清楚python编写的入门简单小游戏复制需要先了解以下几个事情。 大家好,小编来为大家解答以下问题,一些简单好玩的python编程游戏,py

    2024年02月21日
    浏览(15)
  • python入门小游戏代码20行,python小游戏代码大全

    大家好,给大家分享一下python简单小游戏代码20行,很多人还不知道这一点。下面详细解释一下。现在让我们来看看! 01 整体框架 平台:pycharm 关于pygame的安装这里就不在赘述,大家自行上网找合适自己的版本的安装即可。关于pygame模块知识会穿插在下面代码中介绍,用到什

    2024年04月22日
    浏览(9)
  • python游戏代码大全可复制,python小游戏代码大全

    大家好,本文将围绕python游戏编程入门游戏代码展开说明,python游戏代码大全可复制是一个很多人都想弄明白的事情,想搞清楚python小游戏代码大全需要先了解以下几个事情。 本篇文章给大家谈谈如何用python编写一个简单的小游戏,以及如何用Python做小游戏让别人玩,希望对

    2024年04月08日
    浏览(20)
  • python编程小游戏简单的,python小游戏编程100例

    大家好,给大家分享一下python编程小游戏简单的,很多人还不知道这一点。下面详细解释一下。现在让我们来看看! 不会python就不能用python开发入门级的小游戏? 当然不是, 我收集了十个python入门小游戏的源码和教程 ,并且即使你没有python基础,只要跟着这十个小游戏的开

    2024年02月13日
    浏览(14)
  • python超简单小游戏代码,python简单小游戏代码

    大家好,小编来为大家解答以下问题,python超简单小游戏代码,python简单小游戏代码,今天让我们一起来看看吧! 大家好,我是辣条。 今天给大家带来30个py小游戏,一定要收藏! 目录 有手就行 1、吃金币 2、打乒乓 3、滑雪 4、并夕夕版飞机大战 5、打地鼠 简简单单 6、小恐

    2024年03月14日
    浏览(20)
  • python简单小游戏代码100行,python超简单小游戏代码

    大家好,小编为大家解答python简单小游戏代码100行的问题。很多人还不知道python超简单小游戏代码,现在让我们一起来看看吧! Source code download: 本文相关源码 大家好,小编来为大家解答以下问题,一些简单好玩的python编程游戏,python编写的入门简单小游戏,今天让我们一起

    2024年01月19日
    浏览(15)
  • python简单小游戏代码10行,简单的python小游戏代码

    本篇文章给大家谈谈python简单小游戏代码200行,以及python简单小游戏代码20行,希望对各位有所帮助,不要忘了收藏本站喔。 大家好,小编来为大家解答以下问题,python编程一个最简单游戏代码,python编程游戏代码大全,今天让我们一起来看看吧! 大家好,我是辣条。 今天

    2024年01月22日
    浏览(15)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包