Why is my text not showing in Pygame window? - python

this is my first time trying to code in general so I brought upon myself to learn how to make a pygame.
I decided to go for a simple cookie clicker
I couldn't get the text to render with the cookie counted value can someone please help me?
The zip with image and font file
# 1 - Import packages
import pygame
# 2 - Define constants
width=700
height=700
FRAMES_PER_SECOND = 30
x = 20; # x coordnate of image
y = 30; # y coordinate of image
WHITE = (255, 255, 255)
BLACK = (0,0,0)
score = 0
X = 400 #x coord for text
Y = 400 # coord for text
# 3 - Initialize the world
pygame.init()
screen = pygame.display.set_mode( (width, height ) )
screen.fill(WHITE)
clock = pygame.time.Clock()
pygame.display.set_caption('Cookie Clicker simplified')
cookie = pygame.image.load("CookieBig.png")
# 4 - Load assets: image(s), sounds, etc.
cookie = pygame.image.load("CookieBig.png")
screen.blit(cookie , ( x,y)) # paint to screen
pygame.display.flip() # paint screen one time
# 5 - Initialize variables
# Text Display
font = pygame.font.Font('arial.ttf', 32)
text = font.render('COOKIES : ', True, BLACK, WHITE)
textRect = text.get_rect()
textRect.center = (X // 2, Y // 2)
# 6 - Loop forever
running = True
while (running):
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
# Set the x, y postions of the mouse click
x, y = event.pos
if cookie.get_rect().collidepoint(x, y):
score += 1
print("Cookie:",score)
pygame.display.update()

text is a pygame.Surface object. You have to blit the text Surface on the Surface which is associated to the window, in the main application loop.
screen.blit(text, textRect)
Recreate the text surface, when the score changes
text = font.render('COOKIES : ' + str(score), True, BLACK, WHITE)
A pygame.Surface object has not position. Thus the position which is returned by .get_rect() is (0, 0). Set the position by a keyword argument, when you retrieve the pygame.Rect from the Surface:
cookie_pos = (x, y)
x, y = event.pos
if cookie.get_rect(topleft = cookie_pos).collidepoint(x, y):
# [...]
I recommend to clear the display, draw the text and update the display in the main application loop rather than the event loop:
cookie_pos = (x, y)
# 6 - Loop forever
running = True
while running:
# handle the events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
# Set the x, y postions of the mouse click
x, y = event.pos
if cookie.get_rect(topleft = cookie_pos).collidepoint(x, y):
score += 1
text = font.render('COOKIES : ' + str(score), True, BLACK, WHITE)
# <--|
# clear display
screen.fill(WHITE)
# draw scene
screen.blit(cookie, cookie_pos)
screen.blit(text, textRect)
# update display
pygame.display.update()
Note, the main application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()

The problem is your not putting it on the screen. Like the cookie image, you need to do screen.blit(text,textRect). I would also recommend putting them in the loop so it gets put on the screen every frame. This means if you change x or y, it will move. So something like this
running = True
while (running):
screen.fill(WHITE) #reset the screen
screen.blit(cookie , ( x,y)) # paint to screen
text = font.render('COOKIES : ', True, BLACK)
screen.blit(text,textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
# Set the x, y postions of the mouse click
x, y = event.pos
if cookie.get_rect().collidepoint(x, y):
score += 1
print("Cookie:",score)
pygame.display.update()

Related

My pygame timer keeps resetting after I hit a target in my aim game

I am trying to make an aim game where a target pops up and once the player clicks on it, the target vanishes and a new one appears in a random location, I want it so that there is a 10 second timer but it keeps going back to 10 each time a target is "hit"
import pygame, random as r, time
FPS = 60
WIDTH = 900
HEIGHT = 500
WHITE = 255,255,255
BG = 26,26,26
RANGEXMIN = 20
RANGEXMAX = 840
RANGEYMIN = 20
RANGEYMAX = 440
window = pygame.display.set_mode((WIDTH, HEIGHT))
tick = pygame.USEREVENT
pygame.time.set_timer(tick,1000)
pygame.font.init()
FONT = pygame.font.Font('slkscr.ttf', 50)
def aim_train():
def new_target(countdown,text):
clock = pygame.time.Clock()
x = r.randint(RANGEXMIN, RANGEXMAX)
y = r.randint(RANGEYMIN, RANGEYMAX)
hit = False
while not hit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.USEREVENT:
if event.type == tick:
countdown= countdown - 1
text = str(countdown)
clock.tick(FPS)
window.fill(BG)
timer = FONT.render(text, False, WHITE)
window.blit(timer, (435, 20))
pygame.mouse.set_visible(False)
pos = pygame.mouse.get_pos()
pos_x = pos[0]
pos_y = pos[1]
target = pygame.draw.rect(window, WHITE, (x,y,50,50))
cursor_outline = pygame.draw.circle(window, BG, (pos_x,pos_y), 11)
cursor = pygame.draw.circle(window, WHITE,(pos_x,pos_y) ,10)
hit = (pygame.mouse.get_pressed()[0] and target.colliderect(cursor_outline))
pygame.display.update()
run = True
countdown = 10
text = str(countdown)
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill(BG)
pygame.mouse.set_visible(False)
new_target(countdown,text)
pygame.quit()
aim_train()
The variable "counter" somehow gets reset back to 10 after ever successful hit on a target
The problem is that you use a seperate function, new_target, for each target.
The countdown variable is defined inside the scope of the function aim_train. Because the new_target function is defined inside the aim_train function, it is a nested function and it can use all the variables that are defined inside aim_train. However, the new_target function still has its own scope. It can make changes to any variables defined inside aim_train, but those changes remain in its own scope. When a call of new_target is ended, its scope is discarded and all changes to the variables of aim_target are undone. This causes the countdown variable to be reset every in new call of new_target and thus every time a new target is created.
You might also have noticed that you can't close your window. The window doesn't react to clicking the red cross. This is because the same applies to the run variable. When you click the red cross, the run variable is set to True inside new_target, but not in the scope of aim_train. As such, the main loop in aim_train is not quitted and the program continues.
As a solution to this problem, I would recommend to include all the code of new_target into the aim_train function. Then you only have one function, which makes that all changes to variables are in the same scope and no changes are discarded:
import pygame, random as r, time
pygame.init()
FPS = 60
WIDTH = 900
HEIGHT = 500
WHITE = 255,255,255
BG = 26,26,26
RANGEXMIN = 20
RANGEXMAX = 840
RANGEYMIN = 20
RANGEYMAX = 440
window = pygame.display.set_mode((WIDTH, HEIGHT))
tick = pygame.USEREVENT
pygame.time.set_timer(tick,1000)
pygame.font.init()
FONT = pygame.font.SysFont('arial', 50)
def aim_train():
run = True
hit = False
countdown = 10
text = str(countdown)
x = r.randint(RANGEXMIN, RANGEXMAX)
y = r.randint(RANGEYMIN, RANGEYMAX)
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)#switched
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == tick: #changed
countdown= countdown - 1
text = str(countdown)
window.fill(BG)
timer = FONT.render(text, False, WHITE)
window.blit(timer, (435, 20))
pos = pygame.mouse.get_pos()
pos_x = pos[0]
pos_y = pos[1]
target = pygame.draw.rect(window, WHITE, (x,y,50,50))
cursor_outline = pygame.draw.circle(window, BG, (pos_x,pos_y), 11)
cursor = pygame.draw.circle(window, WHITE,(pos_x,pos_y) ,10)
hit = (pygame.mouse.get_pressed()[0] and target.colliderect(cursor_outline))
if hit:
hit = False
x = r.randint(RANGEXMIN, RANGEXMAX)
y = r.randint(RANGEYMIN, RANGEYMAX)
pygame.display.update()
pygame.quit()
aim_train()
Apart from fixing the problem, I have also restructured your code a bit and did the following changes:
I have changed:
elif event.type == pygame.USEREVENT:
if event.type == tick:
into:
elif event.type == tick:
Outside of the aim_train function, you have stated that tick and pygame.USEREVENT are equal. As such, it is useless to compare to them two times, because if the first check is true, then the second one will certainly be.
I've placed pygame.mouse.set_visible(False) outside of the main loop.
Calling the function sets the mouse invisible untill another call changes sets the mouse back to visible. As such, it is useless to call it multiple times in the loop.
There are actually 2 countdown variables, one in the new_target function and one in the aim_train function. If you change the variable countdown in the new_target function, this will not change the variable countdown in the aim_train function. You must return the new value of countdown from the new_target function:
def aim_train():
def new_target(countdown):
# [...]
while not hit:
for event in pygame.event.get():
# [...]
elif event.type == pygame.USEREVENT:
if event.type == tick:
countdown= countdown - 1
# [...]
return countdown
run = True
countdown = 10
while run:
# [...]
countdown = new_target(countdown)
However, I suggest that you restructure your code. Do not use nested application loops. Also see Pygame mouse clicking detection:
import pygame, random as r
FPS = 60
WIDTH = 900
HEIGHT = 500
WHITE = 255,255,255
BG = 26,26,26
RANGEXMIN = 20
RANGEXMAX = 840
RANGEYMIN = 20
RANGEYMAX = 440
window = pygame.display.set_mode((WIDTH, HEIGHT))
tick = pygame.USEREVENT
pygame.time.set_timer(tick,1000)
pygame.font.init()
FONT = pygame.font.Font('slkscr.ttf', 50)
def new_target():
x = r.randint(RANGEXMIN, RANGEXMAX)
y = r.randint(RANGEYMIN, RANGEYMAX)
return x, y
def aim_train():
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
run = True
countdown = 10
hits = 0
countdownSurf = FONT.render(f'time {countdown}', False, WHITE)
hitsSurf = FONT.render(f'hits {hits}', False, WHITE)
target = pygame.Rect(0, 0, 50, 50)
target.center = new_target()
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == tick:
countdown -= 1
countdownSurf = FONT.render(f'time {countdown}', False, WHITE)
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if target.collidepoint(event.pos):
target.center = new_target()
hits += 1
hitsSurf = FONT.render(f'hits {hits}', False, WHITE)
pos = pygame.mouse.get_pos()
window.fill(BG)
window.blit(countdownSurf, countdownSurf.get_rect(center = (300, 45)))
window.blit(hitsSurf, hitsSurf.get_rect(center = (600, 45)))
pygame.draw.rect(window, WHITE, target)
pygame.draw.circle(window, BG, pos, 11)
pygame.draw.circle(window, WHITE, pos, 10)
pygame.display.update()
pygame.quit()
aim_train()

What is the correct way to resize an image in pygame?

I have an image in pygame and my code detects if this image is clicked on. It worked fine and I decided to resize the image, but when I did that the image randomly disappeared. Here was the code before the image disappeared:
import pygame
pygame.init()
width = 500
height = 500
screen = pygame.display.set_mode((width, height))
white = (255, 255, 255)
screen.fill(white)
pygame.display.set_caption('Aim Trainer')
target = pygame.image.load("aim target.png").convert_alpha()
x = 20 # x coordinate of image
y = 30 # y coordinate of image
screen.blit(target, (x, y)) # paint to screen
pygame.display.flip() # paint screen one time
targetSize = pygame.transform.scale(target, (5, 3))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
# Set the x, y positions of the mouse click
x, y = event.pos
if target.get_rect().collidepoint(x, y):
print('clicked on image')
# loop over, quite pygame
pygame.quit()
and here is my code after:
import pygame
pygame.init()
width = 500
height = 500
screen = pygame.display.set_mode((width, height))
white = (255, 255, 255)
screen.fill(white)
pygame.display.set_caption('Aim Trainer')
target = pygame.image.load("aim target.png").convert_alpha()
x = 20 # x coordinate of image
y = 30 # y coordinate of image
pygame.display.flip() # paint screen one time
targetSize = pygame.transform.scale(target, (5, 3))
screen.blit(targetSize, (x, y)) # paint to screen
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
# Set the x, y positions of the mouse click
x, y = event.pos
if target.get_rect().collidepoint(x, y):
print('clicked on image')
# loop over, quite pygame
pygame.quit()
As you can see, the only thing that changed was me renaming screen.blit(target, (x, y)) to screen.blit(targetSize, (x, y)), and me moving this line of code a few lines furthur down to avoid a 'TargetSize is not defined' error. But for some reason, this change makes the image disappear. The program still detects it when I click on the image, it's just that the image isn't visible.
You have to get the rectangle from the scaled iamge
pygame.Surface.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. A Surface is blit at a position on the screen. The position of the rectangle can be specified by a keyword argument. For example, the top left of the rectangle can be specified with the keyword argument topleft. These keyword argument are applied to the attributes of the pygame.Rect before it is returned (see pygame.Rect for a full list of the keyword arguments).
Update the display after drawing the image
targetSize = pygame.transform.scale(target, (5, 3))
target_rect = targetSize.get_rect(topleft = (x, y))
screen.blit(targetSize, (x, y))
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if target_rect.collidepoint(event.pos):
print('clicked on image')

How do you run a function when you click on an image in pygame?

How would you run a function when you click on an image in pygame? In my program, I want to run a specific function when you click on a certain image. The problem is, I have no idea how to do that. Is it even possible to do that? Here is my code below...
import pygame
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
pygame.init()
size = (500, 400)
screen = pygame.display.set_mode(size)
pygame.draw.rect(screen, red,(150,450,100,50))
button1 = pygame.Rect(100,100,50,50)
button2 = pygame.Rect(200,200,50,50)
button3 = pygame.Rect(130,250,50,50)
pygame.display.set_caption("Yami no Game")
txt = pygame.image.load('txt.png')
Stxt = pygame.transform.scale(txt,(48,48))
exe = pygame.image.load('exe.jpg')
Sexe = pygame.transform.scale(exe,(48,48))
done = False
clock = pygame.time.Clock()
background_image=pygame.image.load('windows_background.jpg').convert()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
100, 100 = event.pos
if Sexe.get_rect().collidepoint(100,100):
print('Runnig thing')
screen.blit(background_image, [0,0])
screen.blit(Stxt,[100,100])
screen.blit(Sexe,[250,250])
pygame.display.update()
clock.tick(60)
pygame.quit()
Detect for a mouseclick then check the position of the mouse when the click occurred and see whether it was within the image by using the collidepoint function:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
mousePos = pygame.mouse.get_pos()
if Sexe.get_rect().collidepoint(mousePos):
runFunction()
In general you speak of using a button to execute something. For this we need to know where the player clicked with the mouse, test if it is inside the area that depicts the image (our "button") and if so, execute a function. Here is a small example:
# get the mouse clicks
mouse = pygame.mouse.get_pos() # position
click = pygame.mouse.get_pressed() # left/right click
if img.x + img.width > mouse[0] > img.x and img.y + img.height > mouse[1] > img.y: # Mouse coordinates checking.
if click[0] == 1: # Left click
my_function_on_click()
It requires your image object to have an x and a y coordinate as well as a defined height and width. It is a lot easier if your image object has a rect the same size as you can call on that rect, or as the other answer pointed out use the collidepoint function.
Minimal example using the code you copied into the comments:
width = 48
height = 48
x = 100
y = 100
exe = pygame.image.load('exe.jpg')
Sexe = pygame.transform.scale(exe,(width,height))
while not done:
screen.blit(Sexe,[x,y]) # blit image
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
mouse = pygame.mouse.get_pos()
position click = pygame.mouse.get_pressed() # left/right click
# Mouse coordinates checking:
sexe_rect = Sexe.get_rect()
if sexe_rect.x + sexe_rect.width > mouse[0] > sexe_rect.x and sexe_rect.y + sexe_rect.height > mouse[1] > sexe_rect.y:
# if Sexe.get_rect().collidepoint(mousePos): # Alternative, A LOT shorter and more understandable
if click[0] == 1: # Left click
print("I GOT CLICKED!")

Pygame blit image with mouse event

I use Pygame in this code. This is like a game that when user hit mouse button, from the mouse position comes a laser image that will go up, and eventually go out of the screen. I am trying to blit an image when the user hit mouse button. This code I am using does not work and I do not know why. My problem starts at the main for loop
import pygame
# Initialize Pygame
pygame.init()
#___GLOBAL CONSTANTS___
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# Set the height and width of the screen
screen_width = 500
screen_height = 500
screen = pygame.display.set_mode([screen_width, screen_height])
#Load Laser image of spaceship
laser_image = pygame.image.load('laserRed16.png').convert()
#Load sound music
sound = pygame.mixer.Sound('laser5.ogg')
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
# Get the current mouse position. This returns the position
# as a list of two numbers.
sound.play()
#Get the mouse position
mouse_position = pygame.mouse.get_pos()
mouse_x = mouse_position[0]
mouse_y = mouse_position[1]
# Set the laser image when the spaceship fires
for i in range(50):
screen.blit(laser_image,[mouse_x + laser_x_vector,mouse_y + laser_x_vector])
laser_x_vector += 2
laser_x_vector += 2
# Clear the screen
screen.fill(WHITE)
#Limit to 20 sec
clock.tick(20)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
pygame.quit()
You fill the screen after you blit the lazer, so the lazer will not appear. You should fill before you blit the lazer so the lazer appears.
The others have already explained why you don't see the laser. Here's a working solution for you. First I suggest to use pygame.Rects for the positions of the lasers and put them into a list (rects can also be used for collision detection). Then iterate over these positions/rects in the main while loop, update and blit them. I also show you how to remove rects that are off screen.
import pygame
pygame.init()
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
screen_width = 500
screen_height = 500
screen = pygame.display.set_mode([screen_width, screen_height])
laser_image = pygame.Surface((10, 50))
laser_image.fill(GREEN)
done = False
clock = pygame.time.Clock()
laser_rects = []
laser_velocity_y = -20
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
# Turn the mouse position into a rect with the dimensions
# of the laser_image. You can use the event.pos instead
# of pygame.mouse.get_pos() and pass it as the `center`
# or `topleft` argument.
laser_rect = laser_image.get_rect(center=event.pos)
laser_rects.append(laser_rect)
remaining_lasers = []
for laser_rect in laser_rects:
# Change the y-position of the laser.
laser_rect.y += laser_velocity_y
# Only keep the laser_rects that are on the screen.
if laser_rect.y > 0:
remaining_lasers.append(laser_rect)
# Assign the remaining lasers to the laser list.
laser_rects = remaining_lasers
screen.fill(WHITE)
# Now iterate over the lasers rect and blit them.
for laser_rect in laser_rects:
screen.blit(laser_image, laser_rect)
pygame.display.flip()
clock.tick(30) # 30 FPS is smoother.
pygame.quit()

How to move an image with the mouse in pygame?

I've written a little pygame program for moving an image by clicking and moving the mouse.
I struggle with making the moving function movableImg to work with my own x, y parameters and not with the predefined x, y parameters as it is now.
Here is my code:
import pygame
import time
pygame.init()
display_width = 800
display_height = 600
white = (255, 255, 255)
gameDisplay = pygame.display.set_mode((display_width, display_height))
clock = pygame.time.Clock()
drag = 0 #switch with which I am seting if I can move the image
x = 100 #x, y coordinates of the image
y = 100
img = pygame.image.load('button.png') #my image and then his width and height
imgWidth = 100
imgHeight = 100
def image(imgX,imgY): #function to blit image easier
gameDisplay.blit(img, (imgX, imgY))
def movableImg(): #function in which i am moving image
global drag, x, y
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
image(x, y)
if click[0] == 1 and x + imgWidth > mouse[0] > x and y + imgHeight > mouse[1] > y: #asking if i am within the boundaries of the image
drag = 1 #and if the left button is pressed
if click[0] == 0: #asking if the left button is pressed
drag = 0
if drag == 1: #moving the image
x = mouse[0] - (imgWidth / 2) #imgWidth / 2 because i want my mouse centered on the image
y = mouse[1] - (imgHeight / 2)
def main_loop():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
movableImg()
pygame.display.update()
clock.tick(60)
main_loop()
pygame.quit()
quit()
So here's the code that I found on the internet and which is working as I want to. SOURCE
import os,sys
import pygame as pg #lazy but responsible (avoid namespace flooding)
class Character:
def __init__(self,rect):
self.rect = pg.Rect(rect)
self.click = False
self.image = pg.Surface(self.rect.size).convert()
self.image.fill((255,0,0))
def update(self,surface):
if self.click:
self.rect.center = pg.mouse.get_pos()
surface.blit(self.image,self.rect)
def main(Surface,Player):
game_event_loop(Player)
Surface.fill(0)
Player.update(Surface)
def game_event_loop(Player):
for event in pg.event.get():
if event.type == pg.MOUSEBUTTONDOWN:
if Player.rect.collidepoint(event.pos):
Player.click = True
elif event.type == pg.MOUSEBUTTONUP:
Player.click = False
elif event.type == pg.QUIT:
pg.quit(); sys.exit()
if __name__ == "__main__":
os.environ['SDL_VIDEO_CENTERED'] = '1'
pg.init()
Screen = pg.display.set_mode((1000,600))
MyClock = pg.time.Clock()
MyPlayer = Character((0,0,150,150))
MyPlayer.rect.center = Screen.get_rect().center
while 1:
main(Screen,MyPlayer)
pg.display.update()
MyClock.tick(60)
You don't need the function. All you need is some events in your for loop. Instead of a messy, complicated function, you can simply figure out when the mouse is clicked:
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN: #Remember to use: from pygame.locals import *
coordinates = pygame.mouse.get_pos()
#set x and y to respective values of coordinates
#Enter necessary code here after
Now coordinates is a pre-defined variable I made up here, in this case, it is used to store the coordinates of your mouse. When event.type is MOUSEBUTTONDOWN, it means that the mouse has been clicked and the program should begin to do the code under that if statement. The events will only activate once, whihc means you cannot drag. I recommend that you create a function that allows the event to continue if the user is holding down on the mouse like this:
def holding():
global held, coordinates
if held:
coordinates = pygame.mouse.get_pos()
#Enter code here
held will be a Boolean variable: it will be equal to True if the mouse if being clicked or to False if not. In this case to prevent the program to think you are clicking the mouse infinitely, add another if statement to check whether the mouse has been released and if so, change held to False:
if event.type == MOUSEBUTTONUP:
held = False
also, to make sure that the function will actually register the fact that the mouse is still being held down, put this line in the if statement for the MOUSEBUTTONDOWN event:
held = True
And finally, to run the function, add an if statement in front of your for loop to run the function when needed:
if held:
holding
TO move multiple images, change their positions to be equal to coordinates or somehow related to coordinates. The if statements do not have to move only one image at a time.

Categories