예제 #1
0
파일: pong.py 프로젝트: jcfug8/cs1410-pong
 def __init__(self, width, height, fps):
     game_mouse.Game.__init__(self, "Pygame Starter", width, height, fps)
     self.ball = ball.Ball(width, height)
     self.paddle = paddle.Paddle(width, height, 20)
     self.paddle2 = paddle.Paddle(width, height, width - 40)
     self.score = score.Score()
     return
예제 #2
0
 def initialize(self):
     self.ball = ball.Ball()
     self.left_paddle = paddle.Paddle()
     self.right_paddle = paddle.Paddle()
     self.sprite_group.add(self.ball)
     self.sprite_group.add(self.left_paddle)
     self.sprite_group.add(self.right_paddle)
예제 #3
0
파일: pong.py 프로젝트: psalcedodev/Pong
    def __init__(self, width, height):
        self.mWidth = width
        self.mHeight = height

        score_width = 80
        score_height = 40
        score_x = self.mWidth / 2 - score_width / 2
        score_y = 40
        self.mScoreBoard = score_board.ScoreBoard(
            score_x, score_y, score_width, score_height)

        wall_size = 10
        self.mLeftWall = wall.Wall(0, 0, wall_size, self.mHeight)
        self.mRightWall = wall.Wall(
            self.mWidth-wall_size, 0, wall_size, self.mHeight)
        self.mTopWall = wall.Wall(0, 0, self.mWidth, wall_size)
        self.mBottomWall = wall.Wall(
            0, self.mHeight-wall_size, self.mWidth, wall_size)

        paddle_margin = 20
        paddle_width = 20
        paddle_height = 100
        paddle_speed = self.mHeight / 1.25
        self.mLeftPaddle = paddle.Paddle(self.mLeftWall.getRightX() + paddle_margin,
                                         self.mHeight / 2 - paddle_height / 2,
                                         paddle_width, paddle_height,
                                         paddle_speed,
                                         self.mTopWall.getBottomY(),
                                         self.mBottomWall.getY())
        self.mLeftPaddle.setColor((	0, 89, 179))

        self.mRightPaddle = paddle.Paddle(self.mRightWall.getX() - paddle_margin - paddle_width,
                                          self.mHeight / 2 - paddle_height / 2,
                                          paddle_width, paddle_height,
                                          paddle_speed,
                                          self.mTopWall.getBottomY(),
                                          self.mBottomWall.getY())
        self.mRightPaddle.setColor((196, 2, 51))

        size = 20
        self.mBall = ball.Ball(size,
                               self.mLeftWall.getRightX(),
                               self.mRightWall.getX(),
                               self.mTopWall.getBottomY(),
                               self.mBottomWall.getY(),
                               self.mLeftPaddle.getRightX(),
                               self.mRightPaddle.getX())
        self.serveBall()

        self.mBall.setLeftPaddleY(
            self.mLeftPaddle.getY(), self.mLeftPaddle.getBottomY())
        self.mBall.setRightPaddleY(
            self.mRightPaddle.getY(), self.mRightPaddle.getBottomY())

        return
예제 #4
0
 def __init__(self):
     pygame.init()
     pygame.display.set_caption('Breakout')
     self.FPSClock = pygame.time.Clock()
     self.display = pygame.display.set_mode([width, height])
     self.state = STATE_BALL_IN_PADDLE
     self.player = paddle.Paddle(paddleX, paddleY, paddleWidth,
                                 paddleHeight, paddleColor)
     self.ball = ball.Ball(paddleX + paddleX / 2, paddleY - ballRadius,
                           ballRadius, [5, -5], maxBallX, maxBallY,
                           ballColor)
     self.bricks = Block.Block(brickWidth, brickHeight, brickColor)
     FPS = 60
     gameOver = False
     while not gameOver:
         for event in pygame.event.get():
             if event.type is pygame.QUIT:
                 gameOver = True
         self.display.fill(backgroundColor)
         mouseX = pygame.mouse.get_pos()[0]
         self.check_start()
         if self.state is STATE_PLAYING:
             self.ball.move(self.display)
             self.collisions()
             self.player.draw(self.display)
             self.bricks.draw_blocks(self.display)
             self.ball.draw(self.display)
         elif self.state is STATE_GAME_OVER:
             gameOver = True
         elif self.state is STATE_WON:
             gameOver = True
         self.player.move(mouseX)
         pygame.display.flip()
         pygame.display.update()
         self.FPSClock.tick(FPS)
예제 #5
0
    def __init__(self):  #initializes the game
        self.main_window = tkinter.Tk()  #creates the main window for the game
        self.main_window.title("Pong")  #titels the window of the game
        self.__canvas = tkinter.Canvas(
            self.main_window, bg="papaya whip", width=600,
            height=600)  #creates the canvas where the game is played
        self.__scoreframe = tkinter.Frame(
            self.main_window
        )  #creates a frame at the bottom where the score will be

        self.__canvas.pack()  #packs the canvas
        self.__scoreframe.pack(
            side="bottom")  #packs the scoreboard to the bottom

        self.__paddle = paddle.Paddle(
            self.__canvas
        )  #creates the paddle in the canvas from the paddle file

        self.__balllist = [
            ball.Ball(self.__canvas, 340, 380, 50, 90)
        ]  #creates a list of balls to spawn, and new balls in the canvas are added here
        self.__scoreNum = 0  #set the initial score of the game to 0

        self.main_window.bind(
            '<Motion>', self.__paddle.mouse_move
        )  #attaches the paddle to the mouse so that when I move the mouse the paddle moves too
        self.__score = tkinter.Label(
            self.__scoreframe, text='Score: ' + (str(self.__scoreNum))
        )  #creates a label in the scoreboard to track the score
        self.__score.pack(side="left")  #packs the score to be at the left.

        self.animate()  #runs teh animate funciton on items above

        tkinter.mainloop()  #loops the game
예제 #6
0
파일: game.py 프로젝트: popensesame/Pong
 def __init__(self, screen, screen_rect, pause=True):
     self.pause = pause
     self.screen_rect = screen_rect
     self.screen = screen
     self.player = _paddle.Paddle(screen_rect, 740, 275)
     self.computer = _paddle.Paddle(screen_rect, 50, 275)
     self.ball = _ball.Ball(self.screen_rect, self.player.rect)
     self.done = False
     self.ball.set_ball()
     self.score = [0, 0]
     self.score_text = str(self.score[0]) + ":" + str(self.score[1])
     self.score_obj = pg.font.Font(None, 48)
     self.score_surface = self.score_obj.render(self.score_text, False, (255, 255, 255))
     self.score_surface_rect = self.score_surface.get_rect()
     self.score_surface_rect.topleft = (360, 20)
     self.next = "MENU"
예제 #7
0
    def __init__(self, width, height):
        self.width = width
        self.height = height

        self.ball = ball.Ball(self.width / 2, self.height / 2, self.width,
                              self.height)

        self.paddle = paddle.Paddle(self.width, self.height, self.width / 2,
                                    self.height / 2)
