Ejemplo n.º 1
0
 def __init__(self,screen):
     super().__init__(screen)
     self.id = 'pingball'#片段id
     self.racket = Racket(self.screen)#定义球拍
     self.ball = Ball(self.screen)
     # 创建一个边界碰撞检对象
     self.bc = BorderCrossing(5, 5, RESOLUTION[0] - 10, RESOLUTION[1] - 10)
Ejemplo n.º 2
0
class Pingball(Scene):
    '''游戏主场景,左右移动方块,遇一小球反弹'''
    def __init__(self,screen):
        super().__init__(screen)
        self.id = 'pingball'#片段id
        self.racket = Racket(self.screen)#定义球拍
        self.ball = Ball(self.screen)
        # 创建一个边界碰撞检对象
        self.bc = BorderCrossing(5, 5, RESOLUTION[0] - 10, RESOLUTION[1] - 10)

    def replay(self):
        '''游戏重新开始'''
        self.ball.rect.x = random.randint(100,200)
        self.ball.rect.y = random.randint(100,200)


    def draw(self):
        self.screen.fill((0, 0, 0))
        self.racket.draw()
        self.ball.draw()

    def update(self):
        self.racket.update()#更新球拍
        self.ball.update()#更新球
        self.bc.sprite = self.ball.rect#传递需要检测的对象
        ######################
        #检测球碰到边界时反弹
        if self.bc.isTopBorderCrossing() or self.bc.isBottomBorderCrossing():
            self.ball.speed_y = -self.ball.speed_y
        if self.bc.isLeftBorderCrossing() or self.bc.isRightBorderCrossing():
            self.ball.speed_x = -self.ball.speed_x
        ######################
        # 检测球与球拍的碰撞
        if (pygame.sprite.collide_rect(self.ball,self.racket)) :
            #小球反弹
            self.ball.speed_y = -self.ball.speed_y
            #控制每弹一次,速度增加2
            if self.ball.speed_y > 0:
                self.ball.speed_y += 2
            elif self.ball.speed_y <0:
                self.ball.speed_y -= 2
        # 检测球超出球拍,游戏结束判断。
        if self.ball.rect.y + self.ball.rect.height > self.racket.rect.y+10 :
            for scene in self.scenes:
                if scene.id == 'gameover':
                    scene.start = True
                else:
                    scene.start = False


    def handle_event(self, event):

        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                self.racket.rect.left -= 5
            elif event.key == K_RIGHT:
                self.racket.rect.right += 5
Ejemplo n.º 3
0
    def __init__(self, screen):
        super().__init__(screen)
        self.id = 'GreedSnake'  # 场景ID
        self.gamebackground = GameBackground(self.screen)  # 游戏场景背景
        self.bodys = []  # 创建一个精灵组,用来放置蛇的身体
        self.direction = DOWN  # 移动方向
        self.replay()  # 游戏开始
        self.pause = False  # 游戏暂停控制

        self.last_update = pygame.time.get_ticks()  # 游戏开始时的计时
        self.bordercrossing = BorderCrossing(-1, -1, 401, 401)
Ejemplo n.º 4
0
 def __init__(self, screen):
     super().__init__(screen)
     self.id = 'test'
     self.resolution = (640, 480)
     self.t_x = 300
     self.t_y = 300
     self.last_update = pygame.time.get_ticks()  # 游戏开始时的计时
     self.ms = Myrs(self.screen, self.resolution)
     self.myimgs = pygame.sprite.Group()
     # 创建一个边界碰撞检对象
     self.bc = BorderCrossing(5, 5, self.resolution[0] - 10,
                              self.resolution[1] - 10)
     for i in range(7):
         self.myimgs.add(MyImgSprite())
