【问题标题】:Trouble updating rect object pygame无法更新 rect 对象 pygame
【发布时间】:2020-11-24 04:43:08
【问题描述】:

我是编码新手,我的游戏遇到了问题。我希望白色矩形在单击时更改为新颜色。如果它们不是白色的并且被单击,它们不应改变颜色。现在它们只是保持白色,所以我不确定它们是否没有更新,或者我的鼠标点击检测是否错误。非常感谢任何帮助,谢谢!

# 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()

【问题讨论】:

  • 您在句柄事件中的哪个位置更改了矩形的颜色?
  • @AdityaGupta 将其放在更新下的 Rect 类中。应该在handle_events中吗?
  • 尝试在handle事件中调用Rect类的更新函数

标签: python python-3.x pygame mouseevent rect


【解决方案1】:

你错过了调用update方法:

class Game:
    # [...]

    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 the user clicks M1
         if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            self.mouse_pos = pygame.mouse.get_pos()
            self.left_rect.update(self.mouse_pos)                         # <----
            self.right_rect.update(self.mouse_pos)                        # <----

您需要将颜色与白色进行比较,而不是与字符串 'white' 进行比较:

self.color == 'white'

self.color == pygame.Color('white')

collidepointpygame.Rect 的一个方法。因此你需要创建一个pygame.Rect 对象:

import random
class Rect:
    # [...]

    def update(self, mouse_pos):
      # Updates rectangle to new colour if it is white and detects a mouse click
      rect = pygame.Rect(self.center)
      if self.color == pygame.Color('white') and rect.collidepoint(mouse_pos):
         self.color = pygame.Color(self.colors[random.randint(0, 3)])  

完整代码:

# 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
import random

# 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()
            self.left_rect.update(self.mouse_pos)
            self.right_rect.update(self.mouse_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
      rect = pygame.Rect(self.center)
      if self.color == pygame.Color('white') and rect.collidepoint(mouse_pos):
         self.color = pygame.Color(self.colors[random.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()

【讨论】:

  • 感谢您的回复。我看到我忘记了一堆“小”事情。我想知道如何比较两个矩形以查看它们的颜色是否匹配?如果你能指出我正确的方向那就太好了!
  • @Liam self.left_rect.color == self.right_rect.color
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-25
  • 1970-01-01
  • 2012-06-30
相关资源
最近更新 更多