예제 #8
0
    def __init__(self, dis_width, dis_height):
        pygame.init()

        self.dis_height = dis_height
        self.dis_width = dis_width
        self.screen = pygame.display.set_mode(
            (self.dis_width, self.dis_height))
        self.left_paddle = paddle.Paddle(
            5, 10, (self.dis_height / 2 - self.PADDLE_HEIGHT / 2),
            self.PADDLE_WIDTH, self.PADDLE_HEIGHT, pygame.K_w, pygame.K_s)
        self.right_paddle = paddle.Paddle(
            5, self.dis_width - 20,
            (self.dis_height / 2 - self.PADDLE_HEIGHT / 2), self.PADDLE_WIDTH,
            self.PADDLE_HEIGHT, pygame.K_UP, pygame.K_DOWN)
        self.ball = ball.Ball(-2, self.ball_y_velocity, self.dis_width // 2,
                              self.dis_height // 2, self.BALL_RADIUS)

        self.balls.extend([self.ball])
        self.paddles.extend([self.left_paddle, self.right_paddle])

        pygame.font.init()
        self.score_font = pygame.font.SysFont('Arial', 30)
예제 #9
0
파일: main.py 프로젝트: b3yk3y/Pong
def main():
    global player1
    global player2
    global ball
    global scores
    global font
    # Defining Variables And Classes At The Beginning
    font = pygame.font.SysFont("comicsans", 60, True, False)
    player1 = paddle.Paddle(100, 50, 15, 100)
    player2 = paddle.Paddle(winW - (100 + 15) , winH - (50 + 100), 15, 100)
    ball = pongball.Ball((winW / 2) - (20 / 2), (winH / 2) - (20 / 2), 20, 20)
    scores = np.array([0, 0])
    ball.respawn()
    # Mainloop
    while True:
        clock.tick(fps)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        player1.move(winW, winH, pygame.K_w, pygame.K_s)
        player2.move(winW, winH, pygame.K_UP, pygame.K_DOWN)
        ball.move(winW, winH, 15, 100, player1.pos[0], player1.pos[1], player2.pos[0], player2.pos[1])
        if ball.pos[0] <= 0:
            ball.respawn()
            ball.incVel()
            scores += np.array([0, 1])
        elif ball.pos[0] + ball.size[0] >= winW:
            ball.respawn()
            ball.incVel()
            scores += np.array([1, 0])
            
        if checkWin():
            redraw()
            pygame.time.delay(5000)
            pygame.quit()
            sys.exit()
        redraw()
예제 #10
0
def main():
    # initialize pygame and create window
    pygame.init()
    screen = pygame.display.set_mode((width, height))
    pygame.display.set_caption("TUFF")

    # set game to running
    game_over = False

    myball = ball.Ball(width//2, 300, 4, 0, 20)
    mypaddle = paddle.Paddle(170, 300, 70, 20)
    gravity = 1.0

    while not game_over:
        time.sleep(0.01667)
        # print(ball_y, dy)
        # +0 +1 +2 +3 +4 +5 -4 -3 -2 -1 0

        # update positions
        pos = pygame.mouse.get_pos()
        mypaddle.move_to(pos[0], pos[1], width, height)
        myball.move(width, height)

        # check for collisions and create/destroy objects
        pingpongphysics.handle_collision(myball, mypaddle)

        # process the events (which might change position/physics)
        ev = pygame.event.get()
        for event in ev:
            if event.type == pygame.MOUSEBUTTONUP:
                # pos = pygame.mouse.get_pos()
                # pick = random.choice(circle_palette)
                # pygame.draw.circle(screen, pick, pos, random.randint(10, 50))
                # when mouse button is released, bounce the ll.ll so it goes the other way
                myball.bounce_y()

            if event.type == pygame.QUIT:
                game_over = True

        # apply physics
        myball.accelerate(gravity)

        # clear the screen
        screen.fill(background_color)

        # draw things
        myball.draw(screen, BLUE)
        mypaddle.draw(screen, GREEN)

        # update the display
        pygame.display.update()
    def setUp(self):
        self.expected_x = 125
        self.expected_y = 250
        self.expected_width = 25
        self.expected_height = 100
        self.expected_speed = 90
        self.expected_min_y = 200
        self.expected_max_y = 800

        self.paddle = paddle.Paddle(self.expected_x, self.expected_y,
                                    self.expected_width, self.expected_height,
                                    self.expected_speed, self.expected_min_y,
                                    self.expected_max_y)

        return
예제 #12
0
파일: main.py 프로젝트: will-y/brickbreaker
    def runGame(self):
        paddleInstance = paddle.Paddle(self.screen, self.windowWidth,
                                       self.windowHeight)
        paddleInstance.drawPaddle(300)

        ballInstance = ball.Ball(self.screen, self.windowWidth,
                                 self.windowHeight)
        ballInstance.drawBall((350, 690))

        brickInstance = bricks.Bricks(self.screen, ballInstance)
        brickInstance.drawBricks()

        pg.draw.rect(self.screen, self.borderColor,
                     (0, 0, self.windowWidth, self.windowHeight),
                     self.borderWidth)

        ballFree = False

        while True:
            self.clock.tick(60)
            for event in pg.event.get():
                if (event.type == pg.QUIT):
                    sys.exit()

            key = pg.key.get_pressed()
            if key[pg.K_RIGHT]:
                paddleInstance.movePaddle("right")
                if (ballInstance.stuckToPaddle):
                    ballInstance.moveBall(paddleInstance.getPos(),
                                          paddleInstance.getRect(), "right")
            if (key[pg.K_LEFT]):
                paddleInstance.movePaddle("left")
                if (ballInstance.stuckToPaddle):
                    ballInstance.moveBall(paddleInstance.getPos(),
                                          paddleInstance.getRect(), "left")
            if (not ballFree):
                if (key[pg.K_SPACE]):
                    ballInstance.freeBall()
                    ballFree = True

            brickInstance.checkBallHit(ballInstance.getCollider())

            if (not ballInstance.stuckToPaddle):
                ballInstance.moveBall(paddleInstance.getPos(),
                                      paddleInstance.getRect(), "")

            pg.display.update()
예제 #13
0
class GUI:
    board = board.Board()
    paddle = paddle.Paddle()
    block = block.Block()
    ball = ball.Ball()

    def startGame(self):
        return

    def drawBoard(self):
        return

    def drawBlocks(self):
        return

    def drawPaddle(self):
        return

    def drawBall(self):
        return
예제 #14
0
    def __init__(self):
        self.tps_max = 15.0
        self.temp_color = colors.Colors.GREEN
        self.temp_color_2 = colors.Colors.GREEN
        self.level = 1
        self.score = 0
        self.i = 0
        self.temp = 0
        self.temp_2 = 0
        self.screen = pygame.display.set_mode((1280, 720))
        self.tps_clock = pygame.time.Clock()
        self.tps_delta = 0.0
        try:
            assets.Assets.load()
        except:
            raise Exception("Music initialization went wrong")

        #  hitSound = pygame.mixer.Sound("assets/hit.wav")

        try:
            pygame.display.set_caption("Arcanoid")

            pygame.display.set_icon(assets.Assets.icon)
        except FileNotFoundError:
            raise Exception("File not found")
        self.screen = pygame.display.set_mode((1280, 720))
        self.tps_clock = pygame.time.Clock()
        self.tps_delta = 0.0

        self.player = paddle.Paddle(self)
        self.ball = ball.Ball(self, 600, 350)
        self.button = button.Button(440, 200, 400, 100, False)
        self.button_2 = button.Button(440, 500, 400, 100, False)

        self.list = []

        for i in range(5):
            self.list.append(
                brick.Brick(self, random.randint(100, 400),
                            random.randint(100, 400)))
예제 #15
0
    def __init__(self, width, height, fps):

        game_mouse.Game.__init__(self, "Pygame Starter", width, height, fps)
        pygame.font.init()
        self.font = pygame.font.SysFont('arial', 30, True)

        self.ball = ball.Ball(10, 10, (255, 255, 255), width, height)

        self.paddle = paddle.Paddle(60, 20, (255, 255, 255), width, height)
        self.brick = [
            brick.Brick(60, 30, 575, 0, (255, 255, 255), width, height),
            brick.Brick(60, 30, 575, 62, (255, 255, 255), width, height),
            brick.Brick(60, 30, 575, 124, (255, 255, 255), width, height),
            brick.Brick(60, 30, 575, 186, (255, 255, 255), width, height),
            brick.Brick(60, 30, 575, 248, (255, 255, 255), width, height),
            brick.Brick(60, 30, 575, 310, (255, 255, 255), width, height),
            brick.Brick(60, 30, 575, 372, (255, 255, 255), width, height),
            brick.Brick(60, 30, 575, 434, (255, 255, 255), width, height)
        ]
        self.score = score.Score()

        return
예제 #16
0
def main():

    # Constants that will be used in the program
    APPLICATION_WIDTH = 500
    APPLICATION_HEIGHT = 600
    PADDLE_Y_OFFSET = 30
    BRICKS_PER_ROW = 10
    BRICK_SEP = 4
    BRICK_Y_OFFSET = 70
    BRICK_WIDTH = (APPLICATION_WIDTH -
                   (BRICKS_PER_ROW - 1) * BRICK_SEP) / BRICKS_PER_ROW
    BRICK_HEIGHT = 10
    PADDLE_WIDTH = 60
    PADDLE_HEIGHT = 10
    RADIUS_OF_BALL = 10
    NUM_TURNS = 3
    BLACK = (0, 0, 0)

    # Step 1: Use loops to draw the rows of bricks. The top row of bricks should be 70 pixels away from the top of
    # the screen (BRICK_Y_OFFSET)
    pygame.init()
    # Create a window for the game
    mainSurface = pygame.display.set_mode(
        (APPLICATION_WIDTH, APPLICATION_HEIGHT), 32, 0)
    pygame.display.set_caption("Break Out")
    bricksGroup = pygame.sprite.Group()
    paddleGroup = pygame.sprite.Group()

    # Create a pile of bricks
    x = 0
    y = BRICK_Y_OFFSET
    bricks = 10
    for rows in range(10):
        for number in range(bricks):
            pile = brick.Brick(BRICK_WIDTH, BRICK_HEIGHT, color)
            pile.rect.x = x
            pile.rect.y = y
            bricksGroup.add(pile)
            # Add the row to the window(surface)
            mainSurface.blit(pile.image, pile.rect)
            x = x + BRICK_WIDTH + BRICK_SEP
        # Start over at x = 0, but move up by the brick height + brick separation to draw two new rows
        x = 0
        y = y + BRICK_HEIGHT + BRICK_SEP
    pygame.display.update()

    # Step 2: Create a paddle
    board = paddle.Paddle(mainSurface, BLACK, PADDLE_WIDTH, PADDLE_HEIGHT)
    # The paddle starts at the middle of the screen and has a 30 pixel distance away from the bottom.
    board.rect.x = APPLICATION_WIDTH / 2
    board.rect.y = APPLICATION_HEIGHT - PADDLE_Y_OFFSET
    # Add the paddle to the main surface
    mainSurface.blit(board.image, board.rect)
    paddleGroup.add(board)
    pygame.display.update()

    # Step 3: Create a ball
    circle = ball.Ball(BLACK, APPLICATION_WIDTH, APPLICATION_HEIGHT,
                       RADIUS_OF_BALL)
    # The ball starts at the middle of the window
    circle.rect.x = APPLICATION_WIDTH / 2
    circle.rect.y = APPLICATION_HEIGHT / 2
    pygame.display.update()

    background = pygame.image.load("cat.png")
    background_rect = background.get_rect()
    background_rect.x = 0
    background_rect.y = 0
    while True:
        for event in pygame.event.get():
            if event == QUIT:
                pygame.quit()
                sys.exit()
        # Fill the window with the image every time the ball or the paddle moves
        mainSurface.blit(background, background_rect)
        # Add all the bricks on the window every time the ball or the paddle moves
        for cubes in bricksGroup:
            mainSurface.blit(cubes.image, cubes.rect)
        board.move()
        mainSurface.blit(board.image, board.rect)
        circle.move()
        circle.collision(paddleGroup)
        circle.collisionBrick(bricksGroup)
        # There are three turns in total to play breakout.
        # If the turn becomes 0(the ball hits the ball for three times), the player loses and the game stops and quits.
        if circle.rect.bottom >= APPLICATION_HEIGHT:
            # Every time the ball hits the bottom, the number of turns(3) will minus 1.
            NUM_TURNS = NUM_TURNS - 1
            # Reset the position of the ball.
            circle.rect.x = APPLICATION_WIDTH / 2
            circle.rect.y = APPLICATION_HEIGHT / 2
            # If the ball hits the bottom, the lose sound will be played.
            lose_sound = pygame.mixer.Sound("dk_dawae.wav")
            lose_sound.play()
            # The game waits for a second.
            pygame.time.wait(1000)
            # The ball will move with its original speed from the reset position.
            circle.speedx = 5
            circle.speedy = 6
            # If the user loses, the game waits for 1.5 seconds and quits afterwards.
            if NUM_TURNS == 0:
                pygame.time.wait(1500)
                pygame.quit()
                sys.exit()
        # If all bricks are eliminated(have collided with the ball), the game waits for 3 seconds.
        # It will play the winning sound, and quit.
        if len(bricksGroup) == 0:
            win_sound = pygame.mixer.Sound("clicking.wav")
            win_sound.play()
            pygame.time.wait(3000)
            pygame.quit()
            sys.exit()
        mainSurface.blit(circle.image, circle.rect)
        pygame.display.update()
예제 #17
0
def main():

    # Constants that will be used in the program
    APPLICATION_WIDTH = 400
    APPLICATION_HEIGHT = 600
    PADDLE_Y_OFFSET = 30
    BRICKS_PER_ROW = 10
    BRICK_SEP = 4  # The space between each brick
    BRICK_Y_OFFSET = 70
    BRICK_WIDTH = (APPLICATION_WIDTH -
                   (BRICKS_PER_ROW - 1) * BRICK_SEP) / BRICKS_PER_ROW
    BRICK_HEIGHT = 8
    PADDLE_WIDTH = 60
    PADDLE_HEIGHT = 10
    RADIUS_OF_BALL = 10
    NUM_TURNS = 3

    # Sets up the colors
    RED = (255, 0, 0)
    ORANGE = (255, 165, 0)
    YELLOW = (255, 255, 0)
    GREEN = (0, 255, 0)
    CYAN = (0, 255, 255)
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    colors = [
        RED, RED, ORANGE, ORANGE, YELLOW, YELLOW, GREEN, GREEN, CYAN, CYAN
    ]

    # Step 1: Use loops to draw the rows of bricks. The top row of bricks should be 70 pixels away from the top of
    # the screen (BRICK_Y_OFFSET)

    pygame.init()
    mainSurface = pygame.display.set_mode(
        (APPLICATION_WIDTH, APPLICATION_HEIGHT), 0, 32)
    mainSurface.fill((BLACK))
    pygame.display.set_caption(" Ultimate Breakout Z")

    background_image = pygame.image.load("Dragonball.png")
    # Changing the background image to a custom one.
    background_rect = background_image.get_rect()
    background_rect.x = 0
    background_rect.y = 0
    mainSurface.blit(background_image, background_rect)

    x = 0
    y = BRICK_Y_OFFSET

    brick_group = pygame.sprite.Group()
    paddle_group = pygame.sprite.Group()

    # Places the Paddle at the center of the application width and at the 30 offset of the bottom
    paddle_ = paddle.Paddle(mainSurface, WHITE, PADDLE_WIDTH, PADDLE_HEIGHT)
    paddle_.rect.x = APPLICATION_WIDTH / 2
    paddle_.rect.y = APPLICATION_HEIGHT - PADDLE_Y_OFFSET
    mainSurface.blit(paddle_.image, paddle_.rect)
    paddle_group.add(paddle_)

    # This places the ball at the center of the application
    bally = ball.Ball(RED, APPLICATION_WIDTH, APPLICATION_HEIGHT,
                      RADIUS_OF_BALL)
    bally.rect.x = APPLICATION_WIDTH / 2
    bally.rect.y = APPLICATION_HEIGHT / 2
    mainSurface.blit(bally.image, bally.rect)

    for m in range(BRICKS_PER_ROW):
        color = colors[m]
        x = 0
        for b in range(BRICKS_PER_ROW):

            bricks = brick.Brick(BRICK_WIDTH, BRICK_HEIGHT, color)

            brick_group.add(bricks)
            bricks.rect.x = x
            bricks.rect.y = y
            mainSurface.blit(bricks.image, bricks.rect)
            x = x + BRICK_WIDTH + BRICK_SEP
            # creates a row of bricks
        pygame.display.update()
        y = y + BRICK_HEIGHT + BRICK_SEP
    #     Creates the other row of bricks under the following one

    while True:
        for event in pygame.event.get():
            if event == QUIT:
                pygame.quit()
                sys.exit()
        mainSurface.blit(background_image, background_rect)
        mainSurface.blit(bally.image, bally.rect)
        for x in brick_group:
            mainSurface.blit(x.image, x.rect)
        paddle_.move()
        mainSurface.blit(paddle_.image, paddle_.rect)
        bally.move()
        bally.collide_paddle(paddle_group)
        # When the ball collides with the paddle
        bally.collide(brick_group)
        # When the ball collides with the bricks
        if bally.rect.bottom >= APPLICATION_HEIGHT:
            bally.rect.x = APPLICATION_WIDTH / 2
            bally.rect.y = APPLICATION_HEIGHT / 2
            NUM_TURNS = NUM_TURNS - 1
            pygame.time.delay(6000)
        if NUM_TURNS == 0:
            pygame.quit()
            sys.exit()
        # Exits the game when the player is out of turns
        mainSurface.blit(bally.image, bally.rect)

        pygame.display.update()
예제 #18
0
def main():
    # Constants that will be used in the program
    APPLICATION_WIDTH = 400
    APPLICATION_HEIGHT = 600
    PADDLE_Y_OFFSET = 30
    BRICKS_PER_ROW = 8
    BRICK_SEP = 15  # The space between each brick
    BRICK_Y_OFFSET = 70
    BRICK_WIDTH = (APPLICATION_WIDTH -
                   (BRICKS_PER_ROW - 1) * BRICK_SEP) / BRICKS_PER_ROW
    BRICK_HEIGHT = 16
    PADDLE_WIDTH = 60
    PADDLE_HEIGHT = 10
    RADIUS_OF_BALL = 7
    NUM_TURNS = 3
    HEIGHT = 20
    SPACE = 5

    # Sets up the colors
    RED = (250, 0, 255)
    ORANGE = (255, 165, 0)
    YELLOW = (255, 255, 0)
    GREEN = (0, 255, 0)
    CYAN = (0, 255, 255)
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)

    colors = [ORANGE, RED, YELLOW, GREEN, CYAN]
    files = ["five.png", "four.png", "three.png", "two.png", "one.png"]
    # files is what pygame pulls the images from and puts them on the screen

    # Step 1: Use loops to draw the rows of bricks. The top row of bricks should be 70 pixels away from the top of
    # the screen (BRICK_Y_OFFSET)
    pygame.init()
    main_surface = pygame.display.set_mode(
        (APPLICATION_WIDTH, APPLICATION_HEIGHT), 0, 32)
    background = pygame.image.load("Pool Table.png")
    background_rect = background.get_rect()
    background_rect.x = 0
    background_rect.y = 0
    h = APPLICATION_HEIGHT - PADDLE_Y_OFFSET
    w = APPLICATION_WIDTH / 2
    paddle_group = pygame.sprite.Group()
    board = paddle.Paddle(main_surface, BLACK, PADDLE_WIDTH, PADDLE_HEIGHT)
    board.rect.x = w
    board.rect.y = h
    main_surface.blit(board.image, board.rect)
    paddle_group.add(board)
    cj = ball.Ball(WHITE, APPLICATION_WIDTH, APPLICATION_HEIGHT,
                   RADIUS_OF_BALL)
    cj.rect.x = APPLICATION_WIDTH / 2
    cj.rect.y = APPLICATION_HEIGHT - 50
    main_surface.blit(cj.image, cj.rect)
    brick_group = pygame.sprite.Group()
    x = 0
    y = 5
    # This adds two bricks per color per ball so for example there would be two balls with the number 1 on it
    for file in files:
        for c in range(2):
            for b in range(BRICKS_PER_ROW):
                my_brick = block.Block(BRICK_WIDTH, BRICK_HEIGHT, file)
                brick_group.add(my_brick)
                my_brick.rect.x = x
                my_brick.rect.y = y
                main_surface.blit(my_brick.image, my_brick.rect)
                x = x + BRICK_WIDTH + BRICK_SEP
            y = y + BRICK_HEIGHT + BRICK_SEP
            x = 0

    WIDTH = (500 - (BRICKS_PER_ROW * SPACE)) / BRICKS_PER_ROW
    lose_sound = pygame.mixer.Sound("aww.wav")
    # This while true loop blits the background to the screen and then takes away a turn if the player loses the game
    while True:
        for event in pygame.event.get():
            if event == QUIT:
                pygame.quit()
                sys.exit()
        main_surface.blit(background, background_rect)
        for a in brick_group:
            main_surface.blit(a.image, a.rect)
        board.move()
        main_surface.blit(board.image, board.rect)
        cj.collide(brick_group)
        cj.move()
        cj.paddle_collide(paddle_group)
        main_surface.blit(cj.image, cj.rect)

        pygame.display.update()
        if cj.rect.bottom >= APPLICATION_HEIGHT:
            NUM_TURNS -= 1
        if NUM_TURNS == 0:
            lose_sound.play()
            pygame.time.delay(2000)
            pygame.quit()
            sys.exit()
        if len(brick_group) == 0:
            pygame.quit()
            sys.exit()
예제 #19
0
파일: Player.py 프로젝트: lthoangg/se20
 def __init__(self, name):
     self.name = name
     self.id = next(self.ID)
     self.character = paddle.Paddle(self.id)
     self.score = 0
예제 #20
0
# Create a screen objet from turtle module
screen = turtle.Screen()

# Setting up screen elements
screen.bgcolor("black")
screen.setup(width=800, height=600)
screen.title("Pong Game")
screen.tracer(0)

# Creating a ball object
ball = ball.Ball()

# Creating a scoreboar
scoreboard = scoreboard.Scoreboard()

right_paddle = paddle.Paddle(INITIAL_POSITION_RIGHT[0], INITIAL_POSITION_RIGHT[1])
left_paddle = paddle.Paddle(INITIAL_POSITION_LEFT[0], INITIAL_POSITION_LEFT[1])

# Setting up a screen listener
screen.listen()
screen.onkey(right_paddle.move_up, "Up")
screen.onkey(right_paddle.move_down, "Down")
screen.onkey(left_paddle.move_up, "w")
screen.onkey(left_paddle.move_down, "s")

game_is_on = True

while game_is_on:
    screen.update()
    ball.move(left_paddle, right_paddle, scoreboard)
    time.sleep(ball.pace)
예제 #21
0
def main():
    # Constants that will be used in the program
    APPLICATION_WIDTH = 400
    APPLICATION_HEIGHT = 600
    PADDLE_Y_OFFSET = 30
    BRICKS_PER_ROW = 10
    BRICK_SEP = 4  # The space between each brick
    BRICK_Y_OFFSET = 70
    BRICK_WIDTH = (APPLICATION_WIDTH -
                   (BRICKS_PER_ROW - 1) * BRICK_SEP) / BRICKS_PER_ROW
    BRICK_HEIGHT = 8
    PADDLE_WIDTH = 60
    PADDLE_HEIGHT = 10
    RADIUS_OF_BALL = 10
    NUM_TURNS = 3

    # Sets up the colors
    RED = (255, 0, 0)
    ORANGE = (255, 165, 0)
    YELLOW = (255, 255, 0)
    GREEN = (0, 255, 0)
    CYAN = (0, 255, 255)
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)

    # Step 1: Use loops to draw the rows of bricks. The top row of bricks should be 70 pixels away from the top of
    # the screen (BRICK_Y_OFFSET)
    main_surface = pygame.display.set_mode(
        (APPLICATION_WIDTH, APPLICATION_HEIGHT), 0, 32)
    main_surface.fill((255, 255, 255))

    brick_colors = [RED, ORANGE, YELLOW, GREEN, CYAN]
    brick_group = pygame.sprite.Group()

    x_pos = BRICK_SEP
    y_pos = BRICK_Y_OFFSET

    for color in brick_colors:

        for x in range(2):

            for x in range(BRICKS_PER_ROW):
                MY_BRICK = brick.Brick(main_surface, BRICK_WIDTH, BRICK_HEIGHT,
                                       color)
                MY_BRICK.rect.x = x_pos
                MY_BRICK.rect.y = y_pos
                main_surface.blit(MY_BRICK.image, MY_BRICK.rect)
                brick_group.add(MY_BRICK)
                x_pos += BRICK_SEP + BRICK_WIDTH

            y_pos += BRICK_HEIGHT + BRICK_SEP
            x_pos = BRICK_SEP

    paddle_group = pygame.sprite.Group()
    MY_PADDLE = paddle.Paddle(main_surface, BLACK, PADDLE_WIDTH, PADDLE_HEIGHT)
    MY_PADDLE.rect.x = APPLICATION_WIDTH / 2
    MY_PADDLE.rect.y = APPLICATION_HEIGHT - PADDLE_Y_OFFSET
    paddle_group.add(MY_PADDLE)

    main_surface.blit(MY_PADDLE.image, MY_PADDLE.rect)

    MY_BALL = ball.Ball(BLACK, (APPLICATION_WIDTH), (APPLICATION_HEIGHT),
                        RADIUS_OF_BALL)
    MY_BALL.rect.x = APPLICATION_WIDTH / 2
    MY_BALL.rect.y = APPLICATION_HEIGHT / 2
    main_surface.blit(MY_BALL.image, MY_BALL.rect)

    while True:
        main_surface.fill(WHITE)
        MY_PADDLE.move(pygame.mouse.get_pos())
        main_surface.blit(MY_PADDLE.image, MY_PADDLE.rect)
        MY_BALL.move()
        main_surface.blit(MY_BALL.image, MY_BALL.rect)
        MY_BALL.collide(brick_group)
        MY_BALL.paddle_collide(paddle_group)
        for x in brick_group:
            main_surface.blit(x.image, x.rect)
        if MY_BALL.rect.y >= APPLICATION_HEIGHT:
            NUM_TURNS += -1
            if NUM_TURNS == 0:
                break
            else:
                MY_BALL.rect.x = APPLICATION_WIDTH / 2
                MY_BALL.rect.y = APPLICATION_HEIGHT / 2
        if NUM_TURNS == 0:
            GAME_OVER(main_surface)
        if len(
                brick_group
        ) == 0:  # when is no more bricks in the game the program will show WINNER
            GAME_WNNER(main_surface)
        main_surface.blit(MY_BALL.image, MY_BALL.rect)
        MY_BALL.paddle_collide(paddle_group)
        MY_BALL.brick_collide(brick_group)
        if NUM_TURNS == 0:
            break
        if brick_group == 0:
            break
        pygame.display.update()
        for event in pygame.event.get():
            if event == QUIT:
                pygame.quit()
                sys.exit()
