Python 如何限制桨叶运动?

Python 如何限制桨叶运动?,python,pygame,Python,Pygame,我想这样做,这样乒乓球游戏中的球拍就不会从游戏窗口的两侧移动。(不会向右或向左移动太远)。我试着写了一个解决方案,但是划桨不能完全离开屏幕,但是只有不到一半的划桨仍然能够离开屏幕 我如何修改我的代码,使挡板不会离开屏幕?游戏窗口设置为600乘600。我正在用更新桨来移动桨 以下是我的桨类/方法: import pygame class Paddle: def __init__(self, x, y, c, w, h): self.x = x self.y =

我想这样做,这样乒乓球游戏中的球拍就不会从游戏窗口的两侧移动。(不会向右或向左移动太远)。我试着写了一个解决方案,但是划桨不能完全离开屏幕,但是只有不到一半的划桨仍然能够离开屏幕

我如何修改我的代码,使挡板不会离开屏幕?游戏窗口设置为600乘600。我正在用更新桨来移动桨

以下是我的桨类/方法:

import pygame

class Paddle:
   def __init__(self, x, y, c, w, h):
       self.x = x
       self.y = y
       self.color = c
       self.width = w
       self.height = h

   def draw_paddle(self, screen):
      pygame.draw.rect(screen, self.color,
         pygame.Rect(self.x, self.y, self.width, self.height), 0)

   def update_paddle(self, dir, dx):
      if (dir == 'left') and (self.x >= 0):
         self.x = self.x - dx
      elif (dir == 'right') and (self.x + self.width <= 600):
         self.x += dx

   def get_left(self):
      if (self.x < 300):
         return self.x

   def get_right(self):
      if (self.x >= 300):
         return self.x
导入pygame
班级桨:
定义初始(self,x,y,c,w,h):
self.x=x
self.y=y
self.color=c
自宽=w
自高度=h
def牵引桨(自身,屏幕):
pygame.draw.rect(屏幕、自身颜色、,
pygame.Rect(self.x,self.y,self.width,self.height),0)
def更新_挡板(自身、方向、dx):
如果(dir=='left')和(self.x>=0):
self.x=self.x-dx
elif(dir=='right')和(self.x+self.width=300):
返回self.x

您的末端位置需要在右侧测试self.x+self.width/2,在左侧测试self.x-self.width/2,在顶部和底部测试self.y和self.height/2。这是基于self.x,self.y是桨的中心。由于您防止挡板的中心离开屏幕,因此您将看到它位于边缘,使挡板的其余部分离开屏幕。

您的末端位置需要测试self.x+self.width/2(右侧)和self.x-self.width/2(左侧)以及self.y和self.height/2(顶部和底部)。这是基于self.x,self.y是桨的中心。由于您阻止挡板的中心离开屏幕,因此您会发现它位于边缘,将挡板的其余部分留在屏幕之外。

问题是您正在测试是否已经离开屏幕,而不是测试移动后是否将离开屏幕。如果
self.x
为1,并且向左移动了10个像素,那么当前代码将以-9结束

def update_paddle(self, dir, dx):
   if (dir == 'left'): # always move by dx, even if that moves you off the screen
      self.x -= dx
   else: # (dir == 'right')
      self.x += dx

   self.x = min(max(self.x, 0), 600 - self.width) # clamp to screen afterwards
我建议无条件地采取行动,然后将你的位置固定在屏幕上

def update_paddle(self, dir, dx):
   if (dir == 'left'): # always move by dx, even if that moves you off the screen
      self.x -= dx
   else: # (dir == 'right')
      self.x += dx

   self.x = min(max(self.x, 0), 600 - self.width) # clamp to screen afterwards

与屏幕边界问题无关,我还建议让负数
dx
表示向左移动,而不是使用
dir
参数。函数的第一部分是双向的
self.x+=dx

问题是,您正在测试是否已经离开屏幕,而不是测试移动后是否将离开屏幕。如果
self.x
为1,并且向左移动了10个像素,那么当前代码将以-9结束

我建议无条件地采取行动,然后将你的位置固定在屏幕上

def update_paddle(self, dir, dx):
   if (dir == 'left'): # always move by dx, even if that moves you off the screen
      self.x -= dx
   else: # (dir == 'right')
      self.x += dx

   self.x = min(max(self.x, 0), 600 - self.width) # clamp to screen afterwards

与屏幕边界问题无关,我还建议让负数
dx
表示向左移动,而不是使用
dir
参数。函数的第一部分是两个方向的self.x+=dx。

我们想说的是,拨片位置(其左上角,因为其位置应确保拨片永远不会离开屏幕,或者更准确地说:

  • 挡板左侧的移动距离不得超过屏幕左边缘,且
  • 挡板右侧的移动不得超过屏幕的右边缘
因此,让我们看看左右调整拨杆位置的方法。这只是
更新_-paile()
,它有单独的左右移动路径。让我们看看您的拨杆有什么问题,从左侧开始:

if (dir == 'left') and (self.x >= 0):
    self.x = self.x - dx
x
正好为零时,所有测试都通过,因此可以让挡板进一步向左移动,它可能会稍微离开屏幕;我们不希望这样,我们希望它完全不离开屏幕。更好的做法是让它一直移动到左边缘,然后保持在屏幕上:

if dir == 'left':
    self.x = max(self.x - dx, 0)
同样的问题也发生在右转运动中:

else:  # LEM: dir == 'right':
   self.x = min(self.x + dx, 600 - self.width)

我们想说的是,拨片位置(其顶部,左角,因为)的位置应确保拨片永远不会离开屏幕,或者更准确地说:

  • 挡板左侧的移动距离不得超过屏幕左边缘,且
  • 挡板右侧的移动不得超过屏幕的右边缘
因此,让我们看看左右调整拨杆位置的方法。这只是
更新_-paile()
,它有单独的左右移动路径。让我们看看您的拨杆有什么问题,从左侧开始:

if (dir == 'left') and (self.x >= 0):
    self.x = self.x - dx
x
正好为零时,所有测试都通过,因此可以让挡板进一步向左移动,它可能会稍微离开屏幕;我们不希望这样,我们希望它完全不离开屏幕。更好的做法是让它一直移动到左边缘,然后保持在屏幕上:

if dir == 'left':
    self.x = max(self.x - dx, 0)
同样的问题也发生在右转运动中:

else:  # LEM: dir == 'right':
   self.x = min(self.x + dx, 600 - self.width)

它不应该是
self.x+self.width+dx=0
?它不应该是
self.x+self.width+dx=0
?谢谢你的额外解释!谢谢你的额外解释!