Python TypeError:参数必须是rect样式的对象。我希望程序在子弹击中正确的玩家时停止

Python TypeError:参数必须是rect样式的对象。我希望程序在子弹击中正确的玩家时停止,python,Python,试图让游戏在子弹击中正确的玩家时停止,但程序不会启动,并会给我信息 我不知道102号线上的问题是什么,但它显然似乎是问题所在 文件“c:/Users/teamb/Desktop/PythonWorkspace/myGame/myGame.py”,第102行,在 如果右击球员右击(子弹): TypeError:参数必须是rect样式的对象 导入pygame pygame.init() screen_width = 1280 screen_height = 720 window = pygame.

试图让游戏在子弹击中正确的玩家时停止,但程序不会启动,并会给我信息

我不知道102号线上的问题是什么,但它显然似乎是问题所在

文件“c:/Users/teamb/Desktop/PythonWorkspace/myGame/myGame.py”,第102行,在 如果右击球员右击(子弹): TypeError:参数必须是rect样式的对象

导入pygame

pygame.init()

screen_width = 1280
screen_height = 720
window = pygame.display.set_mode((screen_width, screen_height),0,0)

pygame.display.set_caption("PongGame")

clock = pygame.time.Clock()
#background
background = pygame.image.load("C:/Users/teamb/Desktop/PythonWorkspace/myGame/background.png")

#paddle image
paddle = pygame.image.load("C:/Users/teamb/Desktop/PythonWorkspace/myGame/paddle.png")
paddle_size = paddle.get_rect().size

#left paddle
left_paddle_width = paddle_size[0]
left_paddle_height = paddle_size[1]
left_paddle_xpos = 10
left_paddle_ypos = (screen_height/2 - left_paddle_height/2)

to_x = 0
to_y = 0

character_speed = 10

right_player = pygame.image.load("C:/Users/teamb/Desktop/PythonWorkspace/myGame/right_player.png")
right_player_size = right_player.get_rect().size
#right paddle
right_player_width = right_player_size[0]
right_player_height = right_player_size[1]
right_player_xpos = screen_width - right_player_width - 10
right_player_ypos = (screen_height/2 - left_paddle_height/2)

to_y_2 = 0
# ball
bullet = pygame.image.load("C:/Users/teamb/Desktop/PythonWorkspace/myGame/ball.png")
bullet_size = bullet.get_rect().size
bullet_width = bullet_size[0]
#make the bullet be able to shoot multiple at the same time
bullets = []
#bulllet speed
bullet_speed = 15


running = True
while running:
   dt = clock.tick(60)
   
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         running = False
   #left paddle
      if event.type == pygame.KEYDOWN:
         if event.key == pygame.K_w:
            to_y -= character_speed
         elif event.key == pygame.K_s:
             to_y += character_speed
         elif event.key == pygame.K_SPACE:
            bullet_ypos = left_paddle_ypos + left_paddle_height/2 - 8
            bullet_xpos = left_paddle_xpos + left_paddle_width - 20
            bullets.append([bullet_xpos, bullet_ypos])

      if event.type == pygame.KEYUP:
         if event.key == pygame.K_w or event.key == pygame.K_s:
            to_y = 0
   #right paddle
      if event.type == pygame.KEYDOWN:
         if event.key == pygame.K_UP:
            to_y_2 -= character_speed
         elif event.key == pygame.K_DOWN:
             to_y_2 += character_speed

      if event.type == pygame.KEYUP:
         if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
            to_y_2 = 0

   left_paddle_ypos += to_y
   right_player_ypos += to_y_2

   bullets = [ [w[0] + bullet_speed, w[1]] for w in bullets] #shoot the bullet 


   #barrier for the sprites to not go outside the screen.
   if left_paddle_ypos < 0:
      left_paddle_ypos =  0
   
   if left_paddle_ypos > screen_height - left_paddle_height:
      left_paddle_ypos = screen_height - left_paddle_height

   left_paddle_rect = paddle.get_rect()
   left_paddle_rect.left = left_paddle_xpos
   left_paddle_rect.top = left_paddle_ypos

   right_player_rect = right_player.get_rect()
   right_player_rect.left = right_player_xpos
   right_player_rect.top = right_player_ypos

   if right_player_rect.colliderect(bullets):
      running = False
   

   window.blit(background,(0,0))
   
   window.blit(paddle, (left_paddle_xpos,left_paddle_ypos))
   window.blit(right_player, (right_player_xpos, right_player_ypos))

   for bullet_xpos, bullet_ypos in bullets:      
      window.blit(bullet, (bullet_xpos, bullet_ypos))

   pygame.display.update()

