- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已经完成了游戏的主要代码,并开始制作菜单屏幕。我可以很好地在屏幕上显示按钮,但是当我点击某个地方时,我得到这个 Error :
我该如何解决这个问题?如果我在这个问题上没有说清楚,请告诉我,以便我澄清。谢谢!
这是我的菜单屏幕代码:
import pygame
import random
import time
pygame.init()
#colours
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,155,0)
blue = (50,50,155)
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Numeracy Ninjas')
clock = pygame.time.Clock()
#Fonts
smallfont = pygame.font.SysFont("comicsansms", 25)
medfont = pygame.font.SysFont("comicsansms", 50)
largefont = pygame.font.SysFont("comicsansms", 75)
#Sprites
img_button_start = pygame.image.load('Sprites/Buttons/button_start.png')
img_button_options = pygame.image.load('Sprites/Buttons/button_options.png')
gameDisplay.fill(white)
pygame.display.update()
class Button(pygame.sprite.Sprite):
def __init__(self, image, buttonX, buttonY):
super().__init__()
gameDisplay.blit(image, (buttonX, buttonY))
pygame.display.update()
selfrect = image.get_rect()
def wasClicked(event):
if selfrect.collidepoint(event.pos):
return True
def gameIntro():
buttons = pygame.sprite.Group()
button_start = Button(img_button_start, 27, 0)
button_options = Button(img_button_options, 27, 500)
buttons.add(button_start)
buttons.add(button_options)
print(buttons)
#main game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
print(event.pos)
#check for every button whether it was clicked
for btn in buttons:
print('forbtninbuttons')
if btn.wasClicked():
print('clicked!')
if event.type == pygame.QUIT:
pygame.quit()
最佳答案
你还没有为你的类声明任何属性,只是局部变量。尝试在您的初始化程序和您的 wasClicked(event)
方法中执行 self.selfrect = image.get_rect()
:
def wasClicked(self, event):
if self.selfrect.collidepoint(event.pos):
return True
不过通常习惯将您的 rect 变量命名为 rect。
class Button(pygame.sprite.Sprite):
def __init__(self, image, buttonX, buttonY):
super().__init__()
# This code doesn't make sense here. It should be inside your game loop.
# gameDisplay.blit(image, (buttonX, buttonY))
# pygame.display.update()
self.image = image # It's usually good to have a reference to your image.
self.rect = image.get_rect()
def wasClicked(self, event):
if self.rect.collidepoint(event.pos):
return True
else:
return False
关于python - Pygame Button getRect Collidepoint 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38839401/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!