Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[XP] Pong Minigame

Pong Minigame
Version: 1.0

Introduction
A simple pong minigame, can be easily customized if needed. I haven't used RPG Maker XP for a long time, nor been on the forums (some of you will probably remember me). Some things could have been coded better, it was more of a personal test to see if I still knew how use the script language.

Screenshots
http://img215.imageshack.us/img215/6112/ss1sf7.png[/img]

http://img225.imageshack.us/img225/2446/ss2gq4.png[/img]

Script
Code:
#===============================================================================
# ** Pong Mini game
#------------------------------------------------------------------------------
#    Scripted by Freakboy
#    Version     1.0
#    Build Date  01-09-2008 (DD-MM-YYYY)
#-------------------------------------------------------------------------------
# The good old pong minigame! Enjoy playing
# 
# * This game is easily customizable
# You don't need to worry when you want to use different images for the paddles
# and balls, you can change images at the setup below without worrying about
# the size of the images. Collision calculations are done by the sizes of the
# image. Also, you can use different images for the player, ai paddle 
# and the ball!
# 
# Change the PADDLE_SPEED to make the paddles move faster. The faster the
# paddles move, how more difficult it is to beat the opponent.
#
# NOTE: All pictures EXCEPT the background picture go in the picture folder,
# the background image MUST be in the panorama folder!
#
# To use the script, use $scene = Scene_Pong.new
#===============================================================================

# Background Image
PONG_BACKGROUND = "007-Ocean01"
# Picture of player's paddle
PADDLE_PLAYER_IMAGE = "pong-paddle"
# Picture of ai's paddle
PADDLE_AI_IMAGE     = "pong-paddle"
# Picture of the ball
BALL_IMAGE   = "pong-ball"
# Paddle's movement speed
PADDLE_SPEED = 5
# Ball's movement speed
BALL_SPEED   = 4
# Make the speed of the ball increase when bounce?
INCREASE_SPEED_ON_BOUNCE = true
# Speed to Increase
INCREASE_BALL_SPEED = 0.1


#==============================================================================
# ** Scene_Pong
#------------------------------------------------------------------------------
#  This class performs the pong mini game
#==============================================================================