예제 #22
0
def main():
    # Constants that will be used in the program
    application_width = 400
    application_height = 600
    paddle_y_offset = 30
    bricks_per_row = 10
    brick_sep = 4  # The space between each brick
    brick_y_offset = 70
    brick_width = (application_width -
                   (bricks_per_row - 1) * brick_sep) / bricks_per_row
    brick_height = 8
    paddle_width = 60
    paddle_height = 10
    radius_of_ball = 10
    num_turns = 3

    # Sets up the colors
    red = (255, 0, 0)
    black = (0, 0, 0)
    white = (255, 255, 255)
    purple = (195, 47, 191)

    bricks_group = pygame.sprite.Group()
    paddle_group = pygame.sprite.Group()

    # makes the main surface
    main_surface = pygame.display.set_mode(
        (application_width, application_height), 0, 32)
    main_surface.fill((0, 0, 0))
    background = pygame.image.load("revan.jpg")
    main_surface.blit(background, (0, 0))
    x_pos = brick_sep
    y_pos = brick_y_offset

    # makes the paddle
    my_paddle = paddle.Paddle(main_surface, (black), (paddle_width),
                              (paddle_height))
    my_paddle.rect.x = 200
    my_paddle.rect.y = 570
    main_surface.blit(my_paddle.image, my_paddle.rect)
    paddle_group.add(my_paddle)

    # makes the ball
    my_ball = ball.Ball(white, application_width, application_height,
                        radius_of_ball)
    my_ball.rect.x = 900
    my_ball.rect.y = 1000
    main_surface.blit(my_ball.image, my_ball.rect)
    pygame.display.update()

    # Makes the rows of bricks
    for x in range(bricks_per_row):
        my_brick = brick.Brick(brick_width, (brick_height), (red))
        my_brick.rect.x = x_pos
        my_brick.rect.y = y_pos
        bricks_group.add(my_brick)
        main_surface.blit(my_brick.image, (x_pos, y_pos))
        x_pos = x_pos + brick_sep + brick_width

    x_pos = brick_sep
    y_pos = y_pos + brick_height + brick_sep

    for y in range(bricks_per_row):
        my_brick = brick.Brick(brick_width, (brick_height), (red))
        my_brick.rect.x = x_pos
        my_brick.rect.y = y_pos
        bricks_group.add(my_brick)
        main_surface.blit(my_brick.image, (x_pos, y_pos))
        x_pos = x_pos + brick_sep + brick_width

    x_pos = brick_sep
    y_pos = y_pos + brick_height + brick_sep

    for y in range(bricks_per_row):
        my_brick = brick.Brick((brick_width), (brick_height), (purple))
        my_brick.rect.x = x_pos
        my_brick.rect.y = y_pos
        bricks_group.add(my_brick)
        main_surface.blit(my_brick.image, (x_pos, y_pos))
        x_pos = x_pos + brick_sep + brick_width

    x_pos = brick_sep
    y_pos = y_pos + brick_height + brick_sep

    for y in range(bricks_per_row):
        my_brick = brick.Brick((brick_width), (brick_height), (purple))
        my_brick.rect.x = x_pos
        my_brick.rect.y = y_pos
        bricks_group.add(my_brick)
        main_surface.blit(my_brick.image, (x_pos, y_pos))
        x_pos = x_pos + brick_sep + brick_width

    x_pos = brick_sep
    y_pos = y_pos + brick_height + brick_sep

    for y in range(bricks_per_row):
        my_brick = brick.Brick((brick_width), (brick_height), (red))
        my_brick.rect.x = x_pos
        my_brick.rect.y = y_pos
        bricks_group.add(my_brick)
        main_surface.blit(my_brick.image, (x_pos, y_pos))
        x_pos = x_pos + brick_sep + brick_width

    x_pos = brick_sep
    y_pos = y_pos + brick_height + brick_sep

    for y in range(bricks_per_row):
        my_brick = brick.Brick((brick_width), (brick_height), (red))
        my_brick.rect.x = x_pos
        my_brick.rect.y = y_pos
        bricks_group.add(my_brick)
        main_surface.blit(my_brick.image, (x_pos, y_pos))
        x_pos = x_pos + brick_sep + brick_width

    x_pos = brick_sep
    y_pos = y_pos + brick_height + brick_sep

    for y in range(bricks_per_row):
        my_brick = brick.Brick((brick_width), (brick_height), (purple))
        my_brick.rect.x = x_pos
        my_brick.rect.y = y_pos
        bricks_group.add(my_brick)
        main_surface.blit(my_brick.image, (x_pos, y_pos))
        x_pos = x_pos + brick_sep + brick_width

    x_pos = brick_sep
    y_pos = y_pos + brick_height + brick_sep

    for y in range(bricks_per_row):
        my_brick = brick.Brick((brick_width), (brick_height), (purple))
        my_brick.rect.x = x_pos
        my_brick.rect.y = y_pos
        bricks_group.add(my_brick)
        main_surface.blit(my_brick.image, (x_pos, y_pos))
        x_pos = x_pos + brick_sep + brick_width

    x_pos = brick_sep
    y_pos = y_pos + brick_height + brick_sep

    for y in range(bricks_per_row):
        my_brick = brick.Brick((brick_width), (brick_height), (red))
        my_brick.rect.x = x_pos
        my_brick.rect.y = y_pos
        bricks_group.add(my_brick)
        main_surface.blit(my_brick.image, (x_pos, y_pos))
        x_pos = x_pos + brick_sep + brick_width

    x_pos = brick_sep
    y_pos = y_pos + brick_height + brick_sep

    for y in range(bricks_per_row):
        my_brick = brick.Brick((brick_width), (brick_height), (red))
        my_brick.rect.x = x_pos
        my_brick.rect.y = y_pos
        bricks_group.add(my_brick)
        main_surface.blit(my_brick.image, (x_pos, y_pos))
        x_pos = x_pos + brick_sep + brick_width

    # loop that moves paddle and collides the ball and paddle as well as ends the game after 3 turns
    while True:
        main_surface.blit(background, (0, 0))
        for a_brick in bricks_group:
            main_surface.blit(a_brick.image, a_brick.rect)
        my_paddle.move(pygame.mouse.get_pos())
        my_ball.paddle_collide(paddle_group)
        my_ball.brick_collide(bricks_group)
        main_surface.blit(my_paddle.image, my_paddle.rect)
        my_ball.move()
        if my_ball.rect.bottom > application_height:
            num_turns -= 1
            my_ball.rect.x = 200
            my_ball.rect.y = 300
            main_surface.blit(my_ball.image, my_ball.rect)
        if num_turns == 0:
            main_surface.fill(white)
            myFont = pygame.font.SysFont("Times New Roman", 65)
            label = myFont.render("GAME OVER", 1, white)
            main_surface.blit(label, (200, 275))
            pygame.display.update()
            pygame.time.wait(100)
            break
        main_surface.blit(my_ball.image, my_ball.rect)
        pygame.display.update()
        for event in pygame.event.get():
            if event == QUIT:
                pygame.quit()
                sys.exit()
