Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更新rect对象pygame时出现问题_Python_Python 3.x_Pygame_Mouseevent_Rect - Fatal编程技术网

Python 更新rect对象pygame时出现问题

Python 更新rect对象pygame时出现问题,python,python-3.x,pygame,mouseevent,rect,Python,Python 3.x,Pygame,Mouseevent,Rect,我是新的编码和我的游戏有问题。我想白色矩形改变为一个新的颜色时,点击。如果它们不是白色且被点击,则不应改变颜色。现在它们只是保持白色,所以我不确定它们是否没有更新,或者我的鼠标点击检测是否错误。非常感谢您的帮助,谢谢 # Color Matching Game # Click a box when it is white to change it to a random color # If the two colors match you gain a point import pygame

我是新的编码和我的游戏有问题。我想白色矩形改变为一个新的颜色时,点击。如果它们不是白色且被点击,则不应改变颜色。现在它们只是保持白色,所以我不确定它们是否没有更新,或者我的鼠标点击检测是否错误。非常感谢您的帮助,谢谢

# Color Matching Game
# Click a box when it is white to change it to a random color
# If the two colors match you gain a point

import pygame


# User-defined functions

def main():
   # initialize all pygame modules (some need initialization)
   pygame.init()
   # create a pygame display window
   pygame.display.set_mode((500, 400))
   # set the title of the display window
   pygame.display.set_caption('Color Match')   
   # get the display surface
   w_surface = pygame.display.get_surface() 
   # create a game object
   game = Game(w_surface)
   # start the main game loop by calling the play method on the game object
   game.play() 
   # quit pygame and clean up the pygame window
   pygame.quit() 


# User-defined classes

class Game:
   # An object in this class represents a complete game.

   def __init__(self, surface):
      # Initialize a Game.
      # - self is the Game to initialize
      # - surface is the display window surface object

      # Initializa game
      self.surface = surface
      self.bg_color = pygame.Color('black')
      
      self.FPS = 60
      self.game_Clock = pygame.time.Clock()
      self.close_clicked = False
      self.continue_game = True
      
      # Initialize game specific objects
      self.left_rect = Rect('white', (87.5, 100, 75, 200), self.surface)
      self.right_rect = Rect('white', (337.5, 100, 75, 200), self.surface)
      self.max_frames = 10000
      self.frame_counter = 0
      

   def play(self):
      # Play the game until the player presses the close box.
      # - self is the Game that should be continued or not.

      while not self.close_clicked:
         # play frame
         self.handle_events()
         self.draw()            
         if self.continue_game:
            self.decide_continue()
         self.game_Clock.tick(self.FPS) 
         
   def handle_events(self):
      # Handle each user event by changing the game state appropriately.
      # - self is the Game whose events will be handled

      events = pygame.event.get()
      for event in events:
         
         # Detects if user exits out
         if event.type == pygame.QUIT:
            self.close_clicked = True
         
         # Detects if the user clicks M1
         if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            self.mouse_pos = pygame.mouse.get_pos()
      
               
   def draw(self):
      # Draw all game objects.
      # - self is the Game to draw
      
      self.surface.fill(self.bg_color) # clear the display surface first
      self.left_rect.draw()
      self.right_rect.draw()
      pygame.display.update() # make the updated surface appear on the display

   def decide_continue(self):
      # Check and remember if the game should continue
      # - self is the Game to check
      
      if self.frame_counter > self.max_frames:
         self.continue_game = False


class Rect:
   # An object in this class represents a rect
   
   def __init__(self, rect_color, rect_center, surface):
      # Initialize a rect.
      # - self is the rect to initialize
      # - color is the pygame.Color of the rect
      # - center is a list containing the x and y int coords
      # - surface is the window's pygame.Surface object

      self.color = pygame.Color(rect_color)
      self.center = rect_center
      self.surface = surface
      self.colors = ['red', 'blue', 'green', 'yellow']
   
   def update(self, mouse_pos):
      # Updates rectangle to new colour if it is white and detects a mouse click
      if self.color == 'white' and self.rect.collidepoint(play.mouse_pos):
         self.color = pygame.Color(self.colors[randint(0, 3)])  
      
   
   def draw(self):
      # Draw the rect on the surface
      # - self is the rect
      
      pygame.draw.rect(self.surface, self.color, self.center)


main()

您没有调用
更新
方法:

职业游戏:
# [...]
def处理_事件(自):
#通过适当更改游戏状态来处理每个用户事件。
#-赛尔夫是将处理其事件的游戏
events=pygame.event.get()
对于事件中的事件:
# [...]
#检测用户是否单击M1
如果event.type==pygame.MOUSEBUTTONDOWN和event.button==1:
self.mouse\u pos=pygame.mouse.get\u pos()

self.left_rect.update(self.mouse_pos)#在句柄事件中,您在哪里更改rect的颜色?@AdityaGupta将其放入更新下的rect类中。是否应该在handle_events中?请尝试在handle events中调用Rect类的update函数感谢您的响应。我知道我忘了一些“小”事情。我想知道如何比较这两个矩形,看它们的颜色是否匹配?如果你能给我指出正确的方向那就太好了@Liam
self.left\u rect.color==self.right\u rect.color