Reputation: 13
The program start normally and display the window with the image which moves when I click on it with the function create inside (the part of the code have 2 comment beside it) :
import Units_sol_1
import pygame
import sys
pygame.init()
clock = pygame.time.Clock()
resolution = (1080, 720)
pygame.display.set_caption("AWO")
surface = pygame.display.set_mode((1080, 720))
recon1_x = 0
recon1_y = 0
running = True
while running:
for event in pygame.event.get():
surface.fill((0, 0, 0))
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
recon1 = Units_sol_1.Recon(recon1_x, recon1_y)
pygame.display.update()
surface.blit(recon1.icon, recon1.pos)
#this is the code below
if recon1.pos <= pygame.mouse.get_pos() <= (recon1_x + recon1.w_icon, recon1_y + recon1.h_icon):
print("work1")
if event.type == pygame.MOUSEBUTTONDOWN:
recon1_x += 1
recon1_y += 1
recon1.w_icon += 1
recon1.h_icon += 1
print(recon1.pos)
#this is the code upside
pygame.display.flip()
clock.tick(60)
And below the class fonction Recon (Units_sol_1):
import pygame
class Recon(Vehicule1, pygame.sprite.Sprite):
def __init__(self, posx, posy):
super().__init__(posx, posy)
self.pos = (posx, posy)
self.w_icon = 33
self.h_icon = 33
self.icon = pygame.image.load("Unité/recon_l.png")
Then the same code with the class fonction that don't move the image when i click on it :
import Units_sol_1
import pygame
import sys
pygame.init()
clock = pygame.time.Clock()
resolution = (1080, 720)
pygame.display.set_caption("AWO")
surface = pygame.display.set_mode((1080, 720))
recon1_x = 0
recon1_y = 0
running = True
while running:
for event in pygame.event.get():
surface.fill((0, 0, 0))
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
recon1 = Units_sol_1.Recon(recon1_x, recon1_y)
pygame.display.update()
surface.blit(recon1.icon, recon1.pos)
recon1.icon_moving(recon1_x, recon_y)
pygame.display.flip()
horloge.tick(60)
And the class with the class fonction (Units_sol_1) :
import pygame
class Recon(Vehicule1, pygame.sprite.Sprite):
def __init__(self, posx, posy):
self.pos = (posx, posy)
self.w_icon = 33
self.h_icon = 33
self.icon = pygame.image.load("Unité/recon_l.png")
def icon_moving(self, posx, posy):
for event in pygame.event.get():
if self.pos <= pygame.mouse.get_pos() <= (posx + self.w_icon, posy + self.h_icon):
print("ok 1")
if event.type == pygame.MOUSEBUTTONDOWN:
print("ok 2")
posx += 1
posy += 1
self.w_icon += 1
self.h_icon += 1
print(self.pos)
My problem is that I don't know how to make a class function that work; Idk why the class function don't move the image when I click on it.
Upvotes: 0
Views: 58
Reputation: 210909
You have to create the instance of Recon
before the application loop. However you have to draw the object continuously in the application loop.
Additionally you have to change the position attributes self.pos[0]
and self.pos[1]
when you move the object.
pygame.event.get()
get all the messages and remove them from the queue. See the documentation:
This will get all the messages and remove them from the queue. [...]
If pygame.event.get()
is called in multiple event loops, only one loop receives the events, but never all loops receive all events. As a result, some events appear to be missed.
Get the events once per frame and use them in multiple loops or pass the list of events to functions and methods where they are handled:
class Recon(Vehicule1, pygame.sprite.Sprite):
def __init__(self, posx, posy):
self.pos = [posx, posy]
self.w_icon = 33
self.h_icon = 33
self.icon = pygame.image.load("Unité/recon_l.png")
def icon_moving(self, event_list):
for event in event_list:
if event.type == pygame.MOUSEBUTTONDOWN:
rect = pygame.Rect(*self.pos, self.w_icon, self.h_icon)
if rect.collidepoint(event.pos):
self.pos[0] += 1
self.pos[1] += 1
recon1 = Units_sol_1.Recon(recon1_x, recon1_y)
running = True
while running:
event_list = pygame.event.get()
for event in event_list:
surface.fill((0, 0, 0))
if event.type == pygame.QUIT:
running = False
recon1.icon_moving(event_list)
surface.fill(0)
surface.blit(recon1.icon, recon1.pos)
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()
Upvotes: 1