Pong Mini Game

PONG
Created by: Opt7ons


Introduction
This is just my little version of pong. This is also my first script (made it really just get used to RGSS), so yea, any pointers to make my script any better would be helpful.

Features
  • 2 Player
  • CPU Control
  • Adjustable Speeds

Directions
  1. In your project script window insert a new blank page above 'Main'
  2. Paste my 'Pong' script into into the blank page
  3. Copy the pictures I have provided into your pictures folder
  4. Adjust the variables @paddlespeed, @ballspeed, @cpu and @cpurand to your liking or leave them alone
  5. To call the script, create a new event and make a new script event with "$scene = Scene_Pong.new" inside, without the quotes
  6. Open your game up and talk to your event

Controls
  • SHIFT = Left Paddle Up
  • CTRL = Left Paddle Down
  • UP = Right Paddle Up
  • DOWN = Right Paddle Down
  • ALT = Launch Ball

Pictures Required
Are attached to this thread somewhere

Variables
  • @paddlespeed ---- Sets the paddle speed movement
  • @ballspeed ---- Sets the ball speed
  • @cpu ---- 0 = Disabled, 1 = Enabled
  • @cpurand ---- Sets the cpu speed, it's a little stuffed up but its a shortcut i took to make it fair


Script
Script Spoiler
#==============================================================================
# ** Scene_Pong
#------------------------------------------------------------------------------
#  This class performs the pong mini game processing.
#==============================================================================

