Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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 乒乓球在墙上和桨上弹跳_Python_Pygame_Pong - Fatal编程技术网

Python 乒乓球在墙上和桨上弹跳

Python 乒乓球在墙上和桨上弹跳,python,pygame,pong,Python,Pygame,Pong,大家好,我对python非常陌生,我一直在努力探测墙壁(屏幕的边缘),让我的球在撞击墙壁时弹起来。我一直在寻找如何使用它,但大多数人使用海龟或其他一些我不能使用的东西,所以它没有多大帮助。我得到了如何检测墙的算法,但不知道如何正确编写代码。下面是我的代码,如果有人能告诉我如何为此编写代码,我将不胜感激 这就是我一直从第62行收到的错误消息。 错误消息: 这是完整的代码 import pygame from random import randint # User-defined functi

大家好,我对python非常陌生,我一直在努力探测墙壁(屏幕的边缘),让我的球在撞击墙壁时弹起来。我一直在寻找如何使用它,但大多数人使用海龟或其他一些我不能使用的东西,所以它没有多大帮助。我得到了如何检测墙的算法,但不知道如何正确编写代码。下面是我的代码,如果有人能告诉我如何为此编写代码,我将不胜感激

这就是我一直从第62行收到的错误消息。 错误消息:

这是完整的代码

import pygame
from random import randint


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

      # === 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.ball = Ball('white', 5, [250, 200], [1, 2], 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()   
         self.ballrect = self.ball.get_rect()
         self.ballrect = ballrect.move(speed)
         if self.ballrect.left < 0 or self.ballrect.right > 500:
            speed[0] = -speed[0]
         if self.ballrect.top < 0 or self.ballrect.bottom > 400:
            speed[1] = -speed[1]          
         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
      pygame.draw.rect(self.surface, (255, 255, 255), (75, 190, 5, 20))
      pygame.draw.rect(self.surface, (255, 255, 255), (420, 190, 5, 20))       
      self.ball.draw()
      pygame.display.update() # make the updated surface appear on the display


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

      self.ball.move()
      self.frame_counter += self.frame_counter 




   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 Dot that moves 

   def __init__(self, ball_color, ball_radius, ball_center, ball_velocity, 
                surface):
      # Initialize a Dot.
      # - self is the Dot to initialize
      # - color is the pygame.Color of the dot
      # - center is a list containing the x and y int
      #   coords of the center of the dot
      # - radius is the int pixel radius of the dot
      # - 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 Dot by adding the corresponding 
      # speed values to the x and y coordinate of its center
      # - self is the Dot

      for i in range(0,2):
         self.center[i] = (self.center[i] + self.velocity[i])
   def update(self):
      self.rect.x += self.velocity[0]
      self.rect.y += self.velocity[1]

   def bounce(self):
      self.velocity[0] = -self.velocity[0]
      self.velocity[1] = randint(-8,8)


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

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