Ejemplo n.º 5
0
class GreedSnake(Scene):
    def __init__(self, screen):
        super().__init__(screen)
        self.id = 'GreedSnake'  # 场景ID
        self.gamebackground = GameBackground(self.screen)  # 游戏场景背景
        self.bodys = []  # 创建一个精灵组,用来放置蛇的身体
        self.direction = DOWN  # 移动方向
        self.replay()  # 游戏开始
        self.pause = False  # 游戏暂停控制

        self.last_update = pygame.time.get_ticks()  # 游戏开始时的计时
        self.bordercrossing = BorderCrossing(-1, -1, 401, 401)

    def replay(self):
        '''初始化游戏开始数据,重新开始游戏'''
        self.bodys.clear()  # 清空蛇list
        # 默认出场的蛇
        for i in range(5):
            if i == 0:
                color = RED
            else:
                color = BLACK
            self.bodys.append(
                SnakeBody(self.screen, 2 * 40, (6 - i) * 40, color))
        self.food = Food(self.screen, self.bodys)

    def draw(self):
        self.gamebackground.draw()  # 渲染场景中的背景
        ##渲染蛇
        for body in self.bodys:
            body.draw()
        self.food.draw()  # 渲染食物

    def update(self):
        now = pygame.time.get_ticks()
        if now - self.last_update > 500:
            if not self.pause:  # 如果游戏没有暂停,则继续更新
                # 蛇吃到食物时,添加食物为蛇头,并修改之前蛇头的颜色
                # 蛇的运动,根据方向判断进行坐标修改,对于身体的坐标修改
                if self.direction == 'down':
                    if self.bodys[
                            0].rect.y + 40 == self.food.rect.y and self.bodys[
                                0].rect.x == self.food.rect.x:
                        self.bodys.insert(
                            0,
                            SnakeBody(self.screen, self.food.rect.x,
                                      self.food.rect.y, RED))
                        self.bodys[1].color = BLACK
                        self.food = Food(self.screen, self.bodys)

                    for i in reversed(range(len(self.bodys))):
                        self.bodys[i].rect.x = self.bodys[i - 1].rect.x
                        self.bodys[i].rect.y = self.bodys[i - 1].rect.y
                        if i == 1:
                            break
                    self.bodys[0].rect.y += 40
                    self.last_update = now

                if self.direction == 'up':
                    if self.bodys[
                            0].rect.y - 40 == self.food.rect.y and self.bodys[
                                0].rect.x == self.food.rect.x:
                        self.bodys.insert(
                            0,
                            SnakeBody(self.screen, self.food.rect.x,
                                      self.food.rect.y, RED))
                        self.bodys[1].color = BLACK
                        self.food = Food(self.screen, self.bodys)

                    for i in reversed(range(len(self.bodys))):
                        self.bodys[i].rect.x = self.bodys[i - 1].rect.x
                        self.bodys[i].rect.y = self.bodys[i - 1].rect.y
                        if i == 1:
                            break
                    self.bodys[0].rect.y -= 40
                    self.last_update = now

                if self.direction == 'left':
                    if self.bodys[
                            0].rect.x - 40 == self.food.rect.x and self.bodys[
                                0].rect.y == self.food.rect.y:
                        self.bodys.insert(
                            0,
                            SnakeBody(self.screen, self.food.rect.x,
                                      self.food.rect.y, RED))
                        self.bodys[1].color = BLACK
                        self.food = Food(self.screen, self.bodys)

                    for i in reversed(range(len(self.bodys))):
                        self.bodys[i].rect.x = self.bodys[i - 1].rect.x
                        self.bodys[i].rect.y = self.bodys[i - 1].rect.y
                        if i == 1:
                            break
                    self.bodys[0].rect.x -= 40
                    self.last_update = now

                if self.direction == 'right':
                    if self.bodys[
                            0].rect.x + 40 == self.food.rect.x and self.bodys[
                                0].rect.y == self.food.rect.y:
                        self.bodys.insert(
                            0,
                            SnakeBody(self.screen, self.food.rect.x,
                                      self.food.rect.y, RED))
                        self.bodys[1].color = BLACK
                        self.food = Food(self.screen, self.bodys)

                    for i in reversed(range(len(self.bodys))):
                        self.bodys[i].rect.x = self.bodys[i - 1].rect.x
                        self.bodys[i].rect.y = self.bodys[i - 1].rect.y
                        if i == 1:
                            break
                    self.bodys[0].rect.x += 40
                    self.last_update = now
                # 检测蛇头如果超出场景边界,游戏结束。
                self.bordercrossing.sprite = self.bodys[0].rect
                if self.bordercrossing.isBorder():
                    for scene in self.scenes:
                        if scene.id == 'gameover':
                            scene.start = True
                        else:
                            scene.start = False
                for i in range(1, len(self.bodys)):
                    if self.bodys[0].rect.x == self.bodys[
                            i].rect.x and self.bodys[0].rect.y == self.bodys[
                                i].rect.y:
                        for scene in self.scenes:
                            if scene.id == 'gameover':
                                scene.start = True
                            else:
                                scene.start = False

    def handle_event(self, event):
        #控制游戏暂停
        if event.type == KEYUP:
            if event.key == K_SPACE:
                if self.pause:
                    self.pause = False
                elif not self.pause:
                    self.pause = True
        if event.type == KEYDOWN:
            if event.key == K_DOWN:
                self.direction = DOWN
            elif event.key == K_UP:
                self.direction = UP
            elif event.key == K_LEFT:
                self.direction = LEFT
            elif event.key == K_RIGHT:
                self.direction = RIGHT
