首页/文章/ 详情

Python的射击小游戏

3月前浏览5901

一个基于turtle模块编写的射击小游戏。

假设玩家以往的的命中率为hit_rate = 0.8,随机数函数random()生成0-1之间的随机数,用来模拟玩家射击一次这一动作,若random() > hit_rate则表示击中靶子。蓝色靶上出现一个白色的孔,同时还提示“中靶”。如图所示

若random() < hit_rate则表示击没有击中靶子。靶子没有任何变化,同时提示“脱靶”。

以下是python代码:

import turtle as t
from random import random
t.hideturtle()     # 隐藏箭头
t.dot(70'blue')  # 画靶子
hit_rate = 0.6
if random() < hit_rate:    # random() 生成0-1之间的随机数   
    t.dot(10'white')     # 靶子穿孔
    t.textinput("验靶" ,"中靶")
else:
    t.textinput("验靶" ,"脱靶")
t.done()

turtle的几个方法

hideturtle( )     

可简写为ht()。使箭头(海龟)不可见。当你绘制复杂图形时这是个好主意,因为隐藏海龟可显著加快绘制速度。

dot(size=None, *color)

  • size -- 一个整型数 >= 1 (如果指定)
  • color -- 一个颜色字符串或颜色数值元组

绘制一个直径为 size,颜色为 color 的圆点。如果 size 未指定,则直径取 pensize+4 和 2*pensize 中的较大值。

textinput(title, prompt)

  • title -- 字符串
  • prompt -- 字符串

弹出一个对话框窗口用来输入一个字符串。形参 title 为对话框窗口的标题,prompt 为一条文本,通常用来提示要输入什么信息。返回输入的字符串。如果对话框被取消则返回 None。

来源:数值分析与有限元编程
python游戏
著作权归作者所有,欢迎分享,未经许可,不得转载
首次发布时间:2024-04-02
最近编辑:3月前
太白金星
本科 慢慢来
获赞 2粉丝 5文章 304课程 0
点赞
收藏
作者推荐
配置高颜值的Windows Terminal

1 安装Windows Terminal和新版的PowerShell下载地址:https://github.com/PowerShell/PowerShell/releases/tag/v7.1.3https://github.com/microsoft/terminal/releases/tag/v1.9.1942.0除此之外,还要安装字体CaskaydiaCove Nerd Font, 这个字体能显示一些古怪的符号。https://www.fontke.com/font/64992491/2 安装一些Powershell 插件打开Windows Terminal,如果之前没有配置过它,那么默认打开的是新安装的PowerShell。如图所示,分别输入命令Install-Module -Name PSReadLineInstall-Module oh-my-posh 。如需要支持git插件,输入 Install-Module posh-git ,这要看个人需求了。3 配置 Windows Terminal按ctrl+逗号组合键,打开Windows Terminal的设置面板,左下角有个打开JSON文件,点击,用vscode打开seething.json文件,如图所示,找到新版PowerShell的配置区域。输入下面的代码配置:{ // Powershell 7.1.3配置 &quot;guid&quot;: &quot;{574e775e-4f2a-5b96-ac1e-a2962a402336}&quot;,//这里不能修改,因为每台电脑都不一样 &quot;hidden&quot;: false, &quot;name&quot;: &quot;PWshell&quot;, // 可不改,看个人喜好 // 注意:一定要写上 -nologo,否则开启 powershll 会有一段话输出,很讨厌! &quot;commandline&quot;: &quot;C:/Program Files/PowerShell/7/pwsh.exe -nologo&quot;, &quot;source&quot;: &quot;Windows.Terminal.PowershellCore&quot;, // 启动菜单一定要设置为 &lt;.&gt;,否则后面重要的一步将会无效! &quot;startingDirectory&quot;: &quot;.&quot;, // 字体 &quot;fontFace&quot;: &quot;CaskaydiaCove NF&quot;, &quot;fontSize&quot;: 13, &quot;historySize&quot;: 9001, &quot;padding&quot;: &quot;5, 5, 20, 25&quot;, &quot;snapOnInput&quot;: true, &quot;useAcrylic&quot;: false, // 颜色 &quot;colorScheme&quot;: &quot;Homebrew&quot;} 再往下,schemes后面的中括号内添加{ &quot;name&quot;: &quot;Homebrew&quot;, // 对应上面的颜色配置!!!! &quot;black&quot;: &quot;#000000&quot;, &quot;red&quot;: &quot;#FC5275&quot;, &quot;green&quot;: &quot;#00a600&quot;, &quot;yellow&quot;: &quot;#999900&quot;, &quot;blue&quot;: &quot;#6666e9&quot;, &quot;purple&quot;: &quot;#b200b2&quot;, &quot;cyan&quot;: &quot;#00a6b2&quot;, &quot;white&quot;: &quot;#bfbfbf&quot;, &quot;brightBlack&quot;: &quot;#666666&quot;, &quot;brightRed&quot;: &quot;#e50000&quot;, &quot;brightGreen&quot;: &quot;#00d900&quot;, &quot;brightYellow&quot;: &quot;#e5e500&quot;, &quot;brightBlue&quot;: &quot;#0000ff&quot;, &quot;brightPurple&quot;: &quot;#e500e5&quot;, &quot;brightCyan&quot;: &quot;#00e5e5&quot;, &quot;brightWhite&quot;: &quot;#e5e5e5&quot;, &quot;background&quot;: &quot;#283033&quot;, &quot;foreground&quot;: &quot;#00ff00&quot;}, 4 添加 Powershell 启动参数在 powershell 中输入code $Profile 如果没有安装vscode,则输入notepad.exe $Profile 打开一个空文件,如图所示输入以下内容:#-------- Import Modules BEGIN ---------------------# 引入 posh-git ,可选项,看个人情况Import-Module posh-git# 引入 oh-my-poshImport-Module oh-my-posh# 设置 PowerShell 主题Set-PoshPrompt -Theme Paradox#--------------- Import Modules END ----------------#--------------- Set Hot-keys BEGIN ----------------# 设置 Tab 键补全# Set-PSReadlineKeyHandler -Key Tab -Function Complete# 设置 Ctrl+d 为菜单补全和 IntellisenseSet-PSReadLineKeyHandler -Key &quot;Tab&quot; -Function MenuComplete# 设置 Ctrl+d 为退出 PowerShellSet-PSReadlineKeyHandler -Key &quot;Ctrl+d&quot; -Function ViExit# 设置 Ctrl+z 为撤销Set-PSReadLineKeyHandler -Key &quot;Ctrl+z&quot; -Function Undo# 设置向上键为后向搜索历史记录Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward# 设置向下键为前向搜索历史纪录Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward#--------------- Set Hot-keys END ---------------- 下面就是配置完成的效果 来源:数值分析与有限元编程

未登录
还没有评论
课程
培训
服务
行家
VIP会员 学习 福利任务 兑换礼品
下载APP
联系我们
帮助与反馈