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...
Related
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)
In the below image, I would like to have the score as text within the green ellipse. However, the ellipse is being drawn over the text, regardless of the order of the ellipse() and text() functions in the loop. Can anyone suggest why? My draw loop is shown below.
import processing.core.PApplet;
import processing.core.PFont;
public void drawHUD(PApplet marker, Clock time, int score)
{
PFont font = marker.createFont("Impact", 25, true);
marker.textFont(font);
marker.ellipseMode(CENTER);
fill(25, 100, 25);
marker.ellipse(50, marker.height - 50, 75, 50);
marker.noFill();
marker.text("Score: ", 25, marker.height - 100);
marker.text(score, 50, marker.height - 50);
marker.text("Seconds left: ", marker.width - 175, marker.height - 100);
marker.text(time.toString(), marker.width - 125, marker.height - 50);
}
Try replacing marker.noFill(); with marker.fill(255);. I don't believe noFill() works for text, and I have a feeling the text is being drawn above the ellipse, but is just the same colour as the ellipse so can't be seen.
I deleted the ellipsemode. It got rid of the center ellipse, but it got the text to the front.
Here is Ruby code:
require 'rmagick'
include Magick
img = Image.new(300, 300)
draw = Draw.new
draw.line(0, 150, 300, 150)
draw.line(150, 0, 150, 300)
# for each of known gravity constants...
%w[NorthWestGravity NorthGravity NorthEastGravity WestGravity CenterGravity EastGravity SouthWestGravity SouthGravity SouthEastGravity].
each{|g|
# set gravity to this value...
draw.gravity Magick.const_get(g)
# ...and draw text with this constant name
draw.text 150, 150, g
}
draw.draw(img)
img.write('tmp/gravity.png')
Here is image which it produces:
For SouthEast/NorthWest and similar gravities result is as expected (text is near 150,150, moved in desired direction). But for South, North and others result is really pretty weird.
As far as I can understand from code, RMagick just translates gravity and text commands into corresponding ImageMagick drawing primitives, so, I suppose its something in ImageMagick's gravity concept that I can't get.
What is it?..
I suppose its something in ImageMagick's gravity concept that I can't get.
What is it?..
The key to understanding what's going on is to locate the CenterGravity text.
Shifted left by 150px, and down by 150px.
Now compare compare NorthWestGravity position.
Also translated left & down by 150px respectively. Seeing a trend?
Your issue is with this line...
draw.text 150, 150, g
The Magick::Draw API maps to MVG spec. Use Magick::Draw.push & Magick::Draw.pop to control drawing context.
Edit from comments...
For setting the origin of text to be drawing, you'll need to calculate the position after evaluation the text/type metrics.
Example.
require 'rmagick'
include Magick
img = Image.new(300, 300) {
self.background_color = "palegreen"
}
draw = Draw.new
dotes = Draw.new # Dotes added for point of origin
dotes.fill = "red"
cursor = 1
# for each of known gravity constants...
%w[NorthWestGravity NorthGravity NorthEastGravity WestGravity CenterGravity EastGravity SouthWestGravity SouthGravity SouthEastGravity].
each{|g|
offsetX = 150
offsetY = cursor * 25
dotes.circle offsetX, offsetY, offsetX+2, offsetY+2
# Get metrics of text
metrics = draw.get_type_metrics(img, g)
# Full width
if %w[NorthWestGravity WestGravity SouthWestGravity].include? g then
offsetX -= metrics[:width]
end
# Full height
if %w[SouthWestGravity SouthGravity SouthEastGravity].include? g then
offsetY += metrics[:ascent]
end
# Half width
if %w[NorthGravity SouthGravity CenterGravity].include? g then
offsetX -= metrics[:width] / 2
end
# Half height
if %w[WestGravity CenterGravity EastGravity].include? g then
offsetY += metrics[:ascent] / 2
end
draw.text offsetX, offsetY, g
cursor += 1
}
dotes.draw(img)
draw.draw(img)
img.write('output.png')
So I have been working on a game and for some odd reason down in the level1 function I thought if I had a while loop with leveltext1 = false then I could write the text onto the screen, and then wait 2 seconds and then make level1text = true and it would go away. I guess it didnt...
Code:
#!/usr/bin/python
import pygame
import time
pygame.init()
blue = (25,25,112)
black = (0,0,0)
red = (200,0,0)
bright_red = (255,0,0)
white = (255,255,255)
groundcolor = (139,69,19)
green = (80,80,80)
other_green = (110,110,110)
lives = 0
gameDisplay = pygame.display.set_mode((1336,768))
pygame.display.set_caption("TheAviGame")
direction = 'none'
clock = pygame.time.Clock()
img = pygame.image.load('guyy.bmp')
famousdude = pygame.image.load('kitten1.bmp')
kitten = pygame.image.load('macharacterbrah.bmp')
dorritosman = pygame.image.load('Doritos.bmp')
backround = pygame.image.load('backroundd.bmp')
playernormal = pygame.image.load('normalguy.bmp')
playerocket = pygame.image.load('rocketguyy.bmp')
rocketcat = pygame.image.load('jetpackcat.bmp')
mlglogo = pygame.image.load('mlg.bmp')
level1bg = pygame.image.load('level1background.bmp')
def mts(text, textcolor, x, y, fs):
font = pygame.font.Font(None,fs)
text = font.render(text, True, textcolor)
gameDisplay.blit(text, [x,y])
def button(x,y,w,h,ic,ac):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
if click[0] == 1:
gameloop()
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(black)
button(450,450,250,70,green,other_green)
mts("Play", white, 540, 470, 50)
mts("THE AVI GAME", red, 380, 100, 100)
gameDisplay.blit(famousdude, (100,100))
gameDisplay.blit(kitten, (700,300))
gameDisplay.blit(dorritosman, (1000,400))
pygame.display.update()
clock.tick(15)
def gameloop():
imgx = 1000
imgy = 100
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
imgx += 10
gameDisplay.blit(backround, (30,30))
gameDisplay.blit(rocketcat, (imgx,imgy))
pygame.display.update()
clock.tick(15)
def level1():
imgx = 500
imgy = 500
leveltext = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
while not leveltext:
gameDisplay.blit(level1bg, (0,0))
mts("LEVEL 1:", red, 450, 220, 60)
mts('Controls: W or Up to move up.', white, 450, 300, 40)
mts('Controls: A or Left to move left.', white, 450, 340, 40)
mts('Controls: S or Down to move down.', white, 450, 390, 40)
mts('Controls: D or Right to move Right.', white, 450, 430, 40)
time.sleep(2)
leveltext = True
pygame.display.update()
level1()
From my understanding, when you blit text onto a surface it permanently becomes part of that surface. If you want the font to disappear, you could try blitting it onto a copy of that surface and then blit the altered surface to the display.
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.