Here
#==============================================================================
# ** RMW 2016 Christmas Gift - Blackjack
# Code written by Kazama
# Bundled Graphics by http://kenney.nl/
#------------------------------------------------------------------------------
# * Updates
# - 1.0.0 Base Script
#------------------------------------------------------------------------------
# * Description
# Blackjack is a very simple card game and is very fun to play. Although,
# I've never actually seen one written and released for any of the makers.
# So, I'd like to present this script as a gift from me to you.
#------------------------------------------------------------------------------
# * Features
# - Basic Blackjack
# - Bundled Graphical set for quick Installation
# - Basic Score keeping using Variables
# - Adjustable Audio Settings for various moments
# - Adjustable Messages for various moments
# - YOU MAY REQUEST ADDITIONAL FEATURES IF YOU LIKE
#------------------------------------------------------------------------------
# * License
# - Both Non-Commercial and Commercial usage is allowed.
# - Credit Kazama for the code.
# - Credit Kenney for the wonderful graphical assets.
# - Do not distribute or repost on other websites.
# - Free to edit for personal usage but do not post your edits.
# - Feel free to Donate if you'd like too. (not required at all)
#------------------------------------------------------------------------------
# * Engine
# - This script only works in the RPG Maker VX Ace engine.
#------------------------------------------------------------------------------
# * How to Use
# If you're using everything included in the sample project, then all you
# will have to do is use a script call: SceneManager.call(Scene_KCG_Blackjack)
# The script will handle the rest.
#------------------------------------------------------------------------------
# * Extra Notes
# Being a member for a full year on RMW has been pretty great. You all have
# been so kind and supportive of all the work I've done. So this me hoping
# I'll be able to give back some more! Happy Holidays and game making!
#==============================================================================
module KCG_Blackjack_Settings
# Base Settings #
# Determine the player's name.
PLAYER_NAME = "Player"
# Determine the Dealer's name.
DEALER_NAME = "Dealer"
# Determine what value the Dealer will stand at.
STAND_POINT = 17
# Determine what Variable the victories will be stored in.
VICTORY_VARIABLE = 98
# Determine what Variable the defeats will be stored in.
DEFEAT_VARIABLE = 99
# Message Settings #
BLACKJACK = "#{PLAYER_NAME} has won by Blackjack!"
PLAYER_BEAT_DEALER = "#{DEALER_NAME} has been defeated by #{PLAYER_NAME}!"
DEALER_BEAT_PLAYER = "#{PLAYER_NAME} has been defeated by #{DEALER_NAME}!"
DEALER_BUSTED = "#{DEALER_NAME} has busted. #{PLAYER_NAME} wins!"
PLAYER_BUSTED = "#{PLAYER_NAME} has busted. #{DEALER_NAME} wins!"
PARTICIPANTS_TIED = "#{PLAYER_NAME} and #{DEALER_NAME} are tied. No winner!"
# Audio Settings #
# Determine the BGM track played. (filename, volume, pitch)
BGM_TRACK = ["Town3", 80, 100]
# Determine the Victory ME played. (filename, volume, pitch)
ME_VICTORY = ["Victory2", 80, 100]
# Determine the Tied ME played. (filename, volume, pitch)
ME_TIED = ["Gag", 80, 100]
# Determine the Defeat ME played. (filename, volume, pitch)
ME_DEFEAT = ["Mystery", 80, 100]
# Update 1.?.? Settings #
# None yet. But if one of us think of something, I'll add it here.
end
# Do not modify these values out of curiosity. You will break the script.
SUIT = ["Clubs", "Hearts", "Spades", "Diamonds"]
FACE = [2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A"]
#==============================================================================
# ** Cache
#------------------------------------------------------------------------------
# This module loads graphics, creates bitmap objects, and retains them.
# To speed up load times and conserve memory, this module holds the
# created bitmap object in the internal hash, allowing the program to
# return preexisting objects when the same bitmap is requested again.
#==============================================================================
module Cache
#--------------------------------------------------------------------------
# * Get Blackjack Graphic
#--------------------------------------------------------------------------
def self.kcg_blackjack(filename)
load_bitmap("Graphics/KCG_Blackjack/", filename)
end
end
#==============================================================================
# ** KCG_Card
#------------------------------------------------------------------------------
# This class handles a card object. Each card is handled as a separate object
# instance and is then passed on to the 'Deck of Cards'.
#==============================================================================
class KCG_Card
#--------------------------------------------------------------------------
# * Public Instance Variables
#--------------------------------------------------------------------------
attr_reader :suit, :face, :value
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize(suit, face)
# Get Suit and Face for a Card
@suit, @face = suit, face
end
#--------------------------------------------------------------------------
# * Get Value for each Card
#--------------------------------------------------------------------------
def value
# Go through each face and assign a value to it.
case @face
when 2..10 then @value = @face
when "J", "Q", "K" then @value = 10
when "A" then @value = 11
end
# Return Value of Card
return @value
end
#--------------------------------------------------------------------------
# * Convert Card to a Readable String
#--------------------------------------------------------------------------
def to_s
@face.to_s + "_of_" + @suit
end
end
#==============================================================================
# ** KCG_Deck
#------------------------------------------------------------------------------
# This class handles a deck of cards.
#==============================================================================
class KCG_Deck
#--------------------------------------------------------------------------
# * Public Instance Variables
#--------------------------------------------------------------------------
attr_accessor :cards
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize
# Create Card Array
@cards = Array.new
# Map Cards to Array
map_cards_to_array
end
#--------------------------------------------------------------------------
# * Map Cards to the Array
#--------------------------------------------------------------------------
def map_cards_to_array
# Go through each Suit
SUIT.each do |suit|
# Go through each Face
FACE.each do |face|
# Push a Card to the Deck Array
@cards.push(KCG_Card.new(suit, face))
end
end
end
#--------------------------------------------------------------------------
# * Shuffle the Deck
#--------------------------------------------------------------------------
def shuffle!
@cards.shuffle!
end
#--------------------------------------------------------------------------
# * Draw Card from Deck
#--------------------------------------------------------------------------
def draw_card
@cards.pop
end
#--------------------------------------------------------------------------
# * Get Remaining Cards in the Deck
#--------------------------------------------------------------------------
def remaining
@cards.length
end
end
#==============================================================================
# ** KCG_Participant
#------------------------------------------------------------------------------
# This class handles participants in the game. It's used for both players
# and dealers.
#==============================================================================
class KCG_Participant
#--------------------------------------------------------------------------
# * Included Modules
#--------------------------------------------------------------------------
include KCG_Blackjack_Settings
#--------------------------------------------------------------------------
# * Public Instance Variables
#--------------------------------------------------------------------------
attr_reader :hand_sprites
attr_accessor :hand, :hand_value, :ace_count, :index
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize(index)
# Acquire participant index
@index = index
# Reset all Settings
reset_participant_data
end
#--------------------------------------------------------------------------
# * Reset Participant Data
#--------------------------------------------------------------------------
def reset_participant_data
# Create a new Hand Array
@hand = Array.new
# Create a new Hand Sprite Array
@hand_sprites = Array.new
# Set Hand Value
@hand_value = 0
# Set Ace Count
@ace_count = 0
end
#--------------------------------------------------------------------------
# * Determine an Ace Card's Value
#--------------------------------------------------------------------------
def determine_ace_value
# Adjust hand value while total is over 21 and ace cards are present
while @hand_value > 21 and @ace_count > 0
# Remove the Ace card and subtract the points
@ace_count -= 1 ; @hand_value -= 10
end
end
#--------------------------------------------------------------------------
# * Deal Card to Participant
#--------------------------------------------------------------------------
def deal_card
# Grab card object from the Deck
card = SceneManager.scene.deck.draw_card
# Add the value of the card to the Hand
@hand_value += card.value
# Add to the Ace Pile if we Acquire an Ace
@ace_count += 1 if card.value == 11
# Push Card to the Hand
@hand.push(card)
# Double check the value of aces and apply any changes
determine_ace_value
# Get the Sprite X-Position
sx_position = @hand.length * 72
# Get the Sprite Y-Position
sy_position = @index > 0 ? 100 : Graphics.height - 100
# Get the Sprite Filename
s_filename = card.to_s
# Push Sprite to the Hand Sprite Array
@hand_sprites.push(Sprite_Card.new(s_filename, sx_position, sy_position))
# Hide the Original Card if the Participant is the dealer
@hand_sprites[0].hide_card if @index > 0
end
#--------------------------------------------------------------------------
# * Dispose Cards
#--------------------------------------------------------------------------
def dispose_cards
# Go through all sprites and dispose them
@hand_sprites.each { |card| card.dispose }
# Reset Participant Data
reset_participant_data
end
#--------------------------------------------------------------------------
# * Get Participant Name
#--------------------------------------------------------------------------
def name
@index > 0 ? DEALER_NAME : PLAYER_NAME
end
end
#==============================================================================
# ** Sprite_Felt
#------------------------------------------------------------------------------
# This sprite displays a felt background on screen.
#==============================================================================
class Sprite_Felt < Sprite
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize
super(nil)
# Create Bitmap Image
self.bitmap = Cache.kcg_blackjack("Felt_Background")
end
#--------------------------------------------------------------------------
# * Free
#--------------------------------------------------------------------------
def dispose
# Dispose Bitmap unless it's nil or already disposed
bitmap.dispose unless (bitmap.nil? or bitmap.disposed?)
super
end
end
#==============================================================================
# ** Sprite_Card
#------------------------------------------------------------------------------
# This sprite displays a card on the screen based on card data found in
# a participant's hand.
#==============================================================================
class Sprite_Card < Sprite
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize(filename, x, y)
super(nil)
# Create Bitmap Image
self.bitmap = Cache.kcg_blackjack(filename)
# Set the Zoom Factor of the sprite (nearest neighbor)
self.zoom_x = self.zoom_y = 0.5
# Set Origin X and Y of the sprite
self.ox, self.oy = bitmap.width / 2, bitmap.height / 2
# Set X and Y positions
self.x, self.y = x, y
# Get Filename Tag
@filename = filename
end
#--------------------------------------------------------------------------
# * Hide Card Sprite
#--------------------------------------------------------------------------
def hide_card
# Clone the Filename
@last_card = @filename.dup
# Dispose Bitmap unless it's nil or already disposed
bitmap.dispose unless (bitmap.nil? or bitmap.disposed?)
# Create Bitmap Image
self.bitmap = Cache.kcg_blackjack("back_of_deck")
end
#--------------------------------------------------------------------------
# * Show Card Sprite
#--------------------------------------------------------------------------
def show_card
# Dispose Bitmap unless it's nil or already disposed
bitmap.dispose unless (bitmap.nil? or bitmap.disposed?)
# Create Bitmap Image
self.bitmap = Cache.kcg_blackjack(@last_card)
end
#--------------------------------------------------------------------------
# * Free
#--------------------------------------------------------------------------
def dispose
# Dispose Bitmap unless it's nil or already disposed
bitmap.dispose unless (bitmap.nil? or bitmap.disposed?)
super
end
end
#==============================================================================
# ** Window_KCG_Status
#------------------------------------------------------------------------------
# This window displays the name of participant and their hand value.
#==============================================================================
class Window_KCG_Status < Window_Base
#--------------------------------------------------------------------------
# * Public Instance Variables
#--------------------------------------------------------------------------
attr_reader :participant, :show_value
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize(participant)
super(0, 0, Graphics.width / 2, fitting_height(1))
# Get Participant
@participant = participant
# Set Show Flag to False
@show_value = false
# Refresh Name
refresh_name
end
#--------------------------------------------------------------------------
# * Set Value Boolean
#--------------------------------------------------------------------------
def show_value=(boolean)
# Set Boolean to Show Flag
@show_value = boolean
# Refresh Value
refresh_value
end
#--------------------------------------------------------------------------
# * Refresh
#--------------------------------------------------------------------------
def refresh_name
# Clear Contents Rect
contents.clear_rect(0, 0, contents.width / 2, 24)
# Draw Participant Name
draw_text(contents.rect, @participant.name)
end
#--------------------------------------------------------------------------
# * Refresh Value
#--------------------------------------------------------------------------
def refresh_value
# Clear Contents Rect
contents.clear_rect(contents.width / 2 - 24, 0, contents.width / 2 + 24, 24)
# Get Value
value = @show_value ? @participant.hand_value : "??"
# Draw Participant Hand Value
draw_text(contents.rect, "Hand Total: #{value.to_s}", 2)
end
end
#==============================================================================
# ** Window_KCG_Actions
#------------------------------------------------------------------------------
# This window displays variable actions that the player can perform.
#==============================================================================
class Window_KCG_Actions < Window_HorzCommand
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize
super(Graphics.width / 2, Graphics.height - window_height)
end
#--------------------------------------------------------------------------
# * Get Window Width
#--------------------------------------------------------------------------
def window_width
return Graphics.width / 2
end
#--------------------------------------------------------------------------
# * Get Digit Count
#--------------------------------------------------------------------------
def col_max
return item_max
end
#--------------------------------------------------------------------------
# * Create Command List
#--------------------------------------------------------------------------
def make_command_list
add_command("Hit", :hit)
add_command("Stay", :stay)
add_command("Retire", :retire)
end
end
#==============================================================================
# ** Window_KCG_Result
#------------------------------------------------------------------------------
# This window displays the final post-round results.
#==============================================================================
class Window_KCG_Result < Window_Command
#--------------------------------------------------------------------------
# * Included Modules
#--------------------------------------------------------------------------
include KCG_Blackjack_Settings
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize
super(0, 0)
# Set X-Position
self.x = (Graphics.width - window_width) / 2
# Set Y-Position
self.y = (Graphics.height - window_height) / 2
# Set Openness
self.openness = 0
# Set Default Message
@message = ""
end
#--------------------------------------------------------------------------
# * Get Window Width
#--------------------------------------------------------------------------
def window_width
return Graphics.width / 1.2
end
#--------------------------------------------------------------------------
# * Get Window Height
#--------------------------------------------------------------------------
def window_height
return fitting_height(4)
end
#--------------------------------------------------------------------------
# * Get Digit Count
#--------------------------------------------------------------------------
def col_max
return item_max
end
#--------------------------------------------------------------------------
# * Get Alignment
#--------------------------------------------------------------------------
def alignment
return 1
end
#--------------------------------------------------------------------------
# * Create Command List
#--------------------------------------------------------------------------
def make_command_list
add_command("Next Round", :next_round)
add_command("Retire", :retire)
end
#--------------------------------------------------------------------------
# * Get Rectangle for Drawing Items
#--------------------------------------------------------------------------
def item_rect(index)
# Get original Rectangle
rect = super(index)
# Modify Y-Position
rect.y += line_height * 3
# Return new Rectangle
rect
end
#--------------------------------------------------------------------------
# * Set Message
#--------------------------------------------------------------------------
def set_message=(message)
# Set Message Text
@message = message
# Refresh Contents
refresh
end
#--------------------------------------------------------------------------
# * Refresh
#--------------------------------------------------------------------------
def refresh
super
# Change Color to Crisis Color
change_color(crisis_color)
# Draw Message
draw_text(0, 0, contents.width, line_height, @message, 1)
# Change Color to System Color
change_color(system_color)
# Draw Victory Header
draw_text(0, line_height, contents.width, line_height, "Victories:")
# Draw Defeat Header
draw_text(0, line_height * 2, contents.width, line_height, "Defeats:")
# Change Color to Normal Color
change_color(normal_color)
# Get Victory Variable
victory_variable = $game_variables[VICTORY_VARIABLE]
# Get Defeat Variable
loss_variable = $game_variables[DEFEAT_VARIABLE]
# Draw Victory Variable
draw_text(0, line_height, contents.width, line_height, victory_variable, 2)
# Draw Defeat Variable
draw_text(0, line_height * 2, contents.width, line_height, loss_variable, 2)
end
end
#==============================================================================
# ** Scene_KCG_Blackjack
#------------------------------------------------------------------------------
# This class performs blackjack processing.
#==============================================================================
class Scene_KCG_Blackjack < Scene_Base
#--------------------------------------------------------------------------
# * Included Modules
#--------------------------------------------------------------------------
include KCG_Blackjack_Settings
#--------------------------------------------------------------------------
# * Public Instance Variables
#--------------------------------------------------------------------------
attr_reader :deck
#--------------------------------------------------------------------------
# * Start Processing
#--------------------------------------------------------------------------
def start
super
# Save Game BGM
$game_system.save_bgm
# Create Background Felt
@background = Sprite_Felt.new
# Create Player Object
@player = KCG_Participant.new(0)
# Create Dealer Object
@dealer = KCG_Participant.new(1)
# Create Player Status Window
@player_status_window = Window_KCG_Status.new(@player)
# Set Player Status Window Y-Position
@player_status_window.y = Graphics.height - 48
# Show Player Value
@player_status_window.show_value = true
# Create Dealer Status Window
@dealer_status_window = Window_KCG_Status.new(@dealer)
# Create Action Window
@actions_window = Window_KCG_Actions.new
# Set Action Window Handlers
@actions_window.set_handler(:hit, method(:on_action_hit))
@actions_window.set_handler(:stay, method(:on_action_stay))
@actions_window.set_handler(:retire, method(:return_scene))
# Create Result Window
@result_window = Window_KCG_Result.new
# Set Result Window Handlers
@result_window.set_handler(:next_round, method(:reset_game_data))
@result_window.set_handler(:retire, method(:return_scene))
# Reset Game Data
reset_game_data
# Judge Win/Loss Initially
judge_win_loss(true)
end
#--------------------------------------------------------------------------
# * Reset Game Data
#--------------------------------------------------------------------------
def reset_game_data
# Close Result Window unless it's nil
@result_window.close unless @result_window.nil?
# Stop All Audio
RPG::BGM.stop ; RPG::ME.stop
# Play Theme BGM
RPG::BGM.new(*BGM_TRACK).play
# Create a new Deck and Shuffle it
@deck = KCG_Deck.new ; @deck.shuffle!
# Dispose Player and Dealer Cards
@player.dispose_cards ; @dealer.dispose_cards
# Deal 2 Cards to the Player and Dealer
2.times { @player.deal_card ; @dealer.deal_card }
# Force Dealer Value to Hide
@dealer_status_window.show_value = false
# Refresh Player and Dealer Status Window
@player_status_window.refresh_value ; @dealer_status_window.refresh_value
# Activate Action Window and Select the First Command
@actions_window.activate.select(0)
end
#--------------------------------------------------------------------------
# * On Action Hit
#--------------------------------------------------------------------------
def on_action_hit
# Deal Card to Player
@player.deal_card
# Refresh Player Status Window
@player_status_window.refresh_value
# Activate Action Window
@actions_window.activate
# Judge Win/Loss unless Player's hand value is under 21
judge_win_loss unless @player.hand_value < 21
end
#--------------------------------------------------------------------------
# * On Action Stay
#--------------------------------------------------------------------------
def on_action_stay
# Deal Cards to the Dealer until conditions are met
until @dealer.hand_value >= STAND_POINT
# Deal Card to Dealer
@dealer.deal_card
# Refresh Dealer Status Window
@dealer_status_window.refresh_value
end
# Judge Win/Loss
judge_win_loss
end
#--------------------------------------------------------------------------
# * Wait for Result Window
#--------------------------------------------------------------------------
def wait_for_result_window
# Open Result Window
@result_window.open.activate
# Deactivate Action Window
@actions_window.deactivate
end
#--------------------------------------------------------------------------
# * Determine Win/Loss Results
#--------------------------------------------------------------------------
def judge_win_loss(inital_check = false)
# Check for Blackjack
if @player.hand_value == 21
# Add 1 to Victory Variable
$game_variables[VICTORY_VARIABLE] += 1
# Play Victory ME
play_result_track(:victory)
# Show Dealer's Card
@dealer.hand_sprites[0].show_card
# Show Dealer's Value
@dealer_status_window.show_value = true
# Set Result Message
@result_window.set_message = BLACKJACK
# Wait for Result Window to show
wait_for_result_window
# Replay Main Game BGM
$game_system.replay_bgm
# Break any additional Checks
return
end
# Return if we just want the initial check
return unless !inital_check
# Check if Dealer has Busted
if @dealer.hand_value > 21
# Add 1 to Victory Variable
$game_variables[VICTORY_VARIABLE] += 1
# Play Victory ME
play_result_track(:victory)
# Show Dealer's Card
@dealer.hand_sprites[0].show_card
# Show Dealer's Value
@dealer_status_window.show_value = true
# Set Result Message
@result_window.set_message = DEALER_BUSTED
# Wait for Result Window to show
wait_for_result_window
# Replay Main Game BGM
$game_system.replay_bgm
# Break any additional Checks
return
end
# Check Results Based if Player has stayed
if @player.hand_value <= 21
# If Player's hand is greater then the Dealer's hand
if @player.hand_value > @dealer.hand_value
# Add 1 to Victory Variable
$game_variables[VICTORY_VARIABLE] += 1
# Play Victory ME
play_result_track(:victory)
# Show Dealer's Card
@dealer.hand_sprites[0].show_card
# Show Dealer's Value
@dealer_status_window.show_value = true
# Set Result Message
@result_window.set_message = PLAYER_BEAT_DEALER
# Wait for Result Window to show
wait_for_result_window
# Replay Main Game BGM
$game_system.replay_bgm
# Break any additional Checks
return
# If Player and Dealer are Tied
elsif @player.hand_value == @dealer.hand_value
# Play Tied ME
play_result_track(:tied)
# Show Dealer's Card
@dealer.hand_sprites[0].show_card
# Show Dealer's Value
@dealer_status_window.show_value = true
# Set Result Message
@result_window.set_message = PARTICIPANTS_TIED
# Wait for Result Window to show
wait_for_result_window
# Replay Main Game BGM
$game_system.replay_bgm
# Break any additional Checks
return
# If Dealer has beaten the Player
else
# Add 1 to Defeat Variable
$game_variables[DEFEAT_VARIABLE] += 1
# Play Victory ME
play_result_track(:defeat)
# Show Dealer's Card
@dealer.hand_sprites[0].show_card
# Show Dealer's Value
@dealer_status_window.show_value = true
# Set Result Message
@result_window.set_message = DEALER_BEAT_PLAYER
# Wait for Result Window to show
wait_for_result_window
# Replay Main Game BGM
$game_system.replay_bgm
# Break any additional Checks
return
# Check if Player has Busted
end
else
# Add 1 to Defeat Variable
$game_variables[DEFEAT_VARIABLE] += 1
# Play Victory ME
play_result_track(:defeat)
# Show Dealer's Card
@dealer.hand_sprites[0].show_card
# Show Dealer's Value
@dealer_status_window.show_value = true
# Set Result Message
@result_window.set_message = PLAYER_BUSTED
# Wait for Result Window to show
wait_for_result_window
# Replay Main Game BGM
$game_system.replay_bgm
# Break any additional Checks
return
end
end
#--------------------------------------------------------------------------
# * Play Result ME
#--------------------------------------------------------------------------
def play_result_track(type)
# Get Meta Type
case type
when :victory then meta = ME_VICTORY
when :tied then meta = ME_TIED
when :defeat then meta = ME_DEFEAT
end
# Stop Blackjack BGM
RPG::BGM.stop
# Play Result ME
RPG::ME.new(*meta).play
end
end
Comments
Post a Comment