- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
import pygame as pg
import sys
pg.init()
screen = pg.display.set_mode((800, 600))
clock = pg.time.Clock()
screenGray = pg.Color('gray80')
RotorFont = pg.font.SysFont("malgun gothic",17)
textColour = pg.Color('navy')
background = pg.Surface(screen.get_size())
background.fill(screenGray)
ACTIONPRINT = False
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if event.type == pg.MOUSEBUTTONDOWN:
ACTIONPRINT = True
screen.blit(background,(0,0))
rotorAA = ['G','N','Z','M','V','B','F','L','Q','R','Y','P','I','C','E','A','D','K','J','W','X','S','H','U','O','T']#2
rotorAB = ['L','Q','R','Y','D','K','J','W','X','S','H','U','O','P','I','C','F','A','G','N','Z','M','V','B','E','T']#3
rotorBA = ['Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M']#4
rotorBB = ['Y','H','Q','V','L','T','C','W','K','P','S','N','X','E','O','M','B','U','G','F','A','J','D','R','Z','I']#5
rotorCA = ['D','F','P','A','N','E','Y','C','S','G','K','J','M','X','O','V','L','W','Q','H','T','U','B','R','Z','I']#6
rotorCB = ['Z','I','A','C','T','F','U','Q','N','V','P','B','D','O','L','R','S','X','M','G','H','J','W','E','K','Y']#7
rotorList = [rotorAA,rotorAB,rotorBA,rotorBB,rotorCA,rotorCB]
count = 0
k = 0
if ACTIONPRINT == True:
ACTIONPRINT = False
for i in range(0,len(rotorList)):
if count % 2 == 0 and count != 0:
k += 25
for j in range(0,26):
a = RotorFont.render(rotorList[i][j],1,textColour)
background.blit(a,(25 + (i * 25) + k,90+(j * 16)))
if rotorList[i][j] == letter and i + 2 < 6:
correspondingLetter = rotorList[i+1][(rotorList[i].index(letter))]
pg.draw.line(background,black,(25 + (i * 25) + k,90+(j * 16)),(25 + (i + 1 * 25) + 25,90+(rotorList[i+1].index(correspondingLetter) * 16)))
letter = rotorList[i+2][rotorList[i+1].index(correspondingLetter)]
count += 1
pg.display.flip()
clock.tick(60)
上面的代码将转子成对地打印到屏幕上。
我想要发生的是在输入中输入一个字母,并在相应的字母之间绘制一条线,从最右边的列表到左边的列表。
但是,我不知道如何开始。
提前致谢!
最佳答案
这是解决方案。在主循环开始之前,我将字母传输到背景表面,然后每帧传输背景。为了存储字母的坐标,我将字母添加到字典中,并以列表作为值,并将坐标附加到该列表中。
当用户按下某个键时,我调用 dict.get 来查找字典中的字母,并将相应的坐标列表分配给我然后使用的 coords
变量传递给 pygame.draw.lines 。如果 key 不存在,get
方法将返回 None
,从而防止出现 KeyError
。
import sys
import pygame as pg
pg.init()
screen = pg.display.set_mode((800, 600))
clock = pg.time.Clock()
screenGray = pg.Color('gray80')
RotorFont = pg.font.SysFont('malgun gothic', 17)
textColour = pg.Color('navy')
background = pg.Surface(screen.get_size())
background.fill(screenGray)
# You can create the lists and blit the letters before the main loop starts.
rotorAA = ['G','N','Z','M','V','B','F','L','Q','R','Y','P','I','C','E','A','D','K','J','W','X','S','H','U','O','T']#2
rotorAB = ['L','Q','R','Y','D','K','J','W','X','S','H','U','O','P','I','C','F','A','G','N','Z','M','V','B','E','T']#3
rotorBA = ['Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M']#4
rotorBB = ['Y','H','Q','V','L','T','C','W','K','P','S','N','X','E','O','M','B','U','G','F','A','J','D','R','Z','I']#5
rotorCA = ['D','F','P','A','N','E','Y','C','S','G','K','J','M','X','O','V','L','W','Q','H','T','U','B','R','Z','I']#6
rotorCB = ['Z','I','A','C','T','F','U','Q','N','V','P','B','D','O','L','R','S','X','M','G','H','J','W','E','K','Y']#7
rotorList = [rotorAA,rotorAB,rotorBA,rotorBB,rotorCA,rotorCB]
# This dict will have the letters as the keys and the
# corresponding coordinates as the values.
rotor_dict = {}
k = 0
# You can use `enumerate` if you need the index and the item.
for i, sublist in enumerate(rotorList):
if i % 2 == 0 and i != 0:
k += 25
for j, letter in enumerate(sublist):
surface = RotorFont.render(letter, 1, textColour)
x = 25 + (i*25) + k
y = 90 + (j*16)
background.blit(surface, (x, y))
# If the letter isn't in the dict, add a new list with the
# first coordinates.
if letter not in rotor_dict:
rotor_dict[letter] = [(x+4, y+11)]
# Otherwise append the next coordinates to the list.
else:
rotor_dict[letter].append((x+4, y+11))
coords = None # The list of currently selected coordinates.
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
elif event.type == pg.KEYDOWN: # If the user pressed a key.
# Use the `get` method which returns `None` by
# default if the key doesn't exist.
# `event.unicode` is the letter (string).
# So if the letter is in the dict, this assigns the coords
# list in the dict to the `coords` variable.
coords = rotor_dict.get(event.unicode.upper())
screen.blit(background, (0, 0))
# If a key was pressed and a list was assigned to `coords`.
if coords is not None:
# Pass the coords list to draw lines.
pg.draw.lines(screen, (200, 100, 0), False, coords, 2)
pg.display.flip()
clock.tick(60)
我还会使用collections.defaultdict
而不是普通的字典,但我不知道你是否已经熟悉它们。
如果您想按照评论中描述的方式连接字母,您需要找出以下列表中字母的索引,并使用索引依次获取下一个字母。
然后,您可以压缩字母及其坐标,将它们传输到背景表面上,并为每个字母再次将坐标列表添加到 rotor_dict
中。
import sys
import pygame as pg
pg.init()
screen = pg.display.set_mode((800, 600))
clock = pg.time.Clock()
screenGray = pg.Color('gray80')
RotorFont = pg.font.SysFont('malgun gothic', 17)
textColour = pg.Color('navy')
background = pg.Surface(screen.get_size())
background.fill(screenGray)
rotorAA = ['G','N','Z','M','V','B','F','L','Q','R','Y','P','I','C','E','A','D','K','J','W','X','S','H','U','O','T']
rotorAB = ['L','Q','R','Y','D','K','J','W','X','S','H','U','O','P','I','C','F','A','G','N','Z','M','V','B','E','T']
rotorBA = ['Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M']
rotorBB = ['Y','H','Q','V','L','T','C','W','K','P','S','N','X','E','O','M','B','U','G','F','A','J','D','R','Z','I']
rotorCA = ['D','F','P','A','N','E','Y','C','S','G','K','J','M','X','O','V','L','W','Q','H','T','U','B','R','Z','I']
rotorCB = ['Z','I','A','C','T','F','U','Q','N','V','P','B','D','O','L','R','S','X','M','G','H','J','W','E','K','Y']
rotor_dict = {}
# Iterate over the letters in the first list.
for i, letter1 in enumerate(rotorAA):
# Find the indices of the following letters.
letter2 = rotorAB[i] # The letter at index i in the AB list.
j = rotorBA.index(letter2) # Letter2's index in the BA list.
letter3 = rotorBB[j] # Letter at index j in the AB list.
k = rotorCA.index(letter3) # Letter3's index in the CA list.
letter4 = rotorCB[k] # Letter at index k in the CB list.
# A list of the connected letters.
letters = [letter1, letter2, letter2, letter3, letter3, letter4]
# The coords of the letters in the `letters` list above.
coords = [(25, 90 + i*16), (50, 90 + i*16),
(100, 90 + j*16), (125, 90 + j*16),
(175, 90 + k*16), (200, 90 + k*16),
]
rotor_dict[letter1] = coords
# Draw the letters. Check out the `zip` function.
for letter, coord in zip(letters, coords):
background.blit(RotorFont.render(letter, True, textColour), coord)
coords = None
offset = pg.math.Vector2(5, 11) # Add this to the coords to center the lines.
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
elif event.type == pg.KEYDOWN:
# Add an offset, so that the lines start at the center points.
# This is called a list comprehension if you haven't seen this before.
coords = [coord+offset for coord in rotor_dict.get(event.unicode.upper())]
screen.blit(background, (0, 0))
if coords is not None:
pg.draw.lines(screen, (200, 100, 0), False, coords, 2)
pg.display.flip()
clock.tick(60)
关于python - 如何在pygame中的两个列表之间画线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48241426/
我想做的是让 JTextPane 在 JPanel 中占用尽可能多的空间。对于我使用的 UpdateInfoPanel: public class UpdateInfoPanel extends JP
我在 JPanel 中有一个 JTextArea,我想将其与 JScrollPane 一起使用。我正在使用 GridBagLayout。当我运行它时,框架似乎为 JScrollPane 腾出了空间,但
我想在 xcode 中实现以下功能。 我有一个 View Controller 。在这个 UIViewController 中,我有一个 UITabBar。它们下面是一个 UIView。将 UITab
有谁知道Firebird 2.5有没有类似于SQL中“STUFF”函数的功能? 我有一个包含父用户记录的表,另一个表包含与父相关的子用户记录。我希望能够提取用户拥有的“ROLES”的逗号分隔字符串,而
我想使用 JSON 作为 mirth channel 的输入和输出,例如详细信息保存在数据库中或创建 HL7 消息。 简而言之,输入为 JSON 解析它并输出为任何格式。 最佳答案 var objec
通常我会使用 R 并执行 merge.by,但这个文件似乎太大了,部门中的任何一台计算机都无法处理它! (任何从事遗传学工作的人的附加信息)本质上,插补似乎删除了 snp ID 的 rs 数字,我只剩
我有一个以前可能被问过的问题,但我很难找到正确的描述。我希望有人能帮助我。 在下面的代码中,我设置了varprice,我想添加javascript变量accu_id以通过rails在我的数据库中查找记
我有一个简单的 SVG 文件,在 Firefox 中可以正常查看 - 它的一些包装文本使用 foreignObject 包含一些 HTML - 文本包装在 div 中:
所以我正在为学校编写一个 Ruby 程序,如果某个值是 1 或 3,则将 bool 值更改为 true,如果是 0 或 2,则更改为 false。由于我有 Java 背景,所以我认为这段代码应该有效:
我做了什么: 我在这些账户之间创建了 VPC 对等连接 互联网网关也连接到每个 VPC 还配置了路由表(以允许来自双方的流量) 情况1: 当这两个 VPC 在同一个账户中时,我成功测试了从另一个 La
我有一个名为 contacts 的表: user_id contact_id 10294 10295 10294 10293 10293 10294 102
我正在使用 Magento 中的新模板。为避免重复代码,我想为每个产品预览使用相同的子模板。 特别是我做了这样一个展示: $products = Mage::getModel('catalog/pro
“for”是否总是检查协议(protocol)中定义的每个函数中第一个参数的类型? 编辑(改写): 当协议(protocol)方法只有一个参数时,根据该单个参数的类型(直接或任意)找到实现。当协议(p
我想从我的 PHP 代码中调用 JavaScript 函数。我通过使用以下方法实现了这一点: echo ' drawChart($id); '; 这工作正常,但我想从我的 PHP 代码中获取数据,我使
这个问题已经有答案了: Event binding on dynamically created elements? (23 个回答) 已关闭 5 年前。 我有一个动态表单,我想在其中附加一些其他 h
我正在尝试找到一种解决方案,以在 componentDidMount 中的映射项上使用 setState。 我正在使用 GraphQL连同 Gatsby返回许多 data 项目,但要求在特定的 pat
我在 ScrollView 中有一个 View 。只要用户按住该 View ,我想每 80 毫秒调用一次方法。这是我已经实现的: final Runnable vibrate = new Runnab
我用 jni 开发了一个 android 应用程序。我在 GetStringUTFChars 的 dvmDecodeIndirectRef 中得到了一个 dvmabort。我只中止了一次。 为什么会这
当我到达我的 Activity 时,我调用 FragmentPagerAdapter 来处理我的不同选项卡。在我的一个选项卡中,我想显示一个 RecyclerView,但他从未出现过,有了断点,我看到
当我按下 Activity 中的按钮时,会弹出一个 DialogFragment。在对话框 fragment 中,有一个看起来像普通 ListView 的 RecyclerView。 我想要的行为是当
我是一名优秀的程序员,十分优秀!