- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我尝试使用 pygame 在 python 中创建一个更具交互性的 mastermind 游戏版本。基本上,CPU 将生成一个随机的元组列表(长度为 4),其中每个元组代表一个 RGB 组合(例如 [(255,0,0), (135,6,79), (45,67,18), (12,90,235,4)])。该程序将允许用户从 3x3 颜色网格中选择 4 种颜色。如果选项与 CPU 生成的选项匹配,则用户获胜。
我尝试遍历每个用坐标 (x,y) 区分的案例,其中我尝试将元组附加到 guess_list。然而,我得到的是包含 4 个相同元组的列表(例如 [(255,0,0), (255,0,0), (255,0,0), (255,0,0)])。另外,我想处理用户点击彩色方 block 之外的区域(黑色区域)的情况
#main game loop
execute = True
while execute:
game_screen.fill(black) #black background
#display the rectangles. args: ((screen where the object is to be displayed), (color), (position(x,y)), (dimensions))
pygame.draw.rect(game_screen, red, ((1/7)*screen_width, (screen_height//2) - 80, 40, 40))
pygame.draw.rect(game_screen, green, ((3/7)*screen_width - 80, (screen_height//2) - 80, 40, 40))
pygame.draw.rect(game_screen, blue, ((5/7)*screen_width - 160, (screen_height//2) - 80, 40, 40))
pygame.draw.rect(game_screen, yellow, ((1/7)*screen_width, (screen_height//2), 40, 40))
pygame.draw.rect(game_screen, orange, ((3/7)*screen_width - 80, (screen_height//2), 40, 40))
pygame.draw.rect(game_screen, purple, ((5/7)*screen_width - 160, (screen_height//2), 40, 40))
pygame.draw.rect(game_screen, maroon, ((1/7)*screen_width, (screen_height//2) + 80, 40, 40))
pygame.draw.rect(game_screen, pink, ((3/7)*screen_width - 80, (screen_height//2) + 80, 40, 40))
pygame.draw.rect(game_screen, brown, ((5/7)*screen_width - 160, (screen_height//2) + 80, 40, 40))
#event block
for event in pygame.event.get(): #loop through user events i.e. keyboard, mouse
if event.type == pygame.QUIT: #exit button
execute = False
if event.type == pygame.MOUSEBUTTONDOWN:
i = 1
while i <= 12: #gets inputs 12 times
black_count = 0
white_count = 0
guess_list = []
x_mouse, y_mouse = pygame.mouse.get_pos()
for clicks in range(5): #gets clicks 4 times
if 80 <= x_mouse <= 120 and 235 <= y_mouse <= 275:
guess_list.append(red)
elif 160 <= x_mouse <= 200 and 235 <= y_mouse <= 275:
guess_list.append(green)
elif 240 <= x_mouse <= 280 and 235 <= y_mouse <= 275:
guess_list.append(blue)
elif 80 <= x_mouse <= 120 and 315 <= y_mouse <= 365:
guess_list.append(yellow)
elif 160 <= x_mouse <= 200 and 315 <= y_mouse <= 365:
guess_list.append(orange)
elif 240 <= x_mouse <= 280 and 315 <= y_mouse <= 365:
guess_list.append(purple)
elif 80 <= x_mouse <= 120 and 395 <= y_mouse <= 435:
guess_list.append(maroon)
elif 160 <= x_mouse <= 200 and 395 <= y_mouse <= 435:
guess_list.append(pink)
elif 240 <= x_mouse <= 280 and 395 <= y_mouse <= 435:
guess_list.append(brown)
else: #clicks outside the squares
print("Only click on the colored squares!!") #this loops 48 times (fix ASAP)
#scoring block i.e. for updating black_count, white_count (code ASAP)
i += 1
print(guess_list)
pygame.display.update()
clock.tick(40) #sets fps
pygame.quit()
最佳答案
您的游戏循环运行,因此当玩家点击某处时,您必须跟踪它是否处于您的游戏状态。游戏状态可以是任何东西,从复杂的对象图到简单的变量。
你只需要记住,当事情随着时间的推移发生(比如玩家多次点击)时,你必须以某种方式跟踪它。
这是我一起破解的一个简单示例。你会明白的。注意评论。
import pygame
import random
def main():
# first, we create a simple list of the possible colors
# pygame has a big internal list of predefined colors we
# can use, like 'blue' or 'lightgrey' etc
# also note that we can easily extend our game by adding
# other colors; and we don't have to touch any other code
# since we use lists and loop over them to draw the game
# and handle inputs. No big if/else monsters
colors = ['blue', 'red', 'green', 'yellow', 'white']
# next, we create a list of rects that represent the part
# of the screen where the user can click to select a color.
# the first one is located at (10, 10), and each rect has a
# size of (32, 32), with a gab of 10 between them
# We use pygame's Rect class so we don't have to do the math
# ourself
rects = {}
x, y = 10, 10
for color in colors:
rects[color] = pygame.Rect(x, y, 32, 32)
x += 32 + 10
# since your game runs in a loop, we have to store the choices
# of the player somewhere. We create a list of the colors the
# user picked this turn, and list of where we store the picks
# of previous turns.
picked = []
history = []
# pick the solution the player has to guess
solution = colors[:]
random.shuffle(solution)
solution = solution[:4]
# let's print the solution so we can cheat :-)
print(solution)
pygame.init()
screen = pygame.display.set_mode((800, 600))
while True:
## EVENT HANDLING
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
if e.type == pygame.MOUSEBUTTONDOWN:
# the use clicked somewhere, so here we decide what happens
# first, check if a color was clicked. Since we have the list
# of rects, it's quite easy.
# we make use of the collidepoint function of the Rect class
# which will return True of the coordinate is inside the Rect
clicked_list = [color for color in rects if rects[color].collidepoint(e.pos)]
if clicked_list:
# the expression above returned a list, and
# the clicked color is the first and only element
color = clicked_list[0]
# so the user clicked a color button
# now add the color to the list of clicked buttons
# we don't allow duplicates
if not color in picked:
picked.append(color)
# that's it. We don't need to do anything more
# here in this event handling part
## DRAWING
screen.fill((50, 50, 50))
# draw the buttons
for color in rects:
pygame.draw.rect(screen, pygame.Color(color), rects[color])
# draw the history
# again, 32 is the size of the buttons, and 10 is the gap between tem
# feel free to use constants and/or extract a function for drawing
# instead of using magic numbers.
x, y = 300, 500 - 32 - 10
for turn in history:
for color in turn:
pygame.draw.rect(screen, pygame.Color(color), (x, y, 32, 32))
x += 32 + 10
y -= 32 + 10
x = 300
# draw what the player picked
x, y = 300, 500
for color in picked:
pygame.draw.rect(screen, pygame.Color(color), (x, y, 32, 32))
x += 32 + 10
pygame.display.flip()
## GAME LOGIC
# check if the player has picked 4 colors
if len(picked) == 4:
# check if the player has won
# this works because picked and solution are
# simple lists of strings
if picked == solution:
print("WIN!")
return
# move the picked colors to the history list
history.append(picked)
picked = []
if __name__ == '__main__':
main()
关于python - 如何将鼠标按钮选择存储在列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58446395/
我正在运行一个辅助角色,并检查 Azure 上托管的存储中是否存在数据。当我将连接字符串用于经典类型的存储时,我的代码可以正常工作,但是当我连接到 V2 Azure 存储时,它会抛出此异常。 “远程服
在我的应用程序的主页上,我正在进行 AJAX 调用以获取应用程序各个部分所需的大量数据。该调用如下所示: var url = "/Taxonomy/GetTaxonomyList/" $.getJSO
大家好,我正在尝试将我的商店导入我的 Vuex Route-Gard。 路由器/auth-guard.js import {store} from '../store' export default
我正在使用 C# 控制台应用程序 (.NET Core 3.1) 从 Azure Blob 存储读取大量图像文件并生成这些图像的缩略图。新图像将保存回 Azure,并将 Blob ID 存储在我们的数
我想将 Mlflow 设置为具有以下组件: 后端存储(本地):在本地使用 SQLite 数据库存储 Mlflow 实体(run_id、params、metrics...) 工件存储(远程):使用 Az
我正在使用 C# 控制台应用程序 (.NET Core 3.1) 从 Azure Blob 存储读取大量图像文件并生成这些图像的缩略图。新图像将保存回 Azure,并将 Blob ID 存储在我们的数
我想将 Mlflow 设置为具有以下组件: 后端存储(本地):在本地使用 SQLite 数据库存储 Mlflow 实体(run_id、params、metrics...) 工件存储(远程):使用 Az
我的 Windows 计算机上的本地文件夹中有一些图像。我想将所有图像上传到同一容器中的同一 blob。 我知道如何使用 Azure Storage SDKs 上传单个文件BlockBlobServi
我尝试发出 GET 请求来获取我的 Azure Blob 存储帐户的帐户详细信息,但每次都显示身份验证失败。谁能判断形成的 header 或签名字符串是否正确或是否存在其他问题? 代码如下: cons
这是用于编写 JSON 的 NeutralinoJS 存储 API。是否可以更新 JSON 文件(推送数据),而不仅仅是用新的 JS 对象覆盖数据。怎么做到的??? // Javascript
我有一个并行阶段设置,想知道是否可以在嵌套阶段之前运行脚本,所以像这样: stage('E2E-PR-CYPRESS') { when { allOf {
我想从命令行而不是从GUI列出VirtualBox VM的详细信息。我对存储细节特别感兴趣。 当我在GUI中单击VM时,可以看到包括存储部分在内的详细信息: 但是到目前为止,我还没有找到通过命令行执行
我有大约 3500 个防洪设施,我想将它们表示为一个网络来确定流动路径(本质上是一个有向图)。我目前正在使用 SqlServer 和 CTE 来递归检查所有节点及其上游组件,只要上游路径没有 fork
谁能告诉我 jquery data() 在哪里存储数据以及何时删除以及如何删除? 如果我用它来存储ajax调用结果,会有性能问题吗? 例如: $("body").data("test", { myDa
有人可以建议如何为 Firebase 存储中的文件设置备份。我能够备份数据库,但不确定如何为 firebase 存储中的文件(我有图像)设置定期备份。 最佳答案 如何进行 Firebase 存储的本地
我最近开始使用 firebase 存储和 firebase 功能。现在我一直在开发从功能到存储的文件上传。 我已经让它工作了(上传完成并且文件出现在存储部分),但是,图像永远保持这样(永远在右侧加载)
我想只允许用户将文件上传到他们自己的存储桶中,最大文件大小为 1MB,仍然允许他们删除文件。我添加了以下内容: match /myusers/{userId}/{allPaths=**} { al
使用生命周期管理策略将容器的内容从冷访问层移动到存档。我正在尝试以下策略,希望它能在一天后将该容器中的所有文件移动到存档层,但事实并非如此在职的。我设置了选择标准“一天未使用后”。 这是 json 代
对于连接到 Azure 存储端点,有 http 和 https 两个选项。 第一。 https 会带来开销,可能是 5%-10%,但我不支付同一个数据中心的费用。 第二。 http 更快,但 Auth
有人可以帮我理解这一点吗?我创建了Virtual Machine in Azure running Windows Server 2012 。我注意到 Azure 自动创建了一个存储帐户。当我进入该存
我是一名优秀的程序员,十分优秀!