main()
导入pygame
从随机导入randint
#用户定义函数
def main():
#初始化所有pygame模块(有些模块需要初始化)
pygame.init()
#创建pygame显示窗口
pygame.display.set_模式((500400))
#设置显示窗口的标题
pygame.display.set_标题('Pong')
#获取显示表面
w_surface=pygame.display.get_surface()
#创建一个游戏对象
游戏=游戏(w_表面)
#通过调用游戏对象上的play方法启动主游戏循环
游戏
#退出pygame并清理pygame窗口
pygame.quit()
#用户定义类
班级游戏:
#此类中的对象表示一个完整的游戏。
def u u初始(自,表面):
#初始化游戏。
#-self是要初始化的游戏
#-曲面是显示窗口曲面对象
#==我们将讨论的每个游戏中的对象
self.surface=曲面
self.bg_color=pygame.color('black'))
self.FPS=60
self.game\u Clock=pygame.time.Clock()
self.close\u clicked=False
self.continue\u game=True
#==游戏特定对象
self.ball=球('white',5,[250200],[1,2],self.surface)
self.max_帧=150
self.frame_计数器=0
def播放(自我):
#玩游戏直到玩家按下关闭框。
#-赛尔夫是一个游戏,该继续还是不该继续。
而非self.close_单击:#直到玩家单击关闭框
#游戏框架
self.handle_事件()
self.draw()
self.ballrect=self.ball.get_rect()
self.ballrect=ballrect.move(速度)
如果self.ballrect.left<0或self.ballrect.right>500:
速度[0]=-速度[0]
如果self.ballrect.top<0或self.ballrect.bottom>400:
速度[1]=-速度[1]
如果self.continue\u游戏:
self.update()
self.decision_continue()
self.game_Clock.tick(self.FPS)#每秒最多运行FPS帧
def处理_事件(自):
#通过适当更改游戏状态来处理每个用户事件。
#-赛尔夫是将处理其事件的游戏
events=pygame.event.get()
对于事件中的事件:
如果event.type==pygame.QUIT:
self.close\u clicked=True
def牵引(自):
#绘制所有游戏对象。
#-赛尔夫是要抽签的游戏
self.surface.fill(self.bg_color)#首先清除显示表面
pygame.draw.rect(self.surface,(255,255,255),(75190,5,20))
pygame.draw.rect(self.surface,(255,255,255),(420,190,5,20))
self.ball.draw()
pygame.display.update()#使更新后的曲面显示在显示屏上
def更新(自我):
#更新下一帧的游戏对象。
#-赛尔夫是需要更新的游戏
self.ball.move()
self.frame\u计数器+=self.frame\u计数器
def decision_continue(self):
#检查并记住游戏是否应该继续
#-赛尔夫是要检查的游戏
如果self.frame\u计数器>self.max\u帧:
self.continue\u game=False
班级舞会:
#此类中的对象表示移动的点
定义初始(自身、球颜色、球半径、球中心、球速度、,
表面):
#初始化一个点。
#-self是要初始化的点
#-颜色就是游戏的颜色点的颜色
#-中心是包含x和y int的列表
#圆心坐标
#-半径是点的整数像素半径
#-velocity是包含x和y分量的列表
#-surface是窗口的pygame.surface对象
self.color=pygame.color(球的颜色)
自半径=球半径
self.center=ball\u center
自速度=球的速度
self.surface=曲面
def移动(自我):
#通过添加相应的
#将速度值设置为其中心的x和y坐标
#-自我就是点
对于范围(0,2)内的i:
自中心[i]=(自中心[i]+自速度[i])
def更新(自我):
self.rect.x+=自速度[0]
自校正y+=自速度[1]
def反弹(自我):
自速度[0]=-自速度[0]
自速度[1]=randint(-8,8)
def牵引(自):
#在表面上画点
#-自我就是点
pygame.draw.circle(self.surface、self.color、self.center、self.radius)
main()

您的Ball类需要一个rect属性

class Ball:
   # An object in this class represents a Dot that moves 

   def __init__(self, ball_color, ball_radius, ball_center, ball_velocity, 
                surface):
      # Initialize a Dot.
      # - self is the Dot to initialize
      # - color is the pygame.Color of the dot
      # - center is a list containing the x and y int
      #   coords of the center of the dot
      # - radius is the int pixel radius of the dot
      # - 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

      # create a Rect with width and height equal to radius,
      # then place it at the center of the ball
      self.rect = pygame.Rect(0, 0, self.radius, self.radius)
      self.rect.center = self.center
此外,在更新中,您需要再次将
self.rect.center
设置为
self.center
,否则它将保持不变。
或者您完全去掉了
self.center
,只使用
self.rect.center
,因为这是冗余信息。

您的Ball类需要一个rect属性

class Ball:
   # An object in this class represents a Dot that moves 

   def __init__(self, ball_color, ball_radius, ball_center, ball_velocity, 
                surface):
      # Initialize a Dot.
      # - self is the Dot to initialize
      # - color is the pygame.Color of the dot
      # - center is a list containing the x and y int
      #   coords of the center of the dot
      # - radius is the int pixel radius of the dot
      # - 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

      # create a Rect with width and height equal to radius,
      # then place it at the center of the ball
      self.rect = pygame.Rect(0, 0, self.radius, self.radius)
      self.rect.center = self.center
此外,在更新中,您需要再次将
self.rect.center
设置为
self.center
,否则它将保持不变。
或者您完全去掉了
self.center
,只使用
self.rect.center
,因为这是冗余信息。

错误是,您试图对类对象
Ball
执行方法
get_rect
,而该类对象未满足此条件