Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.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 在pygame中实现Rect对象和球之间的碰撞检测功能 导入pygame,随机,时间 #我们调用所有其他函数的主函数,启动游戏循环,退出pygame并清理窗口。在内部,我们创建一个游戏对象,显示表面,并通过调用游戏对象上的play方法启动游戏循环。还有一个带有游戏标题的标题。 def main(): pygame.init() 大小=(500400) surface=pygame.display.set_模式(大小) pygame.display.set_标题('pongv2') 游戏=游戏(表面) 游戏 pygame.quit() #这就是我们定义课堂游戏的地方 类游戏(): #此类中的对象表示一个完整的游戏 #这里我们初始化一个游戏。self是初始化的游戏,surface是显示窗口surface对象,我们还为继续游戏和关闭窗口设置了默认值。我们还定义了我们运行游戏的fps,并定义了球的速度、颜色、位置和半径 def u u初始(自,表面): #定义曲面、fps、背景色 自表面=表面 自整角速度=120 self.bg_color=pygame.color('black')) 屏幕宽度=曲面。获取宽度() 屏幕高度=曲面。获取高度() #定义球属性 球头半径=10 ball_pos=[random.randint(球半径,屏幕宽度-球半径), random.randint(球半径、屏幕高度-球半径)] ball\u color=pygame.color('white')) 球的速度=[2,1] 球=球(球位置、球半径、球颜色、球速度、表面) #定义桨属性 rect_left=[50450] 矩形顶部=225 矩形高度=60 矩形宽度=10 self.paile1=Rect(Rect_left[0],Rect_top,Rect_width,Rect_height,surface) self.paile2=Rect(Rect_left[1],Rect_top,Rect_width,Rect_height,surface) self.game\u Clock=pygame.time.Clock() self.close\u clicked=False self.continue\u game=True 自我评分1=0 自我评分2=0 自帧计数器=0 def播放(自我): #游戏一直玩到玩家点击关闭 当未单击self.close时,单击: self.handle_事件() self.draw() #如果没有设置为false,游戏将继续更新 如果self.continue\u游戏: self.update() self.game_Clock.tick(self.FPS) #分数被绘制到屏幕上(不重要,这只是在下一个版本中使用一个功能),我们定义分数消息的彩色字体背景等,并在分数被打分时更新分数 def draw_分数(自我): font\u color=pygame.color(“白色”) font\u bg=pygame.Color(“黑色”) font=pygame.font.SysFont(“arial”,18) text\u img=font.render(“玩家1的分数:”+str(self.score1)+“玩家2的分数:”+str(self.score2),True,字体颜色,字体背景) 文本位置=(0,0) self.surface.blit(text\u img,text\u pos) #球,表面,分数,和两个桨被绘制,pygame也会每帧更新一次这个绘制 def牵引(自): self.surface.fill(self.bg_颜色) self.draw_score() #pygame.draw.rect(self.surface,pygame.Color('blue'),(50225,10,50)) #pygame.draw.rect(self.surface,pygame.Color('red'),(450225,10,50)) self.paile1.draw() self.paile2.draw() self.ball.draw() pygame.display.update() #分数值设置为默认值0,我们告诉球移动,并在每次更新时向帧计数器添加1。为下一帧更新游戏对象 def更新(自我): self.ball.move() 自我评分=0 自帧计数器+=自帧计数器+1 #在这里,我们设置一个事件循环,并确定结束游戏的操作是否已完成 def处理_事件(自): events=pygame.event.get() 对于事件中的事件: 如果event.type==pygame.QUIT: self.close\u clicked=True #用户定义的类球 班级舞会: #自我是要初始化的球。为初始化的球定义颜色/中心/半径 定义初始(自身、中心、半径、颜色、速度、表面): 自我中心=中心 自半径=半径 self.color=color 自身速度=速度 自表面=表面 #确定屏幕尺寸,并检查球的边缘是否未接触边缘。如果它接触到边缘,它会反弹并反转速度 def移动(自我): screen\u width=self.surface.get\u width() screen\u height=self.surface.get\u height() 屏幕大小=(屏幕宽度、屏幕高度) 对于范围(0,len(自中心))中的i: 自中心[i]+=自速度[i] 如果(自中心[i]=屏幕尺寸[i]-自半径): 自速度[i]=-自速度[i] #球被抽出来了 def牵引(自): pygame.draw.circle(self.surface,self.color,self.center,self.radius) 类矩形: 定义初始(自、左、顶、宽度、高度、表面): #self.left=左 #self.top=top #自身宽度=宽度 #自我高度=高度 自表面=表面 self.rect=pygame.rect(左、上、宽、高) def牵引(自): pygame.draw.rect(self.surface,pygame.Color('red'),self.rect) def碰撞(自): 如果pygame.Rect.collide(x,y)=True: 返回真值 其他: 返回错误 main()_Python_Pygame - Fatal编程技术网