class Scene_Pong
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    # Assigns the paddle speed to a variable
    @paddlespeed = 6
    # Assigns the ball speed to a variable
    @ballspeed = 8
    # Assigns the ball direction to a variable
    @balldirection = rand(4)
    if @balldirection == 4
      @balldirection = 3
    end
    # Assigns the score to an array
    @score = [0, 0]
    # CPU play 0 = Disabled, 1 = Enabled #NB Disabled by default
    @cpu = 0
    @cpurand = 14
  end
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # Make ball graphic
    @sprite_ball = Sprite.new
    @sprite_ball.bitmap = RPG::Cache.picture("ball.png")
    @sprite_ball.x = 304
    @sprite_ball.y = 224
    # Make left paddle graphic
    @sprite_l_paddle = Sprite.new
    @sprite_l_paddle.bitmap = RPG::Cache.picture("paddle_left.png")
    # Make right paddle graphic
    @sprite_r_paddle = Sprite.new
    @sprite_r_paddle.bitmap = RPG::Cache.picture("paddle_right.png")
    @sprite_r_paddle.x = 608
    # Stop playing ME and BGS
    Audio.me_stop
    Audio.bgs_stop
    # Execute transition
    Graphics.transition
    # Waits for key trigger
    start
    # Main loop
    loop do
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Update paddles
      left_paddle_move
      if @cpu == 1
        cpu
      else
        right_paddle_move
      end
      # Update ball
      ball
      ball_score
      # Frame update
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    @sprite_ball.update
    @sprite_l_paddle.update
    @sprite_r_paddle.update
  end
  #--------------------------------------------------------------------------
  # * Ball Position Update
  #--------------------------------------------------------------------------
  def ball
    if @balldirection == 0
      if @sprite_ball.x >= 608
        @balldirection = 2
      elsif @sprite_ball.y >= 448
        @balldirection = 1
      else
        if @sprite_ball.x == 576
          for i in -20..52
            if @sprite_ball.y == @sprite_r_paddle.y + i
             @balldirection = 2
            end
          end
        end
        @sprite_ball.x += @ballspeed
        @sprite_ball.y += @ballspeed
      end
    elsif @balldirection == 1
      if @sprite_ball.x >= 608
        @balldirection = 3
      elsif @sprite_ball.y <= 0
        @balldirection = 0
      else
        if @sprite_ball.x == 576
          for i in -20..52
            if @sprite_ball.y == @sprite_r_paddle.y + i
             @balldirection = 3
            end
          end
        end
        @sprite_ball.x += @ballspeed
        @sprite_ball.y -= @ballspeed
      end
    elsif @balldirection == 2
      if @sprite_ball.x <= 0
        @balldirection = 2
      elsif @sprite_ball.y >= 448
        @balldirection = 3
      else
        if @sprite_ball.x == 32
          for i in -20..52
            if @sprite_ball.y == @sprite_l_paddle.y + i
             @balldirection = 0
            end
          end
        end
        @sprite_ball.x -= @ballspeed
        @sprite_ball.y += @ballspeed
      end
    elsif @balldirection == 3
      if @sprite_ball.x <= 0
        @balldirection = 1
      elsif @sprite_ball.y <= 0
        @balldirection = 2
      else
        if @sprite_ball.x == 32
          for i in -20..52
            if @sprite_ball.y == @sprite_l_paddle.y + i
             @balldirection = 1
            end
          end
        end
        @sprite_ball.x -= @ballspeed
        @sprite_ball.y -= @ballspeed
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Ball Scored Reset Update
  #--------------------------------------------------------------------------
  def ball_score
    if @sprite_ball.x >= 608
      # Resets the balls position
      @sprite_ball.x = 304
      @sprite_ball.y = 224
      # Increases left player score
      @score[0] += 1
      # Assigns the ball direction to a variable
      @balldirection = rand(2)+ 2
      if @balldirection == 4
      @balldirection = 3
      end
      # Begins waiting for next round
      start
    elsif @sprite_ball.x <=0
      # Resets the balls position
      @sprite_ball.x = 304
      @sprite_ball.y = 224
      # Increases right player score
      @score[1] += 1
      # Assigns the ball direction to a variable
      @balldirection = rand(2)
      if @balldirection == 2
        @balldirection = 1
      end
      # Begins waiting for next round
      start
    end
  end
  def start
    loop do
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Update paddles
      left_paddle_move
      right_paddle_move
      # Frame update
      update
      # Break from loop when condition is meet
      if Input.trigger?(Input::ALT)
        break
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Paddle Position Update
  #--------------------------------------------------------------------------
  # Update left paddle movement
  def left_paddle_move
    if @sprite_l_paddle.y <= 416
      # Check for input
      if Input.press?(Input::CTRL)
        # Move paddle down by @paddlespeed pixels
        @sprite_l_paddle.y += @paddlespeed
      end
    end
    if @sprite_l_paddle.y >= 0\
      # Check for input
      if Input.press?(Input::SHIFT)
        # Move paddle up by @paddlespeed pixels
        @sprite_l_paddle.y -= @paddlespeed
      end
    end
  end
  # Update right paddle movement
  def right_paddle_move
    if @sprite_r_paddle.y <= 416
      # Check for input
      if Input.press?(Input::DOWN)
        # Move paddle down by @paddlespeed pixels
        @sprite_r_paddle.y += @paddlespeed
      end
    end
    if @sprite_r_paddle.y >= 0
      # Check for input
      if Input.press?(Input::UP)
        # Move paddle up by @paddlespeed pixels
        @sprite_r_paddle.y -= @paddlespeed
      end
    end
  end
  #--------------------------------------------------------------------------
  # * CPU Update
  #--------------------------------------------------------------------------
  def cpu
    if @sprite_ball.y <= @sprite_r_paddle.y
      @sprite_r_paddle.y -= rand(@cpurand)
    elsif @sprite_ball.y >= @sprite_r_paddle.y
      @sprite_r_paddle.y += rand(@cpurand)
    end
  end
end
Expand to see the code.


Notes
I might add some other stuff in later like sound. Enjoy :thumb:.
Um... Dude why does your signature say that you stole Rpg Maker Xp, not very smart... You might get into trouble...
The link will make you log out. Btw, your script have too much magic numbers in it ;)
:) Oh I didn't try the link I just don't like people who do stupid things ><
It's just like other members do with linking to fake sites for the ones who want the cracked one.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=32323