pygame.quit()
pygame.init()
屏幕宽度=1280
屏幕高度=720
window=pygame.display.set_模式((屏幕宽度、屏幕高度),0,0)
pygame.display.set_标题(“PongGame”)
clock=pygame.time.clock()
#背景
background=pygame.image.load(“C:/Users/teamb/Desktop/PythonWorkspace/myGame/background.png”)
#桨叶图像
blade=pygame.image.load(“C:/Users/teamb/Desktop/PythonWorkspace/myGame/blade.png”)
桨叶大小=桨叶。获取正确的大小()
#左桨
左桨叶宽度=桨叶大小[0]
左桨高度=桨大小[1]
左桨\u xpos=10
左桨叶高度=(屏幕高度/2-左桨叶高度/2)
to_x=0
to_y=0
字符速度=10
right\u player=pygame.image.load(“C:/Users/teamb/Desktop/PythonWorkspace/myGame/right\u player.png”)
right\u player\u size=right\u player.get\u rect().size
#右桨
右\u播放器\u宽度=右\u播放器\u大小[0]
right\u player\u height=right\u player\u size[1]
右\u播放器\u xpos=屏幕宽度-右\u播放器\u宽度-10
右棋手(屏幕高度/2-左棋盘高度/2)
to_y_2=0
#球
bullet=pygame.image.load(“C:/Users/teamb/Desktop/PythonWorkspace/myGame/ball.png”)
bullet\u size=bullet.get\u rect().size
项目符号宽度=项目符号尺寸[0]
#使子弹能够同时射击多个目标
项目符号=[]
#小牛速度
子弹头速度=15
运行=真
运行时:
dt=时钟滴答声(60)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
#左桨
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_w:
to_y-=字符速度
elif event.key==pygame.K_:
to_y+=字符速度
elif event.key==pygame.K_空间:
子弹头=左桨+左桨高度/2-8
子弹头=左桨叶+左桨叶宽度-20
bullet.append([bullet\u xpos,bullet\u ypos])
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_w或event.key==pygame.K_s:
to_y=0
#右桨
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_UP:
to_y_2-=字符速度
elif event.key==pygame.K_向下:
to_y_2+=字符速度
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_向上或event.key==pygame.K_向下:
to_y_2=0
左桨ypos+=至y
右\u玩家\u ypos+=到\u y\u 2
子弹=[[w[0]+子弹的速度,w[1]]代表子弹中的w]#射出子弹
#精灵不出屏幕的屏障。
如果左桨叶低于0:
左_桨_ypos=0
如果左桨高度>屏幕高度-左桨高度:
左桨叶高度=屏幕高度-左桨叶高度
left_-pable_-rect=桨。get_-rect()
left_-paller_-rect.left=左_-paller_-xpos
左桨右上=左桨右下
right\u player\u rect=right\u player.get\u rect()
right\u player\u rect.left=right\u player\u xpos
right\u player\u rect.top=right\u player\u ypos
如果右击球员右击(子弹):
运行=错误
窗口光点(背景,(0,0))
blit(桨叶,(左桨叶,左桨叶)
blit(右玩家,(右玩家,右玩家)
对于项目符号XPO,项目符号中的项目符号YPO:
窗口。blit(bullet,(bullet\u xpos,bullet\u ypos))
pygame.display.update()
pygame.quit()

似乎
右\u播放器\u rect.collide rect(子弹)
调用期望
子弹
为“rect样式对象”,但事实并非如此。事实上,它根本不是单个对象,而是一个对象列表。我在想,也许你只需要单独碰撞每个项目符号,但项目符号似乎是一个2项列表,如
[bullet\u xpos,bullet\u ypos]
,在我看来,这不像是一个“rect样式的对象”,所以我认为这可能也不起作用。也许你需要将你的项目符号转换成“矩形样式的对象”,比如项目符号位置的一个像素正方形。