Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在游戏中添加简单的图形以在屏幕上添加滚动文本?_Python_Graphics - Fatal编程技术网

Python 如何在游戏中添加简单的图形以在屏幕上添加滚动文本?

Python 如何在游戏中添加简单的图形以在屏幕上添加滚动文本?,python,graphics,Python,Graphics,我正在做一个文字冒险游戏,希望能够有图形,使文字在屏幕上移动,并通过滚动像故事的文字。我如何才能做到这一点?这里的解决方案很有效 from tkinter import *; # Import the Tk library. import time; # So that we can use the time library import math; # And perform maths operations tk = Tk(); # Make a new window tk.title(

我正在做一个文字冒险游戏,希望能够有图形,使文字在屏幕上移动,并通过滚动像故事的文字。我如何才能做到这一点?

这里的解决方案很有效

from tkinter import *; # Import the Tk library.

import time; # So that we can use the time library
import math; # And perform maths operations

tk = Tk(); # Make a new window
tk.title("Not Undertale"); # Set the title of the window. You can change this if you like.
canvas = Canvas(tk, width=600, height=400); # Make a canvas that we can put the text on. You can play around with the numbers if you like.
canvas.pack(); # This basically tells Tk to find somewhere to put the canvas.
text = "         Hello. This goes on forever."; # This is the message that will scroll across the screen. You can change this, although I reccommend putting at least on space either before or after the text.
txt = canvas.create_text(300, 200, text = "          ", font = ("Monospace", 12)); # This creates the item that will hold the widget. Again, you can play around with the numbers. The font is Monospace size 12, this is optional. I reccommend using a fixed-width font for this though.

timer = 0; # The timer.
count = 0; # A variable that we will use later.

while True:
    try:
        tk.update(); # The reason this is in a Try/Except statement is because you get errors when you close the window, so we want to ignore that and finish the program.
    except:
        break;
    time.sleep(0.05); # Wait a bit before drawing the next frame
    timer += 0.05; # And add a bit to timer
    count = math.floor(timer * 4) % len(text); # This is where we use the count variable. Basically this changes by 1 every frame, we use this to find what part of the text we're writing.
    if count > len(text): # If the count variable is more than the length of the text, set it to 0.
        count = 0; # I'm positive we don't need this bit, but it's in there just in case.
    display_txt = ""; # This is a temporary variable where we store the scrolling letters.
    c = count; # Copy the count variable to one called c
    while c < count + 10: # While c is between count and ten more than count.
        display_txt += text[c % len(text)]; # We add a letter to display_txt
        c += 1; # And add one to c.
    canvas.itemconfig(txt, text = display_txt); # The last thing we need to do is change the content of the text item so that it displays the scrolled text.
来自tkinter导入*#导入Tk库。
导入时间;#这样我们就可以使用时间图书馆了
导入数学;#并进行数学运算
tk=tk();#做一扇新窗户
tk.标题(“非秘密”)#设置窗口的标题。如果你愿意,你可以改变这个。
画布=画布(tk,宽度=600,高度=400);#制作一个画布,我们可以把文本放在上面。如果你愿意,你可以玩数字游戏。
canvas.pack();#这基本上告诉Tk找到放置画布的地方。
text=“你好,这将永远持续下去。”#这是将在屏幕上滚动的消息。你可以改变这一点,尽管我建议至少在文本之前或之后加上空格。
txt=canvas.create_text(300200,text=”“,font=(“Monospace”,12));#这将创建保存小部件的项目。同样,你可以玩数字游戏。字体为单空格大小12,这是可选的。不过我建议使用固定宽度的字体。
计时器=0;#计时器。
计数=0;#我们稍后将使用的变量。
尽管如此:
尝试:
tk.update();#在Try/Except语句中出现这种情况的原因是,关闭窗口时会出现错误,因此我们希望忽略该错误并完成程序。
除:
打破
时间。睡眠(0.05);#请稍等,然后再绘制下一帧
定时器+=0.05;#并在计时器中添加一个位
计数=数学地板(计时器*4)%len(文本);#这就是我们使用count变量的地方。基本上,每一帧都会改变1,我们用它来找到我们正在写的文本的哪一部分。
如果count>len(文本):#如果count变量大于文本的长度,则将其设置为0。
计数=0;#我肯定我们不需要这一点,但它就在里面以防万一。
显示_txt=“”;#这是一个临时变量,用于存储滚动字母。
c=计数;#将count变量复制到一个名为c的变量
当c

这可能不是最好的方法,但它很有效,您可以尝试一下。

您在UI中使用了什么?例如,您可以使用
tkinter
pgreloated
或仅使用控制台。任何可以工作且易于添加到我当前代码中以制作图形的工具。因此,我建议使用
tkinter
。它非常适合制作简单的图形界面,而且非常容易学习。我将发布一个答案,解释您很快就能做到这一点的方法。您需要注意的一件事是tkinter在Windows和macOS上附带Python,但Linux用户需要单独下载。建议在codeGood point的顶部添加
import
语句。我把它们移到了顶端。