Display number of times click on screen using Gosu.button_down - ruby
I am working on a ruby program using Gosu. I want to display the number of mouse click times every time I click on a button on the screen. Please help me with this. Thank you so much
require 'rubygems'
require 'gosu'
module ZOrder
BACKGROUND, MIDDLE, TOP = *0..2
end
WIN_WIDTH = 640
WIN_HEIGHT = 400
class DemoWindow < Gosu::Window
def initialize
super(WIN_WIDTH, WIN_HEIGHT, false)
#background = Gosu::Color::WHITE
#button_font = Gosu::Font.new(20)
#info_font = Gosu::Font.new(10)
#locs = [60,60]
end
def draw
Gosu.draw_rect(0, 0, WIN_WIDTH, WIN_HEIGHT, #background, ZOrder::BACKGROUND, mode=:default)
if area_clicked(mouse_x, mouse_y)
draw_line(50, 50, Gosu::Color::BLACK, 150, 50, Gosu::Color::BLACK, ZOrder::TOP, mode=:default)
draw_line(50, 50, Gosu::Color::BLACK, 50, 100, Gosu::Color::BLACK, ZOrder::TOP, mode=:default)
draw_line(150, 50, Gosu::Color::BLACK, 150, 101, Gosu::Color::BLACK, ZOrder::TOP, mode=:default)
draw_line(50, 100, Gosu::Color::BLACK, 150, 100, Gosu::Color::BLACK, ZOrder::TOP, mode=:default)
end
Gosu.draw_rect(50, 50, 100, 50, Gosu::Color::GREEN, ZOrder::MIDDLE, mode=:default)
#button_font.draw_text("Click me", 60, 60, ZOrder::TOP, 1.0, 1.0, Gosu::Color::BLACK)
#info_font.draw_text("mouse_x: #{mouse_x}", 0, 350, ZOrder::TOP, 1.0, 1.0, Gosu::Color::BLACK)
#info_font.draw_text("mouse_y: #{mouse_y}", 100, 350, ZOrder::TOP, 1.0, 1.0, Gosu::Color::BLACK )
if Gosu.button_down? Gosu::MsLeft
index =0
button = area_clicked(mouse_x, mouse_y)
case button
when 1
index+=1
#info_font.draw("Times Click: #{index}", 370, 350, ZOrder::TOP, 1.0, 1.0, Gosu::Color::BLACK)
end
end
end
def needs_cursor?; true; end
def area_clicked(mouse_x, mouse_y)
if ((mouse_x > 50 and mouse_x < 150) and (mouse_y > 50 and mouse_y < 100))
return 1
else
end
end
def button_down(id)
if id == Gosu::KB_ESCAPE
close
else
super
end
end
end
DemoWindow.new.show
Below is the code I have added. When I click on the button, it only displays the number of times click is 1, however, I need it to display the number of times I clicked on.
This is because you're setting index back to zero every time the mouse is clicked (index =0). You should probably set your index to zero in your initialize function, and make it a class variable (add # to the front).
Then your initialize code would be:
def initialize
super(WIN_WIDTH, WIN_HEIGHT, false)
#background = Gosu::Color::WHITE
#button_font = Gosu::Font.new(20)
#info_font = Gosu::Font.new(10)
#locs = [60,60]
#index = 0
end
and the code for the mouse click would be:
if Gosu.button_down? Gosu::MsLeft
button = area_clicked(mouse_x, mouse_y)
case button
when 1
#index += 1
#info_font.draw("Times Click: #{#index}", 370, 350, ZOrder::TOP, 1.0, 1.0, Gosu::Color::BLACK)
Related
why do i need to click multiple times for the code to work?
Im making this game where you can collect pokemon and when you run into a pokeball a new page shows up and shows the pokemon you found and you can click anywhere to continue. I always have to click about 10 times for it to actually work I thought it may be because the page is in the draw function but when I took it out the draw function it didnt work. Also I dont have this problem with the start page and that is in the draw function I also have a problem that it sometimes falls through the block when I finish collecting a pokemon but that one should be a little easier to figure out. https://openprocessing.org/sketch/1786189 function preload() { sprite = loadImage('sprite1.png') lava = loadImage('lava3.png') pokeball1 = loadImage('pokeball.png') pokeball2 = loadImage('pokeball.png') pokeball3 = loadImage('pokeball.png') pokeball4 = loadImage('pokeball.png') pokeball5 = loadImage('pokeball.png') pichu = loadImage('0172Pichu.png') shinx = loadImage('shinximage.png') wooper = loadImage('194.png') hoppip = loadImage('Hoppip-Pokemon-PNG-Image.png') rowlet = loadImage('5859604c4f6ae202fedf2854.png') srowlet = loadImage('ddhnzm5-18daf778-cd90-49bf-bcc3-3cd51b9c7d02.png') gymBadges = loadImage('gymbadges.webp') } function setup() { createCanvas(700, 700); background(100); /*start = createButton('START') start.size(400,80) start.position(350,620)*/ background(194, 220, 255) //frameRate(30) /*page1 = createButton('X'); page1.position(10,30); page1.size(60,60) page1.style('background-color',255 ) page1.style('border-radius', '10px') page1.style('font-size', '30px') page1.mousePressed(back)*/ } let page = 0 let x = 10 //let y = 300 let y =580 function draw() { let bcolor = [255, 203, 164, 255]; //let bcolor = [0,0,0,255] let rightColor = get(x + 47, y + 22.5) let leftColor = get(x - 3, y + 22.5) //let downColor = get(x + 23, 350) let downColor = get(x + 23, y + 47) let upColor = get(x + 23, y + 50) let shinyOrNo = int(random(4)) //strokeWeight(10) //point(x-3,y+22.5) if (page == 0) { fill(128, 157, 255) rect(150, 300, 400, 100, 5) fill(255) textSize(60) text('START', 240, 370) textSize(28) text('use arrow keys to play', 200, 450) } if (page == 1) { background(224, 209, 255) fill(266) ellipse(x, y + 22.5, 5, 5) stroke(0) strokeWeight(5) //sky fill(210, 235, 249) rect(0, 0, 700, 700, 10) //grass noStroke() fill(148, 244, 176) rect(3, 630, 694, 67) //obstacles stroke(0) strokeWeight(2) fill(255, 203, 164) //fill(0) //noStroke() rect(70, 610, 45, 20, 2) rect(140, 590, 45, 40, 2) rect(210, 570, 45, 60, 2) rect(278, 540, 290, 25, 2) rect(600, 530, 40, 25, 2) //rect(610, 500, 40, 20, 2) rect(500, 470, 60, 25, 2) rect(430, 410, 50, 30, 2) rect(70, 370, 113, 25, 2) rect(200, 370, 60, 25, 2) rect(280, 370, 100, 25, 2) rect(15, 330, 30, 25, 2) rect(100, 290, 30, 25, 2) rect(15, 250, 30, 25, 2) rect(100, 210, 30, 25, 2) rect(175, 165, 30, 25, 2) rect(260, 130, 270, 25, 2) rect(560, 100, 100, 25, 2) fill(134, 89, 248) triangle(210, 360, 200, 370, 220, 370) triangle(230, 360, 220, 370, 240, 370) triangle(250, 360, 240, 370, 260, 370) image(lava, 310, 488, 70, 70) image(pokeball1, 222, 540, 23, 23) image(pokeball2, 520, 441, 23, 23) image(pokeball3, 150, 340, 23, 23) image(pokeball4, 18, 220, 23, 23) image(pokeball5, 420, 100, 23, 23) rect(620, 50, 30, 16) line(650, 66, 650, 99) //sprite //print (y) if (eqColor(downColor, bcolor)) { } else { y = y +2 } //y=y+2 image(sprite, x, y, 45, 45) if (keyIsPressed) { if (keyIsDown(RIGHT_ARROW)) { if (eqColor(rightColor, bcolor)) { } else { x = x + 2 } } else if (keyIsDown(LEFT_ARROW)) { if (eqColor(leftColor, bcolor)) { } else { x = x - 2 } } if (keyIsDown(UP_ARROW)) { if(eqColor(upColor,bcolor)) { }else{ y=y-6 } } } //end of keyispressed //jump //if (y = y - 6) { //y = y + 9 //} //ground if (y > 590) { y = 591 } //walk off screen if (x < 0 || x > 700) { x = 0 } else if (x > 655) { x = 655 } else if (y < 0) { y = 0 } //collisons on the top /*if (x>60 && x<90) { y = 565 }*/ //pokeball 1 if (x > 189 && x < 218 && y > 512 && y < 535) { page = 2 } if (page == 2) { fill(192, 176, 232) rect(0, 0, 700, 700) image(pichu, 230, 200, 300, 300) fill(0) strokeWeight(1) text('click anywhere to continue', 200, 600) text('You found Pichu!!', 250, 150) pokeball1 =loadImage('640px-HD_transparent_picture.png') } //print(x,y) //pokeball 2 if (x > 505 && x < 533 && y > 420 && y < 444) { page = 3 } if (page == 3) { fill(192, 176, 232) rect(0, 0, 700, 700) image(shinx, 150, 200, 450, 300) fill(0) strokeWeight(1) text('You found Shinx!!', 250, 150) textSize(19) text('fun fact: shinx is micaiah\'s favorite pokemon', 180,530) textSize(28) text('click anywhere to continue', 200, 600) pokeball2 =loadImage('640px-HD_transparent_picture.png') } //pokeball 3 if (x > 114 && x < 145 && y > 302 && y < 350) { page = 4 } if (page == 4) { fill(192, 176, 232) rect(0, 0, 700, 700) image(wooper, 230, 200, 300, 300) fill(0) strokeWeight(1) text('click anywhere to continue', 200, 600) text('You found Wooper!!', 250, 150) pokeball3 =loadImage('640px-HD_transparent_picture.png') } //pokeball 4 if (x > 0 && x < 25 && y > 200 && y < 225 && shinyOrNo == 1) { page = 5 } if (x > 0 && x < 25 && y > 200 && y < 225 && shinyOrNo == 2) { page = 5 } if (page == 5) { fill(192, 176, 232) rect(0, 0, 700, 700) image(rowlet,230,200,300,300) fill(0) strokeWeight(1) text('click anywhere to continue', 200, 600) text('You found rowlet!!', 250,150) pokeball4 =loadImage('640px-HD_transparent_picture.png') } if (x > 0 && x < 25 && y > 200 && y < 225 && shinyOrNo == 3) { page = 7 } if (page == 7) { fill(192, 176, 232) rect(0, 0, 700, 700) image(srowlet,230,200,300,300) fill(0) strokeWeight(1) text('click anywhere to continue', 200, 600) text('You found a shiny rowlet!!', 250,150) pokeball4 =loadImage('640px-HD_transparent_picture.png') } //print(page) //pokeball 5 if (x > 382 && x < 415 && y > 84 && y < 115) { page = 6 } if (page == 6) { fill(192, 176, 232) rect(0, 0, 700, 700) image(hoppip, 230, 200, 300, 300) fill(0) strokeWeight(1) text('click anywhere to continue', 200, 600) text('You found Hoppip!!', 250, 150) pokeball5 =loadImage('640px-HD_transparent_picture.png') } //lava /*if (x > 285 && x < 355 && y > 454 ) { x = 10 y = 580 }*/ point(322,502) //flag if (x > 595 && x < 620 && y < 63) { page = 8 } if (page == 8) { fill(192, 176, 232) rect(0, 0, 700, 700) fill(0) strokeWeight(1) text('your prize is all of the pokemon gym badges',100,200) image(gymBadges, 160,200,430,300) text('You finished the game!!', 220, 150) } //spikes if (x > 200 && x < 250 && y > 323 && y<375) { x = 10 y = 580 } /*if(y>330 && y<335){ y=331 }*/ } //end of page 1 //print(x,y) } //end of draw function mousePressed() { if (page ==0 && mouseX >= 150 && mouseX <= 550 && mouseY >= 300 && mouseY <= 400) { page = 1 } if (page>1) { page = 1 print(true) } } function eqColor(a, b) { return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3]; }
Hover over button
I want to make a shape in gosu to have a border when a mouse hovers over it. Gosu.draw_rect(50, 50, 100, 50, Gosu::Color::GREEN, ZOrder::TOP, mode=:default) I expect the output, the shape will have a black border around it when the mouse cursor hovers over the rectangle
You can add a check in update routine. def update if mouse_over?(50, 50, 150, 100) Gosu.draw_rect(50, 50, 100, 50, Gosu::Color::GREEN, ZOrder::TOP, mode=:default) end end def mouse_over?(px1, py1, px2, py2) mx = mouse_x my = mouse_y ((mx >= px1) && (my >= py1)) && (mx <= px2) && (my <= py2) end mouse_x and mouse_y are variable provided by Gosu. HERE MORE INFO...
How can I create an array from a method with ruby
How can I make an array of 30 min intervals to 8 hours. so this ish: [30, 60, 90, all-the-way-to, 480]
You can use a Range and the step method, then convert it to an Array: (30..480).step(30).to_a The result is: [30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360, 390, 420, 450, 480)
Your arguments are increment = 30 duration = 480 # 8*60 You could use increment.step(by: increment, to: duration).to_a #=> [ 30, 60, 90, 120, 150, 180, 210, 240, # 270, 300, 330, 360, 390, 420, 450, 480] which reads well. Numeric#step, when used without a block, returns an enumerator, which is why .to_a is needed.
I came up with this, but #infused answer is way better. a = (1..16).to_a.map{|i| i*30 }
Option selecting (Enumerable#select) from the range: stop = 480 step = 30 (step..stop).select { |n| n % step == 0 } #=> [0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360, 390, 420, 450, 480]
Ruby green shoes timer
I'm building this popup countdown timer for a game. The issue is I can't figure out how to update the the animate so that the display changes too. It works so far, but you can't see the numbers changing. It blends into the 00:00. I'm pretty positive it works so far just having trouble with this change. This is done with green shoes. Any ideas what I'm doing wrong here? #Timer button used for creating a countdown timer for the game. #Game ends when timer is zero. flow width: 80, height: 0.2 do button 'Timer', width: 1.0, height: 1.0 do window do def time_change! #clock = '%02d:%02d' % [#second / 60, #second % 60] if(#second == 0) alert "game is over" #clock = '00:00' close() para #clock, size: 50, stroke: black end end background whitesmoke #clock = '00:00' para #clock, size: 50, stroke: black #second = 10 animate(1) do #second -= 1 time_change! para #clock, size: 50, stroke: black end end end
You can replace the text of the current para which displays the clock: Shoes.app do flow do button 'Timer', width: 100, height: 50 do window width: 200, height: 100 do #Open a child window when the button is clicked seconds = 3 tenths = 0 clock = '%02d:%02d' flow do #p = para clock % [seconds, tenths], #Assign the clock para to an instance variable, size: 50, #which will make the variable visible outside this block. stroke: black end a = animate(10) do #Upate the clock every 1/10 of a second. tenths -= 1 if tenths < 0 tenths = 9 seconds -= 1 end #p.text = clock % [seconds, tenths] #Replace the clock text. if seconds == 0 and tenths == 0 a.stop alert("game over") close #=>self.close where self is the window whose block contains this code end end #animate end #window end #button end #flow end #app To verify that the clock is actualy showing each time increment, you can slow things down by changing animate(10) to animate(1).
unable to enter data in Input Control of a GUI in autoit
The below one is my code. I am unable to click and edit in the second Input control. #include <GUIConstants.au3> $gui = GuiCreate("Hello World", 700, 600) $Label_HelloWorld = GuiCtrlCreateLabel("Path / Directory", 40, 20, 300, 18) $file = GUICtrlCreateInput("", 140, 20, 300, 20) $Label_boot = GuiCtrlCreateLabel("path of boot.c", 40, 60, 300, 18) $file2 = GUICtrlCreateInput("", 140, 60, 300, 20) $Button_OK = GuiCtrlCreateButton("CHECK", 400, 90, 50, 20) GuiSetState(#SW_SHOW, $gui) Sleep(10000)
Your labels are overlapping the controls (again), this time horizontally. A width of 300px when the inputs are 100px to the right means the first 200px is overlapping. If you try to click in the last 100px of the input then it will work. This is very easy to check for, just use the autoit window info tool and look at the outlines of the controls.