예제 #23
0
import paddle as p
import ball
import time
from scoreboard import ScoreBoard

my_screen = t.Screen()
my_screen.setup(width=800, height=600)
my_screen.bgcolor("black")
my_screen.title("my ping pong game ")
my_screen.tracer(0)

fast = 0

#paddle challenge
total_paddle = []
r_paddle = p.Paddle((-350, 0))

l_paddle = p.Paddle((350, 0))

ball = ball.Ball()

my_screen.listen()

my_screen.onkey(r_paddle.goUp, "Up")
my_screen.onkey(r_paddle.goDown, "Down")
my_screen.onkey(l_paddle.goUp, "w")
my_screen.onkey(l_paddle.goDown, "s")

score = ScoreBoard()

is_game_on = True
dash.ht()
dash.speed(0)
dash.color("white")
dash.sety(300)
dash.seth(270)
dash.pendown()
dash.forward(10)
scr.listen()
scr.tracer(0)
t = -1
l_socre = scoreboard.Score(-20)
r_socre = scoreboard.Score(20)

balls = ball.Ball()

paddle_l = paddle.Paddle(350, 0)
paddle_r = paddle.Paddle(-350, 0)
scr.onkeypress(paddle_l.up, "Up")
scr.onkeypress(paddle_l.down, "Down")
scr.onkeypress(paddle_r.up, "w")
scr.onkeypress(paddle_r.down, "s")

