【问题标题】:Getting a weird order problem when running this script [duplicate]运行此脚本时出现奇怪的顺序问题[重复]
【发布时间】:2021-11-09 12:10:27
【问题描述】:

所以我正在为 sea jam 制作一个游戏,并且我一直在运行我的两个按钮时遇到这个错误,我认为它不是函数或类之一,但如果它是请分享我需要在 29 日之前完成这个,这是我的第一次果酱,这里是代码:

import pygame

sh = 400
sw = 400

pygame.font.init() # you have to call this at the start, 
                   # if you want to use this module.


screen = pygame.display.set_mode((sh, sw))
b = pygame.image.load("b.png").convert_alpha()
ub = pygame.image.load("ub.png").convert_alpha()
scr = 0


class ubut():
  def __init__(self,x,y,image):
    self.image = image
    self.rect = self.image.get_rect()
    self.rect.topleft = (x, y)
  def draw(self):
    screen.blit(self.image, (self.rect.x, self.rect.y))
    mpos = pygame.mouse.get_pos()
    global scr
    for event in pygame.event.get():
      if event.type == pygame.MOUSEBUTTONUP:
        if self.rect.collidepoint(mpos):
          print("x")


class but():
  def __init__(self,x,y,image):
    self.image = image
    self.rect = self.image.get_rect()
    self.rect.topleft = (x, y)
  def draw(self):
    screen.blit(self.image, (self.rect.x, self.rect.y))
    mpos = pygame.mouse.get_pos()
    for event in pygame.event.get():
      if event.type == pygame.MOUSEBUTTONUP:
        if self.rect.collidepoint(mpos):
          global scr
          scr += 1
          print(scr)
  def redb(self):
        mpos = pygame.mouse.get_pos()
        for event in pygame.event.get():
          if event.type == pygame.MOUSEBUTTONUP:
            if self.rect.collidepoint(mpos):
              global scr
              scr += 1
              print(scr)
  def purpb(self):
        mpos = pygame.mouse.get_pos()
        for event in pygame.event.get():
          if event.type == pygame.MOUSEBUTTONUP:
            if self.rect.collidepoint(mpos):
              print("x")




buttton = but(125,125 , b)
ubutton = but(125,225,ub)
run = True

while run:
  screen.fill((0,255,0))
  ubutton.purpb()
  ubutton.draw()
  buttton.draw()
  buttton.redb()
  
  mpos = pygame.mouse.get_pos()
  
  pygame.display.update()

感谢您的帮助。

【问题讨论】:

    标签: python button pygame


    【解决方案1】:

    pygame.event.get()](https://www.pygame.org/docs/ref/event.html#pygame.event.get) 获取所有消息并将它们从队列中删除。请参阅文档:

    这将获取所有消息并将它们从队列中删除。 [...]

    如果在多个事件循环中调用pygame.event.get(),则只有一个循环接收事件。结果,似乎错过了一些事件。每帧获取一次事件并将事件列表传递给处理它们的方法:

    class ubut():
      # [...]
      def draw(self, event_list):
        screen.blit(self.image, (self.rect.x, self.rect.y))
        global scr
        for event in event_list:
          if event.type == pygame.MOUSEBUTTONUP:
            if self.rect.collidepoint(event.pos):
              print("x")
    
    
    class but():
      # [...]
    
      def draw(self, event_list):
        screen.blit(self.image, (self.rect.x, self.rect.y))
        for event in event_list:
          if event.type == pygame.MOUSEBUTTONUP:
            if self.rect.collidepoint(event.pos):
              global scr
              scr += 1
              print(scr)
    
    while run:
      # [...]
    
      event_list = pygame.event.get()
      ubutton.draw(event_list)
      buttton.draw(event_list)
    
      # [...]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 2011-10-25
      • 1970-01-01
      相关资源
      最近更新 更多