Python 在pygame中实现Rect对象和球之间的碰撞检测功能 导入pygame,随机,时间 #我们调用所有其他函数的主函数,启动游戏循环,退出pygame并清理窗口。在内部,我们创建一个游戏对象,显示表面,并通过调用游戏对象上的play方法启动游戏循环。还有一个带有游戏标题的标题。 def main(): pygame.init() 大小=(500400) surface=pygame.display.set_模式(大小) pygame.display.set_标题('pongv2') 游戏=游戏(表面) 游戏 pygame.quit() #这就是我们定义课堂游戏的地方 类游戏(): #此类中的对象表示一个完整的游戏 #这里我们初始化一个游戏。self是初始化的游戏,surface是显示窗口surface对象,我们还为继续游戏和关闭窗口设置了默认值。我们还定义了我们运行游戏的fps,并定义了球的速度、颜色、位置和半径 def u u初始(自,表面): #定义曲面、fps、背景色 自表面=表面 自整角速度=120 self.bg_color=pygame.color('black')) 屏幕宽度=曲面。获取宽度() 屏幕高度=曲面。获取高度() #定义球属性 球头半径=10 ball_pos=[random.randint(球半径,屏幕宽度-球半径), random.randint(球半径、屏幕高度-球半径)] ball\u color=pygame.color('white')) 球的速度=[2,1] 球=球(球位置、球半径、球颜色、球速度、表面) #定义桨属性 rect_left=[50450] 矩形顶部=225 矩形高度=60 矩形宽度=10 self.paile1=Rect(Rect_left[0],Rect_top,Rect_width,Rect_height,surface) self.paile2=Rect(Rect_left[1],Rect_top,Rect_width,Rect_height,surface) self.game\u Clock=pygame.time.Clock() self.close\u clicked=False self.continue\u game=True 自我评分1=0 自我评分2=0 自帧计数器=0 def播放(自我): #游戏一直玩到玩家点击关闭 当未单击self.close时,单击: self.handle_事件() self.draw() #如果没有设置为false,游戏将继续更新 如果self.continue\u游戏: self.update() self.game_Clock.tick(self.FPS) #分数被绘制到屏幕上(不重要,这只是在下一个版本中使用一个功能),我们定义分数消息的彩色字体背景等,并在分数被打分时更新分数 def draw_分数(自我): font\u color=pygame.color(“白色”) font\u bg=pygame.Color(“黑色”) font=pygame.font.SysFont(“arial”,18) text\u img=font.render(“玩家1的分数:”+str(self.score1)+“玩家2的分数:”+str(self.score2),True,字体颜色,字体背景) 文本位置=(0,0) self.surface.blit(text\u img,text\u pos) #球,表面,分数,和两个桨被绘制,pygame也会每帧更新一次这个绘制 def牵引(自): self.surface.fill(self.bg_颜色) self.draw_score() #pygame.draw.rect(self.surface,pygame.Color('blue'),(50225,10,50)) #pygame.draw.rect(self.surface,pygame.Color('red'),(450225,10,50)) self.paile1.draw() self.paile2.draw() self.ball.draw() pygame.display.update() #分数值设置为默认值0,我们告诉球移动,并在每次更新时向帧计数器添加1。为下一帧更新游戏对象 def更新(自我): self.ball.move() 自我评分=0 自帧计数器+=自帧计数器+1 #在这里,我们设置一个事件循环,并确定结束游戏的操作是否已完成 def处理_事件(自): events=pygame.event.get() 对于事件中的事件: 如果event.type==pygame.QUIT: self.close\u clicked=True #用户定义的类球 班级舞会: #自我是要初始化的球。为初始化的球定义颜色/中心/半径 定义初始(自身、中心、半径、颜色、速度、表面): 自我中心=中心 自半径=半径 self.color=color 自身速度=速度 自表面=表面 #确定屏幕尺寸,并检查球的边缘是否未接触边缘。如果它接触到边缘,它会反弹并反转速度 def移动(自我): screen\u width=self.surface.get\u width() screen\u height=self.surface.get\u height() 屏幕大小=(屏幕宽度、屏幕高度) 对于范围(0,len(自中心))中的i: 自中心[i]+=自速度[i] 如果(自中心[i]=屏幕尺寸[i]-自半径): 自速度[i]=-自速度[i] #球被抽出来了 def牵引(自): pygame.draw.circle(self.surface,self.color,self.center,self.radius) 类矩形: 定义初始(自、左、顶、宽度、高度、表面): #self.left=左 #self.top=top #自身宽度=宽度 #自我高度=高度 自表面=表面 self.rect=pygame.rect(左、上、宽、高) def牵引(自): pygame.draw.rect(self.surface,pygame.Color('red'),self.rect) def碰撞(自): 如果pygame.Rect.collide(x,y)=True: 返回真值 其他: 返回错误 main()

