我不明白为什么按钮不绘制在它之前工作的背景上
这不会显示错误。
这是代码------>
这段代码是错误的吗?我的问题是否可以重现?它可能在按钮的代码中,我将不胜感激
import pygame
pygame.init()
screen = pygame.display.set_mode((3840,2160))
running = True
mouse = pygame.mouse.get_pos()
pygame.display.set_caption("GermanBall")
bg = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\Tan.jpg")
icon = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\box.png")
button1 = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\shirt.png").convert_alpha()
class Button():
def __init__(self,x,y,image, scale):
width = image.get_width()
height = image.get_height()
self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
self.rect = self.image.get_rect()
self.rect.topleft = (x,y)
posmoose = pygame.mouse.get_pos()
if self.rect.collidepoint(posmoose):
if pygame.mouse.get_pressed()[0] == 1:
print("click")
def draw(self):
screen.blit(self.image,(self.rect.x,self.rect.y))
stat = Button(1550,700,button1,0.5)
pygame.display.set_icon(icon)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.blit(bg,(0,0))
stat.draw()
pygame.display.update()
你的类中的缩进是错误的:方法draw
, intro
, ... 应该处于同一水平__init__
。但基本上你的类是无用的,因为你没有创建该类的任何对象。你应该有类似的东西my_button = Button(...)
在您的代码中(以及类本身之外)。
将你的班级提升到最高水平也是一个很好的做法。
所以也许你应该从这样的事情开始:
import pygame
pygame.init()
screen = pygame.display.set_mode((3840,2160))
running = True
mouse = pygame.mouse.get_pos()
pygame.display.set_caption("GermanBall")
bg = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\Tan.jpg")
icon = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\box.png")
button1 = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\shirt.png").convert_alpha()
pygame.display.set_icon(icon)
class Button():
def __init__(self,x,y,image, scale):
...
def draw(self):
...
def intro(self):
...
def game(self):
...
def manager():
...
my_button = Button(100, 200, button1, 1.0) # <- create a button
while running == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.blit(bg,(0,0))
my_button.draw() # <- draw the button
pygame.display.update()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)