for i in range(300, -300, -10):
    if (t == 1):
        dash.pendown()
        dash.forward(10)
    else:
        dash.penup()
        dash.forward(10)
    t *= -1
예제 #25
0
파일: breakout.py 프로젝트: arianacd/unit11
def main():
    # Constants that will be used in the program
    APPLICATION_WIDTH = 400
    APPLICATION_HEIGHT = 600
    PADDLE_Y_OFFSET = 30
    BRICKS_PER_ROW = 10
    BRICK_SEP = 4  # The space between each brick
    BRICK_Y_OFFSET = 70
    BRICK_WIDTH =  (APPLICATION_WIDTH - (BRICKS_PER_ROW -1) * BRICK_SEP) / BRICKS_PER_ROW
    BRICK_HEIGHT = 8
    PADDLE_WIDTH = 60
    PADDLE_HEIGHT = 10
    RADIUS_OF_BALL = 10
    NUM_TURNS = 3

    # Sets up the colors
    RED = (255, 0, 0)
    ORANGE = (255, 165, 0)
    YELLOW = (255, 255, 0)
    GREEN =(0, 255, 0)
    CYAN = (0, 255, 255)
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)

    colors = [RED, ORANGE, YELLOW, GREEN, CYAN]

    # creates a sprite fo the sprite groups so the bricks and paddle can collide with the ball
    bricks_group = pygame.sprite.Group()
    paddle_group = pygame.sprite.Group()

    # creates a main surface with a picture as the background
    main_surface = pygame.display.set_mode((APPLICATION_WIDTH, APPLICATION_HEIGHT), 0, 32)
    bg = pygame.image.load("construction.jpeg")
    main_surface.blit(bg, (0, 0))

    # the following code displays the bricks on the screen
    x_pos = BRICK_SEP
    y_pos = BRICK_Y_OFFSET
    for hue in colors:
        for y in range(2):
            for x in range(BRICKS_PER_ROW):
                my_brick = brick.Brick(BRICK_WIDTH, BRICK_HEIGHT, hue)
                my_brick.rect.x = x_pos
                my_brick.rect.y = y_pos
                bricks_group.add(my_brick)
                main_surface.blit(my_brick.image, my_brick.rect)
                x_pos += BRICK_WIDTH + BRICK_SEP
            y_pos += BRICK_HEIGHT + BRICK_SEP
            x_pos = BRICK_SEP
    # displays the paddle
    my_paddle = paddle.Paddle(main_surface, BLACK, PADDLE_WIDTH, PADDLE_HEIGHT)
    paddle_group.add(my_paddle)
    my_paddle.rect.y = APPLICATION_HEIGHT - PADDLE_Y_OFFSET
    main_surface.blit(my_paddle.image, my_paddle.rect)
    # displays the ball
    my_ball = ball.Ball(BLACK, APPLICATION_WIDTH, APPLICATION_HEIGHT, RADIUS_OF_BALL)
    my_ball.rect.x = APPLICATION_WIDTH/2
    my_ball.rect.y = APPLICATION_HEIGHT/2
    main_surface.blit(my_ball.image, my_ball.rect)

    tries = 0

    while True:
        main_surface.fill(WHITE)
        main_surface.blit(bg, (0, 0))
        # makes the paddle move with the mouse
        for a_brick in bricks_group:
            main_surface.blit(a_brick.image, a_brick.rect)
        my_paddle.move(pygame.mouse.get_pos())
        main_surface.blit(my_paddle.image, my_paddle.rect)
        my_ball.move()
        my_ball.collide(paddle_group)
        my_ball.collide_brick(bricks_group)
        main_surface.blit(my_ball.image, my_ball.rect)
        end_sound = pygame.mixer.Sound('maybe-next-time.wav')
        # resets the ball if it goes off the screen
        if my_ball.rect.bottom >= APPLICATION_HEIGHT:
            end_sound.play()
            my_ball.rect.y = APPLICATION_HEIGHT / 2
            tries += 1
        # displays "game over" if the player looses. The >= makes the game over displays even when the ball
        # keeps hitting the bottom of the screen
        if tries >= 3:
            game_over(main_surface)
        # displays "winner!" when the player wins by breaking all the bricks
        if len(bricks_group) == 0:
            win_game(main_surface)
        pygame.display.update()
        for event in pygame.event.get():
            if event == QUIT:
                pygame.quit()
                sys.exit()