Python 在pygame中实现Rect对象和球之间的碰撞检测功能 导入pygame,随机,时间 #我们调用所有其他函数的主函数,启动游戏循环,退出pygame并清理窗口。在内部,我们创建一个游戏对象,显示表面,并通过调用游戏对象上的play方法启动游戏循环。还有一个带有游戏标题的标题。 def main(): pygame.init() 大小=(500400) surface=pygame.display.set_模式(大小) pygame.display.set_标题('pongv2') 游戏=游戏(表面) 游戏 pygame.quit() #这就是我们定义课堂游戏的地方 类游戏(): #此类中的对象表示一个完整的游戏 #这里我们初始化一个游戏。self是初始化的游戏,surface是显示窗口surface对象,我们还为继续游戏和关闭窗口设置了默认值。我们还定义了我们运行游戏的fps,并定义了球的速度、颜色、位置和半径 def u u初始(自,表面): #定义曲面、fps、背景色 自表面=表面 自整角速度=120 self.bg_color=pygame.color('black')) 屏幕宽度=曲面。获取宽度() 屏幕高度=曲面。获取高度() #定义球属性 球头半径=10 ball_pos=[random.randint(球半径,屏幕宽度-球半径), random.randint(球半径、屏幕高度-球半径)] ball\u color=pygame.color('white')) 球的速度=[2,1] 球=球(球位置、球半径、球颜色、球速度、表面) #定义桨属性 rect_left=[50450] 矩形顶部=225 矩形高度=60 矩形宽度=10 self.paile1=Rect(Rect_left[0],Rect_top,Rect_width,Rect_height,surface) self.paile2=Rect(Rect_left[1],Rect_top,Rect_width,Rect_height,surface) self.game\u Clock=pygame.time.Clock() self.close\u clicked=False self.continue\u game=True 自我评分1=0 自我评分2=0 自帧计数器=0 def播放(自我): #游戏一直玩到玩家点击关闭 当未单击self.close时,单击: self.handle_事件() self.draw() #如果没有设置为false,游戏将继续更新 如果self.continue\u游戏: self.update() self.game_Clock.tick(self.FPS) #分数被绘制到屏幕上(不重要,这只是在下一个版本中使用一个功能),我们定义分数消息的彩色字体背景等,并在分数被打分时更新分数 def draw_分数(自我): font\u color=pygame.color(“白色”) font\u bg=pygame.Color(“黑色”) font=pygame.font.SysFont(“arial”,18) text\u img=font.render(“玩家1的分数:”+str(self.score1)+“玩家2的分数:”+str(self.score2),True,字体颜色,字体背景) 文本位置=(0,0) self.surface.blit(text\u img,text\u pos) #球,表面,分数,和两个桨被绘制,pygame也会每帧更新一次这个绘制 def牵引(自): self.surface.fill(self.bg_颜色) self.draw_score() #pygame.draw.rect(self.surface,pygame.Color('blue'),(50225,10,50)) #pygame.draw.rect(self.surface,pygame.Color('red'),(450225,10,50)) self.paile1.draw() self.paile2.draw() self.ball.draw() pygame.display.update() #分数值设置为默认值0,我们告诉球移动,并在每次更新时向帧计数器添加1。为下一帧更新游戏对象 def更新(自我): self.ball.move() 自我评分=0 自帧计数器+=自帧计数器+1 #在这里,我们设置一个事件循环,并确定结束游戏的操作是否已完成 def处理_事件(自): events=pygame.event.get() 对于事件中的事件: 如果event.type==pygame.QUIT: self.close\u clicked=True #用户定义的类球 班级舞会: #自我是要初始化的球。为初始化的球定义颜色/中心/半径 定义初始(自身、中心、半径、颜色、速度、表面): 自我中心=中心 自半径=半径 self.color=color 自身速度=速度 自表面=表面 #确定屏幕尺寸,并检查球的边缘是否未接触边缘。如果它接触到边缘,它会反弹并反转速度 def移动(自我): screen\u width=self.surface.get\u width() screen\u height=self.surface.get\u height() 屏幕大小=(屏幕宽度、屏幕高度) 对于范围(0,len(自中心))中的i: 自中心[i]+=自速度[i] 如果(自中心[i]=屏幕尺寸[i]-自半径): 自速度[i]=-自速度[i] #球被抽出来了 def牵引(自): pygame.draw.circle(self.surface,self.color,self.center,self.radius) 类矩形: 定义初始(自、左、顶、宽度、高度、表面): #self.left=左 #self.top=top #自身宽度=宽度 #自我高度=高度 自表面=表面 self.rect=pygame.rect(左、上、宽、高) def牵引(自): pygame.draw.rect(self.surface,pygame.Color('red'),self.rect) def碰撞(自): 如果pygame.Rect.collide(x,y)=True: 返回真值 其他: 返回错误 main(),python,pygame,Python,Pygame,以上是我迄今为止的工作,基本上它应该是复古街机游戏乒乓球,球从球拍边缘反弹,如果没有,则对方在击中窗户边缘时得分。因此,具体来说,项目的这一部分要求我让球从桨的前部反弹,我不知道如何做到这一点。我最初的想法是在类Rect中使用collidepoint方法,如果 import pygame, random, time # main function where we call all other functions, start the game loop, quit pygame and cle

