Reputation: 119
So I been trying make it so when I press my button it draws my image forever, but nothing is happening:https://gyazo.com/83017705fa2b3f40ca26112a153791c5 I'm pressing my button but nothing is happening, I want it to draw my image and make my image stay when I press my button
This is what I tried
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if Abutton.clicked:
Abutton.clicked = True
window.blit(A,(0,0))
My full code
import pygame
pygame.init()
# Drawing window screen
window = pygame.display.set_mode((700,500))
# The Name of my window
pygame.display.set_caption("StickMan")
# Drawing the buttons
A = pygame.image.load("A.png")
A2 = pygame.image.load("A1.png")
# Button class
class button1():
def __init__(self, color, x,y,width,height, text=''):
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
self.clicked = False
def draw(self,window,outline=None):
#Call this method to draw the button on the screen
if outline:
pygame.draw.rect(window, outline, (self.x-2,self.y-2,self.width+4,self.height+4),0)
pygame.draw.rect(window, self.color, (self.x,self.y,self.width,self.height),0)
if self.text != '':
font = pygame.font.SysFont('comicsans', 60)
text = font.render(self.text, 1, (0,0,0))
window.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2)))
def isOver(self, pos):
#Pos is the mouse position or a tuple of (x,y) coordinates
if pos[0] > self.x and pos[0] < self.x + self.width:
if pos[1] > self.y and pos[1] < self.y + self.height:
return True
return False
def playSoundIfMouseIsOver(self, pos, sound):
if self.isOver(pos):
if not self.over:
eat.play()
self.over = True
else:
self.over = False
# Color
white = (255,255,255)
def redrawwindow():
window.fill((0,0,0))
Abutton = button1((0,255,0),287,310,55,55, '')
Abutton.draw(window)
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if Abutton.clicked:
Abutton.clicked = True
window.blit(A,(0,0))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
redrawwindow()
pygame.display.update()
pygame.quit()
Upvotes: 1
Views: 47
Reputation: 27567
In this part of your code:
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if Abutton.isOver(pos):
window.blit(A,(0,0))
you are telling pygame to only draw A
when you click. Simply set a variable, like self.clicked = False
, and add self.clicked = True
in the if
statement. Then, instead of drawing A
in the if
block, move it out:
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if self.clicked:
window.blit(A,(0,0))
If you only want the image to show while the mouse is over the button, simply change event.type == pygame.MOUSEBUTTONDOWN
to event.type == pygame.MOUSEMOTION
.
QUESTION EDITED; NEW ANSWER:
The main problem is that you defined your Abutton
inside the redrawwindow
function.
Because you call the function during each iteration of the for
loop, the purpose of the
self.clicked
variable is lost, as in every iteration of the while
loop, the Abutton
is redefined
with self.clicked
set to False
.
A tip: in your isOver
function, you can directly return the expression, as the expression returns a bool value.
The working code:
import pygame
pygame.init()
# Drawing window screen
window = pygame.display.set_mode((700,500))
# The Name of my window
pygame.display.set_caption("StickMan")
# Drawing the buttons
A = pygame.image.load("A.png")
A2 = pygame.image.load("A1.png")
# Button class
class button1():
def __init__(self, color, x,y,width,height, text=''):
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
self.clicked = False
def draw(self,window,outline=None):
#Call this method to draw the button on the screen
if outline:
pygame.draw.rect(window, outline, (self.x-2,self.y-2,self.width+4,self.height+4),0)
pygame.draw.rect(window, self.color, (self.x,self.y,self.width,self.height),0)
if self.text != '':
font = pygame.font.SysFont('comicsans', 60)
text = font.render(self.text, 1, (0,0,0))
window.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2)))
def isOver(self, pos):
#Pos is the mouse position or a tuple of (x,y) coordinates
return pos[0] > self.x and pos[0] < self.x + self.width and pos[1] > self.y and pos[1] < self.y + self.height
def playSoundIfMouseIsOver(self, pos, sound):
if self.isOver(pos):
if not self.over:
eat.play()
self.over = True
else:
self.over = False
# Color
white = (255,255,255)
Abutton = button1((0,255,0),287,310,55,55, '')
def redrawwindow():
window.fill((0,0,0))
Abutton.draw(window)
if Abutton.clicked:
window.blit(A,(0,0))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if Abutton.isOver(pos):
Abutton.clicked = True
redrawwindow()
pygame.display.update()
pygame.quit()
Upvotes: 1