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

104 篇文章 1 订阅
13 篇文章 0 订阅

前言

大家好,本文将围绕python编程一个最简单游戏代码展开说明,20行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入门

在这里插入图片描述

Python入门视频600集:

二、Python爬虫

爬虫作为一个热门的方向,不管是在自己兼职还是当成辅助技能提高工作效率,都是很不错的选择。

通过爬虫技术可以将相关的内容收集起来,分析删选后得到我们真正需要的信息。

这个信息收集分析整合的工作,可应用的范畴非常的广泛,无论是生活服务、出行旅行、金融投资、各类制造业的产品市场需求等等,都能够借助爬虫技术获取更精准有效的信息加以利用。

在这里插入图片描述

Python爬虫视频资料

在这里插入图片描述

三、数据分析

清华大学经管学院发布的《中国经济的数字化转型:人才与就业》报告显示,2025年,数据分析人才缺口预计将达230万。

这么大的人才缺口,数据分析俨然是一片广阔的蓝海!起薪10K真的是家常便饭。

在这里插入图片描述

四、Python高级进阶

从基础的语法内容,到非常多深入的进阶知识点,了解编程语言设计,学完这里基本就了解了python入门到进阶的所有的知识点。

在这里插入图片描述

到这就基本就可以达到企业的用人要求了,如果大家还不知道去去哪找面试资料和简历模板,我这里也为大家整理了一份,真的可以说是保姆及的系统学习路线了。

在这里插入图片描述
但学习编程并不是一蹴而就,而是需要长期的坚持和训练。整理这份学习路线,是希望和大家共同进步,我自己也能去回顾一些技术点。不管是编程新手,还是需要进阶的有一定经验的程序员,我相信都可以从中有所收获。

一蹴而就,而是需要长期的坚持和训练。整理这份学习路线,是希望和大家共同进步,我自己也能去回顾一些技术点。不管是编程新手,还是需要进阶的有一定经验的程序员,我相信都可以从中有所收获。

资料领取

这份完整版的Python全套学习资料已经上传网盘,朋友们如果需要可以点击下方微信卡片免费领取 ↓↓↓【保证100%免费】
或者

点此链接】领取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值