以上是我迄今为止的工作,基本上它应该是复古街机游戏乒乓球,球从球拍边缘反弹,如果没有,则对方在击中窗户边缘时得分。因此,具体来说,项目的这一部分要求我让球从桨的前部反弹,我不知道如何做到这一点。我最初的想法是在类Rect中使用collidepoint方法,如果
import pygame, random, time
# main function where we call all other functions, start the game loop, quit pygame and clean up the window. Inside we create a game object, display surface, and start the game loop by calling the play method on the game object. There is also a set caption with the title of the game.
def main():
    pygame.init()
    size =(500,400)
    surface=pygame.display.set_mode(size)
    pygame.display.set_caption('Pong v2')
    game = Game(surface)
    game.play()
    pygame.quit()
# This is where we define the class game    
class Game():
    # an object in this class represents a complete game

    # here we initialize a game. self is the game that is initialized surface is the display window surface object we also set default values for continuing the game and closing the window. we also define what fps we are running the game at, and define the velocity color position and radius of the ball
    def __init__(self,surface):

        # defining surface, fps, background color
        self.surface=surface
        self.FPS=120
        self.bg_color=pygame.Color('black')
        screen_width = surface.get_width()
        screen_height = surface.get_height()

        # defining ball attributes
        ball_radius=10
        ball_pos = [random.randint(ball_radius, screen_width-ball_radius), 
                    random.randint(ball_radius, screen_height-ball_radius)]
        ball_color=pygame.Color('white')
        ball_velocity=[2,1]
        self.ball=Ball(ball_pos,ball_radius,ball_color,ball_velocity,surface)

        # defining paddle attributes

        rect_left=[50,450]
        rect_top=225
        rect_height=60
        rect_width=10
        self.Paddle1=Rect(rect_left[0],rect_top,rect_width,rect_height,surface)
        self.Paddle2=Rect(rect_left[1],rect_top,rect_width,rect_height,surface)


        self.game_Clock=pygame.time.Clock()
        self.close_clicked=False
        self.continue_game=True
        self.score1=0
        self.score2=0
        self.frame_counter=0

    def play(self):
        # game is played until player clicks close
        while not self.close_clicked:  

            self.handle_events()

            self.draw()


            # if nothing sets this to false the game will continue to update
            if self.continue_game:
                self.update()

            self.game_Clock.tick(self.FPS)
    # score is drawn onto the screen (unimportant this is just playing with a feature for the next version), we define color font background etc of the score message and update score upon points being scored
    def draw_score(self):
        font_color = pygame.Color("white")
        font_bg    = pygame.Color("black")
        font       = pygame.font.SysFont("arial", 18)
        text_img   = font.render("Score for Player 1: " + str(self.score1) + ' Score for Player 2: ' + str(self.score2), True, font_color, font_bg)     
        text_pos   = (0,0)
        self.surface.blit(text_img, text_pos)

    # ball, surface, score, and two paddles are drawn, pygame also updates this drawing once per frame    
    def draw(self):
        self.surface.fill(self.bg_color)

        self.draw_score()
        #pygame.draw.rect(self.surface,pygame.Color('blue'),(50,225,10,50))
        #pygame.draw.rect(self.surface,pygame.Color('red'),(450,225,10,50))
        self.Paddle1.draw()
        self.Paddle2.draw()

        self.ball.draw()


        pygame.display.update()
    # score value set to default of 0 we tell ball to move and add 1 to frame counter upon each update. update game object for the next frame 
    def update(self):
        self.ball.move()
        self.score=0
        self.frame_counter+=self.frame_counter+1











    # here we setup an event loop and figure out if the action to end the game has been done
    def handle_events(self):
        events=pygame.event.get()
        for event in events:
            if event.type== pygame.QUIT:
                self.close_clicked=True
