Im creating a version of a pokemon battle using ruby tk. Im a beginner
programmer, and this is what ive come up with. Im trying to let the
buttons change text and commands but only the first button is changing
to "hyper fang. Which it should not do. Im confused at how to solve this
and the normal helpfull inet is not producing answers. Could any of you
help, and also, general other tips to improve this code are always
welcome,
Thanks!
require ‘tk’
require ‘tkextlib/tile’
root = TkRoot.new {title “Pokemon Battle”}
content = Tk::Tile::Frame.new(root).grid :column => 0, :row => 0
TkGrid.columnconfigure root, 0, :weight => 1; TkGrid.rowconfigure root,
0, :weight => 1
#inhoudgrid
$enemyname = TkVariable.new; name = $ownname = TkVariable.new
enemynamelabel = Tk::Tile::Label.new(content) {textvariable
$enemyname}.grid :column => 0, :row => 0, :columnspan => 5, :sticky =>
‘w’
ownnamelabel = Tk::Tile::Label.new(content) {textvariable $ownname}.grid
:column => 5, :row => 5, :columnspan => 5, :sticky => ‘w’
enemyprogressbar = Tk::Tile::Progressbar.new(content) {orient
‘horizontal’; length 100; mode ‘determinate’}.grid :column => 0, :row
=> 1, :sticky => ‘n’, :columnspan => 5
$enemyhp = TkVariable.new
enemyprogressbar.variable = $enemyhp
ownprogressbar = Tk::Tile::Progressbar.new(content) {orient
‘horizontal’; length 100; mode ‘determinate’}.grid :column => 5, :row
=> 6, :sticky => ‘n’, :columnspan => 5
$ownhp = TkVariable.new
ownprogressbar.variable = $ownhp
ownimage = TkPhotoImage.new
ownimage.file = “rattata.gif”
ownimagelabel = TkLabel.new(content).grid :column => 0, :row => 5,
:columnspan => 5, :rowspan => 2
ownimagelabel.image = ownimage
enemyimage = TkPhotoImage.new
enemyimage.file = “eevee.gif”
enemyimagelabel = TkLabel.new(content).grid :column => 5, :row => 0,
:columnspan => 5, :rowspan => 2
enemyimagelabel.image = enemyimage
streep1 = Tk::Tile::Separator.new(content) {orient ‘horizontal’}.grid
:column => 0, :row => 10, :ipady => 5, :ipadx => 100, :columnspan => 10
@btn1 = Tk::Tile::Button.new(content) {text ‘Fight’; command
{comfight}}.grid :column => 0, :row => 11, :columnspan => 5
@btn2 = Tk::Tile::Button.new(content) {text ‘Run’; command
{comrun}}.grid :column => 5, :row => 11, :columnspan => 5
@btn3 = Tk::Tile::Button.new(content) {text ‘Bag’; command {combag};
state “disabled”}.grid :column => 0, :row => 12, :columnspan => 5
@btn4 = Tk::Tile::Button.new(content) {text ‘Pokemon’; command
{compokemon}; state “disabled”}.grid :column => 5, :row => 12,
:columnspan => 5
$enemyhp.value = 100
$ownhp.value = 100
$enemyname.value = ‘Eevee’
$ownname.value = ‘Rattata’
TkWinfo.children(content).each {|w| TkGrid.configure w, :padx => 2,
:pady => 2}
#define methods for buttons
def comfight
if $ownname == “Rattata”
text “Tackle”; command {atttackle}
@btn2 = text ‘Quick Attack’; command {attquickattack}
@btn3 = text “Bite”; command {attbite}
@btn4 = text “Hyper Fang”; command {atthyperfang}
end
end
#define methods for attacks
def atttackle
Tk::messageBox :message => “#{$ownname} tackles the enemy
#{$enemyname}”
$enemyhp.value = $enemyhp - 10
end
def attquickattack
Tk::messageBox :message => “#{$ownname} speeds up and slams the enemy
#{$enemyname}”
$enemyhp.value = $enemyhp - 20
#Randomize extra turn
end
def attbite
Tk::messageBox :message => “#{$ownname} bites the enemy #{$enemyname}”
$enemyhp.value = $enemyhp - 30
end
def atthyperfang
Tk::messageBox :message => “#{$ownname} crunches the enemy
#{$enemyname} with his jaws”
$enemyhp.value = $enemyhp - 50
end
def comrun
Tk::messageBox :message => ‘You ran from battle!’
exit
end
Tk.mainloop