예제 #26
0
def main():
    # Constants that will be used in the program
    APPLICATION_WIDTH = 400
    APPLICATION_HEIGHT = 600
    PADDLE_Y_OFFSET = 30
    BRICKS_PER_ROW = 10
    BRICK_SEP = 4  # The space between each brick
    BRICK_Y_OFFSET = 70
    BRICK_WIDTH = (APPLICATION_WIDTH -
                   (BRICKS_PER_ROW - 1) * BRICK_SEP) / BRICKS_PER_ROW
    BRICK_HEIGHT = 8
    PADDLE_WIDTH = 60
    PADDLE_HEIGHT = 10
    RADIUS_OF_BALL = 10
    NUM_TURNS = 3
    ball_width = RADIUS_OF_BALL * 2
    ball_height = RADIUS_OF_BALL * 2
    # Sets up the colors
    RED = (255, 0, 0)
    ORANGE = (255, 165, 0)
    YELLOW = (255, 255, 0)
    GREEN = (0, 255, 0)
    CYAN = (0, 255, 255)
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    REDER = (200, 0, 0)
    BLUE = (0, 0, 255)
    lose = pygame.image.load("skull copy.png")
    heart = pygame.image.load("Pixel_heart3.png")
    wins = pygame.image.load("trophy copy.png")
    # Step 1: Use loops to draw the rows of bricks. The top row of bricks should be 70 pixels away from the top of
    # the screen (BRICK_Y_OFFSET)
    brick_group = pygame.sprite.Group()
    paddle_group = pygame.sprite.Group()
    pygame.init()
    main_window = pygame.display.set_mode(
        (APPLICATION_WIDTH, APPLICATION_HEIGHT), 32, 0)
    pygame.display.set_caption("Breakout")
    bg = pygame.image.load("ocean.png")
    xpos = 0
    ypos = BRICK_Y_OFFSET

    my_paddle = paddle.Paddle(main_window, RED, PADDLE_WIDTH, PADDLE_HEIGHT)
    paddle_group.add(my_paddle)
    my_paddle.rect.x = APPLICATION_WIDTH / 2
    my_paddle.rect.y = APPLICATION_HEIGHT - PADDLE_Y_OFFSET

    my_ball = ball.Ball(REDER, ball_width, ball_height, RADIUS_OF_BALL)
    my_ball.rect.x = 200
    my_ball.rect.y = 200

    colors = [RED, ORANGE, YELLOW, GREEN, CYAN]
    pygame.mixer.init()
    pygame.init()
    theme = pygame.mixer.Sound("Pirates of the Caribbean - Hes a Pirate-2.wav")
    theme.play(10)
    for color in colors:
        for x in range(2):
            for y in range(10):
                my_brick = brick.Brick(BRICK_WIDTH, BRICK_HEIGHT, color)
                brick_group.add(my_brick)
                my_brick.rect.x = xpos
                my_brick.rect.y = ypos
                xpos += (BRICK_SEP + BRICK_WIDTH)
            xpos = 0
            ypos += BRICK_SEP + BRICK_HEIGHT

    while True:

        mousefont = pygame.font.SysFont("Helvetica", 30)
        mouselable = mousefont.render(str(NUM_TURNS), 80, (255, 100, 100))
        main_window.blit(mouselable, (350, 30))
        main_window.blit(heart, (365, 26))
        pygame.display.update()
        for event in pygame.event.get():
            if event == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == MOUSEMOTION:
                my_paddle.move(pygame.mouse.get_pos())
        if my_ball.rect.bottom > 590:
            my_ball.rect.x = 200
            my_ball.rect.y = 200
            NUM_TURNS -= 1
            print(NUM_TURNS)
        if my_ball.rect.bottom > 580:
            pygame.mixer.init()
            pygame.init()
            sound = pygame.mixer.Sound("splash1.wav")
            sound.play()
        main_window.blit(bg, (0, 0))
        if len(brick_group) == 0:
            pygame.mixer.init()
            pygame.init()
            win = pygame.mixer.Sound("Winning-sound-effect.wav")
            win.play()

            mousefont = pygame.font.SysFont("Helvetica", 30)
            mouselable = mousefont.render("Victory", 1, (0, 255, 0))
            main_window.blit(mouselable, (170, 300))
            pygame.display.update()
            main_window.blit(wins, (170, 200))
            pygame.display.update()
            pygame.time.wait(10000)
        if NUM_TURNS == 1 and my_ball.rect.bottom > 580:
            main_window.blit(lose, (75, 250))
            pygame.display.update()
        if NUM_TURNS == 0:
            pygame.time.wait(100000)
        my_ball.move(NUM_TURNS)
        my_ball.colide(paddle_group, brick_group)
        main_window.blit(my_ball.image, my_ball.rect)

        for a_brick in brick_group:
            main_window.blit(a_brick.image, a_brick.rect)

        main_window.blit(my_paddle.image, my_paddle.rect)

        pygame.display.update()
