python-3.x “参数1必须是pygame.Surface,而不是pygame.Rect”我怎么修复这个,它在说什么?

gab6jxml  于 2023-02-06  发布在  Python
关注(0)|答案(1)|浏览(113)

我正在尝试做一个类似于饼干点击器的游戏,但是用铅笔。用pygame。

class pencilsDisplay():
  def __init__(self, x, y):
    self.x = x
    self.y = y
    self.height = 100
    self.length = 100

  def draw(self):
    font = pygame.font.Font('font/Gidole-Regular.ttf', 24)
    small_font = pygame.font.Font('font/Gidole-Regular.ttf', 24)

    PENCILS = font.render('{} Pencils'.format( int(user.pencils) ), True, WHITE)
    PENCILSPERSECOND = font.render('Per Second: {}'.format( int(user.pencils) ), True, WHITE)
    screen.blit(PENCILS.get_rect( center=( int(self.x + self.length/2),int(self.y + self.height/2) )))
    screen.blit(PENCILSPERSECOND.get_rect( center=( int(self.x + self.length/2),int(self.y + self.height/2) )))

pencil = MainPencil(100,100)
pencil_display = pencilsDisplay(100,0)

class Player:
  def __init__(self):
    self.pencils = 0
    self.pencilspersecond = 0

user = Player()

def draw():
  pencil.draw()
  pencil_display.draw()

running = True
while running:
  for event in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONDOWN:
      mouse_pos = event.pos
      if pencil.collidepoint(mouse_pos):
        user.score += 1
        pencil.animation_state = 1

    
    if event.type == pygame.QUIT:
      running = False

  draw()

我有加载窗口的代码,但是这是错误所指的区域。窗口中没有显示任何内容,我得到了这个。

File "main.py", line 82, in <module>
    draw()
  File "main.py", line 67, in draw
    pencil_display.draw()
  File "main.py", line 52, in draw
    screen.blit(PENCILS.get_rect( center=( int(self.x + self.length/2),int(self.y + self.height/2) )))
TypeError: argument 1 must be pygame.Surface, not pygame.Rect
exit status 1

我做错了什么?我是pygame的新手,所以任何解释我都会很感激。

qlzsbp2j

qlzsbp2j1#

pygame.Surface.blit的第一个参数是源 * 曲面 *:
screen.blit(PENCILS.get_rect( center=( int(self.x + self.length/2),int(self.y + self.height/2) )))

screen.blit(PENCILS, PENCILS.get_rect(center = (int(self.x + self.length/2), int(self.y + self.height/2))))

相关问题