- 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/
Surface.blit在 1.8 中有一个新参数:混合。定义了以下值: BLEND_ADD BLEND_SUB BLEND_MULT BLEND_MIN BLEND_MAX BLEND_RGBA_A
import sys import pygame import pygame.locals as pgl class Test: def __init__(self): pyg
我对 PyGame 比较陌生。我正在尝试制作一个简单的程序来显示表示鼠标在屏幕上的位置的字符串。 import pygame, sys from pygame.locals import * pyga
我有总是在后台运行的音乐和一些在触发时会播放声音的事件。音乐效果很好。 pygame.mixer.music.load(os.path.join(SOUND_FOLDER, 'WateryGrave.
我有这些代码 FONT = pygame.font.Font("font/calibri.ttf", 50) FONT.size = 25 但是编译器说 AttributeError: 'pygame
有我正在导入的图像: look_1 = pygame.image.load('data\\png\\look1.png').convert_alpha() 我试图减少它的大小是这样的: pygame.
我正在为我的 pygame 制作一个帮助屏幕,每当我运行它时,我都会收到此错误消息: > self.surface.blit(self.helpscreen) TypeError: argument
在 pyGame 中应用程序,我想渲染 SVG 中描述的无分辨率 GUI 小部件。 我怎样才能做到这一点? (我喜欢 OCEMP GUI 工具包,但它的渲染似乎依赖于位图) 最佳答案 这是一个完整的例
有没有办法将多首歌曲加载到 Pygame 中?我不是在谈论这样的音效; crash_sound = pygame.mixer.Sound("crash.ogg") #and pygame.mixer.
我还有一个问题。当我尝试运行我的代码时,pygame 启动然后立即停止。 这是我的代码: import pygame import os import time import random pygam
我正在使用 pymunk 和 pygame 开发一个项目。我正在使用 PivotJoint 约束将我的 body 连接在一起。如果可能的话,我想让关节不可见 - 有什么办法可以做到这一点吗?现在关节在
我使用 fedora 20、Python 2.7 和 virtualenv 1.10.1。我想在 virtualenv 中安装 pygame,我得到了 You are installing a pot
尝试将文本添加到矩形中并使用箭头键在屏幕上移动矩形。我想让文字不会超出边缘。到目前为止,我已经在没有将其放入 Rect 中的情况下工作了,但我想让 Rect 函数工作。现在文本只是反弹回来,我不知道要
我是第一次玩 pygame(总的来说我是 python 的新手),想知道是否有人可以帮助我... 我正在制作一款小型射击游戏,希望能够为坏人创建一个类。我的想法是类应该继承自 pygame.Surfa
我在 Windows 10 机器上运行了 python 3.9.1。我通过 pip 在我的机器上安装了 pygame 2.0.1 (python -m pip install https://gith
错误: File "/home/alien/cncell/core/animator.py", line 413, in create_animation_from_data pygame
这是代码。 5000 个弹跳旋转的红色方块。 (16x16 png) 在 pygame 版本上,我获得 30 fps,但使用 pyglet 获得 10 fps。对于这种事情,OpenGl 不应该更快吗
我以为 pygame.font.Font 是用来加载 .ttf 字体的,如果没有 .ttf 文件在同一个目录下就无法加载字体,但我看过一个视频,有人在没有 .ttf 文件的情况下加载字体。 ttf 字
我正在尝试使用 Travis CI 设置一个项目。项目也使用 pygame。我曾多次尝试设置它 - 但它似乎失败了。 我得到的最接近的是以下内容: .travis.yml : language: py
这个问题已经有答案了: Python error "ImportError: No module named" (38 个回答) 已关闭 3 年前。 我正在使用 Mac 并输入 pip install
我是一名优秀的程序员,十分优秀!