class Scene_Pong
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main()
    # Create background
    @pong_background = Sprite.new()
    @pong_background.bitmap = RPG::Cache.panorama(PONG_BACKGROUND, 0)
    # Create Player paddle
    @paddle_player = Sprite.new()
    @paddle_player.bitmap = RPG::Cache.picture(PADDLE_PLAYER_IMAGE)
    @paddle_player.x = 32
    @paddle_player.y = ((480 / 2) - (@paddle_player.bitmap.height / 2))
    # Create AI paddle
    @paddle_ai = Sprite.new()
    @paddle_ai.bitmap = RPG::Cache.picture(PADDLE_AI_IMAGE)
    @paddle_ai.x = 640 - 32 - @paddle_ai.bitmap.width
    @paddle_ai.y = ((480 / 2) - (@paddle_ai.bitmap.height / 2))
    # Create ball
    @pong_ball = Sprite.new()
    @pong_ball.bitmap = RPG::Cache.picture(BALL_IMAGE)
    @pong_ball.x = (640 / 2) - (@pong_ball.bitmap.width / 2)
    @pong_ball.y = (480 / 2) - (@pong_ball.bitmap.height / 2)
    # Create Score Window
    @pong_window = Window_Pong.new()
    # Make the window draw the "press space to continue"
    @pong_window.update_start_game(false)
    # Set ball speed
    @pong_ball_speed = BALL_SPEED
    # Set first direction
    @pong_ball_direction = [1, 3, 7, 9][rand(4)]
    # Can start the game?
    @start_game = false
    # Hold score for Player
    @score_player = 0
    # Hold score for AI
    @score_ai = 0
    # Execute transition
    Graphics.transition
    # Main loop
    loop do
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Frame update
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    # Prepare for transition
    Graphics.freeze
    # Dispose of content
    @paddle_player.dispose
    @paddle_ai.dispose
    @pong_ball.dispose
    @pong_background.dispose
    @pong_window.dispose
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update()
    # return to the map when pressing the x or escape button.
    if Input.trigger?(Input::B)
      $scene = Scene_Map.new
      return
    end
    # 'Pause' the game until hitting the space key
    if (not @start_game)
      @pong_window.update_start_game(false)
      if Input.trigger?(Input::C)
        @start_game = true
      end
      return
    end
    # Update sprites and window
    update_content()
    # Update score for winning or losing
    update_score
    # Update user input from the keyboard
    update_user_input()
    # Update collision of the ball
    update_ball_collision()
    # Update movement of the ball
    update_ball_movement()
    # Update AI movement
    update_ai_movement()
  end
  
  #--------------------------------------------------------------------------
  # * Update Content
  #--------------------------------------------------------------------------
  def update_content()
    @paddle_player.update
    @paddle_ai.update
    @pong_ball.update
    @pong_background.update
    @pong_window.update_start_game(@start_game)
    @pong_window.update_score(@score_player, @score_ai)
  end
  #--------------------------------------------------------------------------
  # * Update Score
  # Check if the ball hits the side walls and update the score
  #--------------------------------------------------------------------------
  def update_score
    # If the ball touches the left wall
    if (@pong_ball.x <= 0)
      # Player loses and AI gets added a point
      @score_ai += 1
      # Reset the game
      reset_pong_game()
      return
    end
    
    if (@pong_ball.x >= 640 + @pong_ball.bitmap.width)
      # AI loses and Player gets added a point
      @score_player += 1
      # Reset the game
      reset_pong_game()
      return
    end
  end
  #--------------------------------------------------------------------------
  # * Reset Pong Game
  # Resets the paddle and ball positions and wait for user input
  #--------------------------------------------------------------------------
  def reset_pong_game()
    # Update sprites and window
    update_content()
    # Make the game wait for input
    @start_game = false
    # Reset Player's paddle position
    @paddle_player.x = 32
    @paddle_player.y = ((480 / 2) - (@paddle_player.bitmap.height / 2))
    # Reset AI's paddle position
    @paddle_ai.x = 640 - 32 - @paddle_ai.bitmap.width
    @paddle_ai.y = ((480 / 2) - (@paddle_ai.bitmap.height / 2))
    # Reset the ball position
    @pong_ball.x = (640 / 2) - (@pong_ball.bitmap.width / 2)
    @pong_ball.y = (480 / 2) - (@pong_ball.bitmap.height / 2)
    # Reset the ball speed
    @pong_ball_speed = BALL_SPEED
    # Reset ball direction
    @pong_ball_direction = [1, 3, 7, 9][rand(4)]
  end
  
  #--------------------------------------------------------------------------
  # * Update User Input
  # This part handles the input of the player.
  #--------------------------------------------------------------------------
  def update_user_input()
    # If the player presses the up key
    if Input.press?(Input::DOWN)
      # Move the player's paddle up
      @paddle_player.y = [[@paddle_player.y + PADDLE_SPEED, 0].max, 480-@paddle_player.bitmap.height].min
    end
    # If the player presses the down key
    if Input.press?(Input::UP)
      # Move the player's paddle down
      @paddle_player.y = [[@paddle_player.y - PADDLE_SPEED, 0].max, 480-@paddle_player.bitmap.height].min
    end
  end
  #--------------------------------------------------------------------------
  # * Update Ball Speed
  #--------------------------------------------------------------------------
  def update_ball_speed()
    # Make the speed of the ball increase when bounce?
    if (INCREASE_SPEED_ON_BOUNCE)
      # Increase the speed
      @pong_ball_speed += INCREASE_BALL_SPEED
    end
  end
  #--------------------------------------------------------------------------
  # * Update Ball Collision
  # This part handles the collision against the wall and paddles.
  #--------------------------------------------------------------------------
  def update_ball_collision()
    case @pong_ball_direction
    # When the ball is moving left/down
    when 1
      # If the ball touches the floor
      if (@pong_ball.y >= (480 - @pong_ball.bitmap.height))
        # Change the ball direction to up/left
        @pong_ball_direction = 7
        # update movement speed of the ball
        update_ball_speed()
        return
      end
      # If the ball touches the player
      if (collision_ball_player?)
        # Change the ball direction to right/down
        @pong_ball_direction = 3
        # update movement speed of the ball
        update_ball_speed()
        return
      end
    
    # When the ball is moving left/up
    when 7
      # If the ball touches the ceiling
      if (@pong_ball.y <= 0)
        # Change the ball direction to left/down
        @pong_ball_direction = 1
        # update movement speed of the ball
        update_ball_speed()
        return
      end
      # If the ball touches the player
      if (collision_ball_player?)
        # Change the ball direction to up/right
        @pong_ball_direction = 9
        # update movement speed of the ball
        update_ball_speed()
        return
      end
    
    # When the ball is moving right/down
    when 3
      # If the ball touches the floor
      if (@pong_ball.y >= (480 - @pong_ball.bitmap.height))
        # Change the ball direction to right/up
        @pong_ball_direction = 9
        # update movement speed of the ball
        update_ball_speed()
        return
      end
      # If the ball touches the ai
      if (collision_ball_ai?)
        # Change the ball direction to left/down
        @pong_ball_direction = 1
        return
      end
      
    # When the ball is moving right/up
    when 9
      # If the ball touches the ceiling
      if (@pong_ball.y <= 0)
        # Change the ball direction to right/down
        @pong_ball_direction = 3
        # update movement speed of the ball
        update_ball_speed()
        return
      end
      # If the ball touches the ai
      if (collision_ball_ai?)
        # Change the ball direction to left/up
        @pong_ball_direction = 7
        # update movement speed of the ball
        update_ball_speed()
        return
      end
    end
  end
  
  #--------------------------------------------------------------------------
  # * Collision Check Player
  # Returns true or false wether the ball touched the player's paddle.
  #--------------------------------------------------------------------------
  def collision_ball_player?()
    # If the ball y-axis is between the Player's paddle height
    if (@pong_ball.y.between?(@paddle_player.y, 
        @paddle_player.y + @paddle_player.bitmap.height) && 
        # and the ball x-axis matches the paddle's x-axis
        @pong_ball.x.between?(@paddle_player.x, @paddle_player.x + @paddle_player.bitmap.width))
      # update movement speed of the ball
      update_ball_speed()
      # It's a hit!
      return true
    end
    # The ball hasn't hit the Player's paddle
    return false
  end
  
  #--------------------------------------------------------------------------
  # * Collision Check AI
  # Returns true or false wether the ball touched the player's paddle.
  #--------------------------------------------------------------------------
  def collision_ball_ai?()
    # If the ball y-axis is between the AI's paddle height
    if (@pong_ball.y.between?(@paddle_ai.y, 
        @paddle_ai.y + @paddle_ai.bitmap.height) && 
        # and the ball x-axis matches the AI's paddle x-axis
        @pong_ball.x.between?(@paddle_ai.x - @pong_ball.bitmap.width, @paddle_ai.x))
      # update movement speed of the ball
      update_ball_speed()
      # It's a hit!
      return true
    end
    # The ball hasn't hit the AI's paddle
    return false
  end
  
  #--------------------------------------------------------------------------
  # * Update Ball Movement
  # This part handles which direction the ball moves.
  #--------------------------------------------------------------------------
  def update_ball_movement()
    case @pong_ball_direction
    when 1 # Left / Down
      @pong_ball.x -= @pong_ball_speed
      @pong_ball.y += @pong_ball_speed
    when 3 # Right / Down
      @pong_ball.x += @pong_ball_speed
      @pong_ball.y += @pong_ball_speed
    when 7 # Left / Up
      @pong_ball.x -= @pong_ball_speed
      @pong_ball.y -= @pong_ball_speed
    when 9 # Right / Up
      @pong_ball.x += @pong_ball_speed
      @pong_ball.y -= @pong_ball_speed
    end
  end
  
  #--------------------------------------------------------------------------
  # * Update AI Movement
  # simple method of moving the AI
  #--------------------------------------------------------------------------
  def update_ai_movement()
    dist = (@pong_ball.y - @paddle_ai.y - (@paddle_ai.bitmap.height/2)).to_i.abs
    
    if (@pong_ball.y > @paddle_ai.y && dist > PADDLE_SPEED)
      @paddle_ai.y += PADDLE_SPEED
      @paddle_ai.y = [[@paddle_ai.y, 0].max, 480-@paddle_ai.bitmap.height].min
      
    end
    if (@pong_ball.y < @paddle_ai.y && dist > PADDLE_SPEED) 
      @paddle_ai.y -= PADDLE_SPEED
      @paddle_ai.y = [[@paddle_ai.y, 0].max, 480-@paddle_ai.bitmap.height].min
    end
  end