예제 #27
0
def main():
    # Constants that will be used in the program
    APPLICATION_WIDTH = 400
    APPLICATION_HEIGHT = 600
    PADDLE_Y_OFFSET = 30
    BRICKS_PER_ROW = 10
    BRICK_SEP = 4  # The space between each brick
    BRICK_Y_OFFSET = 70
    BRICK_WIDTH = (APPLICATION_WIDTH -
                   (BRICKS_PER_ROW - 1) * BRICK_SEP) / BRICKS_PER_ROW
    BRICK_HEIGHT = 8
    PADDLE_WIDTH = 60
    PADDLE_HEIGHT = 10
    RADIUS_OF_BALL = 10
    NUM_TURNS = 3

    # Sets up the colors
    RED = (255, 0, 0)
    ORANGE = (255, 165, 0)
    YELLOW = (255, 255, 0)
    GREEN = (0, 255, 0)
    CYAN = (0, 255, 255)
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    color = [RED, ORANGE, YELLOW, GREEN, CYAN]
    mainSurface = pygame.display.set_mode(
        (APPLICATION_WIDTH, APPLICATION_HEIGHT), 32, 0)
    mainSurface.fill(WHITE)
    # Step 1: Use loops to draw the rows of bricks. The top row of bricks should be 70 pixels away from the top of
    # the screen (BRICK_Y_OFFSET)
    brick_group = pygame.sprite.Group()
    paddle_group = pygame.sprite.Group()
    pygame.init()
    x = 0
    y = BRICK_Y_OFFSET
    for q in color:
        for z in range(2):
            for c in range(BRICKS_PER_ROW):
                my_brick = brick.Brick(BRICK_WIDTH, BRICK_HEIGHT, q)
                brick_group.add(my_brick)
                my_brick.rect.x = x
                my_brick.rect.y = y
                mainSurface.blit(my_brick.image, my_brick.rect)
                x = x + BRICK_WIDTH + BRICK_SEP
            y = y + BRICK_HEIGHT + BRICK_SEP
            x = 0

    paddle_1 = paddle.Paddle(mainSurface, BLACK, PADDLE_WIDTH, PADDLE_HEIGHT)
    paddle_1.rect.x = APPLICATION_WIDTH / 2
    paddle_1.rect.y = APPLICATION_HEIGHT - PADDLE_Y_OFFSET
    mainSurface.blit(paddle_1.image, paddle_1.rect)
    paddle_group.add(paddle_1)

    ball_1 = ball.Ball(BLACK, APPLICATION_WIDTH, APPLICATION_HEIGHT,
                       RADIUS_OF_BALL)
    ball_1.rect.x = APPLICATION_HEIGHT / 2
    ball_1.rect.y = APPLICATION_WIDTH / 2
    mainSurface.blit(ball_1.image, ball_1.rect)

    win_sound = pygame.mixer.Sound("Yay.wav")
    lose = pygame.mixer.Sound("lose.wav")
    lost = pygame.mixer.Sound("no.wav")

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
        mainSurface.fill(WHITE)
        for x in brick_group:
            mainSurface.blit(x.image, x.rect)
        paddle_1.move()
        ball_1.move()
        ball_1.collide(paddle_group)
        ball_1.collideBrick(brick_group)
        mainSurface.blit(paddle_1.image, paddle_1.rect)
        mainSurface.blit(ball_1.image, ball_1.rect)

        if len(brick_group) == 0:
            print("You Won!")
            win_sound.play()
        if ball_1.rect.bottom >= APPLICATION_HEIGHT:
            ball_1.rect.x = APPLICATION_HEIGHT / 2
            ball_1.rect.y = APPLICATION_WIDTH / 2
            NUM_TURNS -= 1
            lose.play()
        if NUM_TURNS == 0:
            print("Game Over")
            lose.play()
            lost.play()
            pygame.quit()
            sys.exit()
        pygame.display.update()