# user defined class ball            
class Ball:
    # self is the ball to intialize. color/center/radius are defined for the ball that is initialized
    def __init__(self,center,radius,color,velocity,surface):
        self.center=center
        self.radius=radius
        self.color=color
        self.velocity=velocity
        self.surface=surface
    # screen size is determined and edge of ball is checked that it is not touching the edge. if it is touching the edge it bounces and reverses velocity   
    def move(self):
        screen_width=self.surface.get_width()
        screen_height=self.surface.get_height()
        screen_size=(screen_width,screen_height)
        for i in range(0,len(self.center)):
            self.center[i]+=self.velocity[i]
            if (self.center[i]<=0 + self.radius or self.center[i]>=screen_size[i] - self.radius):
                self.velocity[i]=-self.velocity[i]
    # ball is drawn            
    def draw(self):
        pygame.draw.circle(self.surface,self.color,self.center,self.radius)


class Rect:
    def __init__(self,left,top,width,height,surface):
        #self.left=left
        #self.top=top
        #self.width=width
        #self.height=height
        self.surface=surface
        self.rect=pygame.Rect(left,top,width,height)

    def draw(self):
        pygame.draw.rect(self.surface,pygame.Color('red'),self.rect)

    def collide(self):
        if pygame.Rect.collide(x,y) == True:
            return True
        else:
            return False
















main()