stoneskin

learning coding with Scratch , Python and Java

2.3 Continue Build Smiley Pong Game

3 Make it a Pong game

3.1 diagram of game logic

smiley pong

3.2 Code of the Smaley face Pong game

import pygame  

pygame.init()
screen = pygame.display.set_mode([800,600])
pygame.display.set_caption("Happy Face")
keep_going = True
pic = pygame.image.load("happyface.png")
color_key = pic.get_at((0,0))  
pic.set_colorkey(color_key) #Set the transparent colorkey
pic_x = 0
pic_y = 0
BLACK = (0,0,0)
WHITE = (255,255,255)
timer = pygame.time.Clock()
speed_x = 5
speed_y = 5

pic_w = pic.get_width()
pic_h = pic.get_height()

points = 0
lives = 5
font = pygame.font.SysFont("Times", 24)

# define paddle size and location
paddle_w = 200
paddle_h = 25
paddle_x = 300
paddle_y = 550


while keep_going:    
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            keep_going = False
        # Add event when you click F5 to restrat game
        if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_F1:    # F1 = New Game
                    points = 0
                    lives = 5
                    pic_x = 0
                    pic_y = 0
                    speed_x = 5
                    speed_y = 5  
                
    pic_x += speed_x
    pic_y += speed_y
    
    # Update the logic to handle the bounce
    if pic_x <= 0 or pic_x >= 700:
        speed_x = -speed_x * 1.1
    if pic_y <= 0:
        speed_y = -speed_y + 1
    if pic_y >= 500:
        lives -= 1
        speed_y = -5
        speed_x = 5
        pic_y = 499
        
   
    
    screen.fill(BLACK)    
    screen.blit(pic, (pic_x, pic_y))  #draw the picture on the screen
    
    
    # Draw paddle
    paddle_x = pygame.mouse.get_pos()[0]
    paddle_x -= paddle_w/2
    pygame.draw.rect(screen, WHITE, (paddle_x, paddle_y, paddle_w, paddle_h))
    
    # Check for paddle bounce
    if pic_y + pic_h >= paddle_y and pic_y + pic_h <= paddle_y + paddle_h \
       and speed_y > 0:
        if pic_x + pic_w/2 >= paddle_x and pic_x + pic_w/2 <= paddle_x + \
           paddle_w:
            speed_y = -speed_y
            points += 1

    # Check whether the game is over, and Draw text on screen
    draw_string = "Lives: " + str(lives) + " Points: " + str(points)
    if lives < 1:   
        speed_x = speed_y = 0
        draw_string = "Game Over. Your score was: " + str(points)
        draw_string += ". Press F1 to play again. "
        
    text = font.render(draw_string, True, WHITE)
    text_rect = text.get_rect()
    text_rect.centerx = screen.get_rect().centerx
    text_rect.y = 10
    screen.blit(text, text_rect)
       
    pygame.display.update()
    timer.tick(60)  # how many frames every sec, https://www.pygame.org/docs/ref/time.html

# Exit    
pygame.quit() 

3.3 Load Sound with Game

Save the sound files to your folder blip.wav blap.wav

Code of load sound:

pygame.mixer.init() # add sounds
blip = pygame.mixer.Sound("blip.wav")
blap = pygame.mixer.Sound("blap.wav")

Code of play sound

    # check for paddle bounce
    if picy + pich >= paddley and picy + pich <= paddley + paddleh  and speedy > 0:
        if picx + picw/2 >= paddlex and picx + picw/2 <= paddlex + paddlew:
            speedy = -speedy
            points += 1
            blip.play()

Final Code of Smiley Pong Game

3.4 Key points

3.4.1 Add the paddle

Define the paddle size, color and init location

WHITE = (255,255,255)
paddle_w = 200
paddle_h = 25
paddle_x = 300
paddle_y = 550

Draw paddle in the game loop, the paddle location will get from mouse location

    paddle_x = pygame.mouse.get_pos()[0]
    paddle_x -= paddle_w/2
    pygame.draw.rect(screen, WHITE, (paddle_x, paddle_y, paddle_w, paddle_h))

upper code use pygame.mouse object and call the function get_pos() to get the location array [x,y], the array first item index is 0, so use [0] to get the mouse x location

pygame.draw object has function rect(screen,color,x,y,with,height) for more please read draw rectangle in pygame

3.4.2 Score and Life

define the score and life

points = 0
lives = 5

when the ball touch the bottom, lose one life, touch the paddle earn one point

if pic_y >= 500:
        lives -= 1
# Check for paddle bounce
    if pic_y + pic_h >= paddle_y and pic_y + pic_h <= paddle_y + paddle_h \
       and speed_y > 0:
        if pic_x + pic_w/2 >= paddle_x and pic_x + pic_w/2 <= paddle_x + \
           paddle_w:
            speed_y = -speed_y
            points += 1

3.4.3 Show the score and Life

define font used for display the life and score

font = pygame.font.SysFont("Times", 24)

define and draw the string in the game loop

  draw_string = "Lives: " + str(lives) + " Points: " + str(points)

  text = font.render(draw_string, True, WHITE)
  text_rect = text.get_rect()
  text_rect.centerx = screen.get_rect().centerx # get the center location of the screen, not hard code 300 or 400 
  text_rect.y = 10
  screen.blit(text, text_rect)

3.4.4 Define the game over

Add below code to loop will check the game over and display the message

if lives < 1:   
        speed_x = speed_y = 0
        draw_string = "Game Over. Your score was: " + str(points)
        draw_string += ". Press F1 to play again. "
        

Ref