예제 #28
0
pygame.display.set_caption('Pong')

# Enable this to make the mouse disappear when over our window
pygame.mouse.set_visible(0)

# This is a font we use to draw text on the screen (size 36)
font = pygame.font.Font(None, 36)

# Create a surface we can draw on
background = pygame.Surface(screen.get_size())

# Create the ball
ball = b.GameBall()

# Create the player paddle object
player = p.Paddle(580)

movingsprites = pygame.sprite.Group()
movingsprites.add(player)
movingsprites.add(ball)

handler = d.InputHandler()

clock = pygame.time.Clock()
done = False
exit_program = False

while not exit_program:

    screen.fill(c.BLACK)
예제 #29
0
import pygame, sys, os
import ball, paddle
import font
from pygame.locals import *

pygame.init()

window = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Python Pong")

FPS = 30
Frame = pygame.time.Clock()

b = ball.Ball()
b._init_(320, 0)
pad = paddle.Paddle()
pad._init_(50, 200)
enemy = paddle.Paddle()
enemy._init_(575, 200)

white = pygame.Color(255, 255, 255)

up, down = 0, 0
score1, score2 = 0, 0

#game loop
while True:
    window.fill(pygame.Color(0, 0, 0))
    #quit the game
    for e in pygame.event.get():
        if e.type == QUIT:
예제 #30
0
from turtle import Screen, Turtle
import paddle
import ball
import time
import scoreboard


t = Turtle()
screen = Screen()
screen.tracer(0)
screen.setup(width=1000, height=600)
screen.title("Pong Game")
screen.bgcolor("black")
screen.listen()

p1 = paddle.Paddle(480, 0)
p2 = paddle.Paddle(-490, 0)
p3 = Turtle()
b = ball.Ball()
player_1 = scoreboard.Scoreboard(-250, 250)
player_2 = scoreboard.Scoreboard(250, 250)


screen.onkey(p1.up, "Up")
screen.onkey(p1.down, "Down")
screen.onkey(p2.up, "w")
screen.onkey(p2.down, "s")

game_on = True
while game_on:
    b.refresh()