首页 > 解决方案 > builtins.TypeError:需要一个整数(获取类型元组)

问题描述

我试图为 pygame 创建两个矩形。但是,“它说builtins.TypeError:需要一个整数(获取类型元组)”这是我的代码。

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('A template for graphical games with two moving Balls')

   # 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
   pygame.draw.rect(w_surface,pygame.Color('yellow'),[400,200],(80,30))
   pygame.draw.rect(w_surface,pygame.Color('yellow'),[100,200],(80,30))     
   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

      # === objects that are part of every game that we will discuss
      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

      # === game specific objects
      self.small_Ball = Ball('red', 30, [50, 50], [1, 2], self.surface)
      #self.big_Ball = Ball('blue', 40, [200, 100], [2, 1], self.surface)
      self.max_frames = 150
      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:  # until player clicks close box
         # play frame
         self.handle_events()
         self.draw()
         #pygame.draw.rect(surface,pygame.Color('yellow'),[400,200],[80,30])
         #pygame.draw.rect(surface,pygame.Color('yellow'),[100,200],[80,30])         
         if self.continue_game:
            self.update()
            self.decide_continue()
         self.game_Clock.tick(self.FPS) # run at most with FPS Frames Per Second 

   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:
         if event.type == pygame.QUIT:
            self.close_clicked = True

   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.small_Ball.draw()
      #self.big_Ball.draw()
      pygame.display.update() # make the updated surface appear on the display

   def update(self):
      # Update the game objects.
      # - self is the Game to update

      self.small_Ball.move()
      #self.big_Ball.move()
      #self.frame_counter = self.frame_counter + 1

   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 Ball:
   # An object in this class represents a Ball that moves 

   def __init__(self, Ball_color, Ball_radius, Ball_center, Ball_velocity, surface):
      # Initialize a Ball.
      # - self is the Ball to initialize
      # - color is the pygame.Color of the Ball
      # - center is a list containing the x and y int
      #   coords of the center of the Ball
      # - radius is the int pixel radius of the Ball
      # - velocity is a list containing the x and y components
      # - surface is the window's pygame.Surface object

      self.color = pygame.Color(Ball_color)
      self.radius = Ball_radius
      self.center = Ball_center
      self.velocity = Ball_velocity
      self.surface = surface

   def move(self):
      # Change the location of the Ball by adding the corresponding 
      # speed values to the x and y coordinate of its center
      # - self is the Ball

      size=self.surface.get_size()#size is a tuple(width,height)
      for index in range(0,2):#Gives you a sequence of integers in the  range
         self.center[index]=self.center[index]+self.velocity[index]
         if self.center[index]<self.radius or self.center[index]+self.radius>size[index]:
             self.velocity[index]=-self.velocity[index]

   def draw(self):
      # Draw the Ball on the surface
      # - self is the Ball

      pygame.draw.circle(self.surface, self.color, self.center, self.radius)


main()

如果我单独运行它,它工作得很好。这是我的第一个 pygame 测试程序的一部分。

r_colour=pygame.Color('Green')
r_top_left_corner=[400,10]
r_width_height=[80,30]
r2_colour=pygame.Color('white')
r2_top_left_corner=[400,200]
r2_width_height=[5,100]
rectangle2=pygame.Rect(r2_top_left_corner,r2_width_height)
rectangle=pygame.Rect(r_top_left_corner,r_width_height)
pygame.draw.rect(w_surface,r_colour,rectangle)
pygame.draw.rect(w_surface,r2_colour, rectangle2

我尝试使用与我的第一个测试程序相同的方法创建两个矩形,但失败了。谁能告诉我为什么?

标签: pythonpygame

解决方案


矩形必须在一个参数中进行编码。您尝试为位置传递一个参数,并为宽度和高度传递一个单独的参数:

pygame.draw.rect(w_surface,pygame.Color('yellow'),[400,200],(80,30)) 
pygame.draw.rect(w_surface,pygame.Color('yellow'),[100,200],(80,30))  

这就是导致错误的原因。

第三个参数pygame.draw.rect必须是一个pygame.Rect对象

pygame.draw.rect(w_surface, pygame.Color('yellow'), pygame.Rect(400,200,80,30))
pygame.draw.rect(w_surface, pygame.Color('yellow'), pygame.Rect(100,200,80,30)) 

甚至可以为 x 和 y 位置分别传递宽度和高度的 4 个分量的元组:

pygame.draw.rect(w_surface, pygame.Color('yellow'), (400,200,80,30))
pygame.draw.rect(w_surface, pygame.Color('yellow'), (100,200,80,30)) 

或者

pygame.draw.rect(w_surface, pygame.Color('yellow'), ((400,200), (80,30)))
pygame.draw.rect(w_surface, pygame.Color('yellow'), ((100,200), (80,30)))

推荐阅读