Ejemplo n.º 6
0
def runGame():
    '''游戏核心方法,渲染游戏场景,游戏逻辑判断等'''
    m = MyImgSprite()  #创建一个精灵对象
    m.image = pygame.image.load('./images/b.png')

    #创建一组精灵对象
    myimgs = pygame.sprite.Group()
    for i in range(7):
        myimgs.add(MyImgSprite())
    print(len(myimgs))
    #创建一个边界碰撞检对象
    bc = BorderCrossing(5, 5, SCREEN_WIDTH - 10, SCREEN_HEIGHT - 10)

    #######游戏引擎设置:判断游戏结束,更新游戏,刷新帧速率设置
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
        ############
        drawbackground()  #绘制场景背景颜色
        DISPLAYSURF.blit(m.image, m.rect)  #绘制绿球
        myimgs.draw(DISPLAYSURF)  #绘制一组红球

        #绿球边界碰撞检测
        bc.sprite = m.rect
        if bc.isTopBorderCrossing() or bc.isBottomBorderCrossing():
            m.speed_y = -m.speed_y
        if bc.isLeftBorderCrossing() or bc.isRightBorderCrossing():
            m.speed_x = -m.speed_x

        #红绿边界碰撞检测
        for sp in myimgs:
            bc.sprite = sp.rect
            if bc.isTopBorderCrossing() or bc.isBottomBorderCrossing():
                sp.speed_y = -sp.speed_y
            if bc.isLeftBorderCrossing() or bc.isRightBorderCrossing():
                sp.speed_x = -sp.speed_x
        #绿球与红球碰撞检测
        for m1 in myimgs:
            k = pygame.sprite.collide_rect(m, m1)
            if k:
                m.speed_x = -m.speed_x
                m.speed_y = -m.speed_y
                m1.speed_x = -m1.speed_x
                m1.speed_y = -m1.speed_y
        # rst = list_collide = pygame.sprite.spritecollide(m,myimgs,True)
        #true,碰到后直接删除, false会删除Group组中的精灵。
        #########################################
        #红球之间的碰撞检测
        ####################
        for s1 in myimgs:
            for s2 in myimgs:
                if s1.rect.x != s2.rect.x:  #判断不是同一个sprite
                    print('不是同一个精灵')
                    if pygame.sprite.collide_rect(s1, s2):  #碰撞检测
                        print(s1.rect, s2.rect)
                        s2.speed_x = -s2.speed_x
                        s2.speed_y = -s2.speed_y
                        s1.speed_x = -s1.speed_x
                        s1.speed_y = -s1.speed_y
                        myimgs.update()  #这里需要立即更新红球的坐标

        m.update()  #更新绿球
        myimgs.update()  #更新红球Group

        pygame.display.flip()
        FPSCLOCK.tick(FPS)  #设置帧速率
Ejemplo n.º 7
0
class TestScene(Scene):
    def __init__(self, screen):
        super().__init__(screen)
        self.id = 'test'
        self.resolution = (640, 480)
        self.t_x = 300
        self.t_y = 300
        self.last_update = pygame.time.get_ticks()  # 游戏开始时的计时
        self.ms = Myrs(self.screen, self.resolution)
        self.myimgs = pygame.sprite.Group()
        # 创建一个边界碰撞检对象
        self.bc = BorderCrossing(5, 5, self.resolution[0] - 10,
                                 self.resolution[1] - 10)
        for i in range(7):
            self.myimgs.add(MyImgSprite())

    def draw(self):
        self.screen.fill((255, 0, 0))
        pygame.draw.rect(
            self.screen,
            (255, 255, 255),
            (100, 100, 100, 100),
        )

        self.ms.draw()
        self.myimgs.draw(self.screen)
        print_text(
            self.screen,
            title_h3,
            self.t_x,
            self.t_y,
            'hello world请按回车继续',
        )

    def update(self):
        self.ms.update()
        # 红绿边界碰撞检测
        for sp in self.myimgs:
            self.bc.sprite = sp.rect
            if self.bc.isTopBorderCrossing() or self.bc.isBottomBorderCrossing(
            ):
                sp.speed_y = -sp.speed_y
            if self.bc.isLeftBorderCrossing() or self.bc.isRightBorderCrossing(
            ):
                sp.speed_x = -sp.speed_x

        self.myimgs.update()  ##检测边界碰撞后更新所有组中球的坐标。
        now = pygame.time.get_ticks()
        if now - self.last_update > 1000:
            self.t_x = random.randint(100, 400)
            self.t_y = random.randint(100, 400)
            self.last_update = now

    def handle_event(self, event):
        if event.type == KEYUP:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
            elif event.key == K_RETURN:
                for scene in self.scenes:
                    if scene.id == 'gameover':
                        scene.start = True
                    else:
                        scene.start = False