end

#==============================================================================
# ** Window_Pong
#------------------------------------------------------------------------------
#  This window displays the score of the Player and AI
#==============================================================================

class Window_Pong < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 640, 128)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.font.size = 24
    self.opacity = 0
    self.back_opacity = 0
    @score1 = 0
    @score2 = 0
    @start = false
    
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    # Draw the score of the player and ai
    text = "Player #{@score1} - Computer #{@score2}"
    self.contents.draw_text(0, 0, 640, 32, text.to_s, 1)
    # Draw 'press space to continue' when needed
    if (not @start)
      text = "Press space to continue"
      self.contents.draw_text(0, 32, 640, 32, text.to_s, 1)
    end
  end
  #--------------------------------------------------------------------------
  # * Update Score
  #--------------------------------------------------------------------------
  def update_score(s1, s2)
    @score1 = s1
    @score2 = s2
    refresh
  end
  #--------------------------------------------------------------------------
  # * Update Start Game
  #--------------------------------------------------------------------------
  def update_start_game(start)
    @start = start
    refresh
  end
end

Instructions
1) You need to put this script above main.
2) Download the rar file with the images used here: http://www.mediafire.com/?tdxty3yamsc
3) Unrar the images and put them in the /pictures map.
4) Call $scene = Scene_Pong.new to use this script.
5) Enjoy playing the game.

Compatibility
This script will only work with RPG Maker XP. I do not own RPG Maker VX so I cannot promise it will work with VX

Terms and Conditions
You can use and edit this script freely, as long as you give credit!
 
jjangjae":1mng095y said:
Seems like a pretty cool script, but how do you call it via an event?  The script doesn't say how.
Oops, sorry... Totally forgot about that.
Code:
$scene = Scene_Pong.new

I've added it in the script as well
 
One last thing (pardon me if I accidently skipped it when skimming the script), how will players be able to exit from the game?
 
jjangjae":1yh5869w said:
One last thing (pardon me if I accidently skipped it when skimming the script), how will players be able to exit from the game?

I haven't included that feature, yet. Will add it now.

-edit- added, press the x or esc button to exit to the map
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top