when I click on the image it is supposed to show me an image on top of it and the image stays until the mouse button is up so i put if event.type == pygame.MOUSEBUTTONUP:
then blit
the image but still it stays for a second after the mouse button is up im sure its because of my if statement here is my code:
where my problem is:
for i in range(4):
pygame.draw.rect(screen, (colors[i]), (17 + 80 * i, 265, 50, 50))
pygame.draw.rect(screen, (0,0,0), (17 + 80 * i, 265, 50, 50),5)
if mouse_x > mouse_pos_x_left[i] and mouse_x < mouse_pos_x_right[i]:
if mouse_y > 5.4 and mouse_y < 6.26:
pygame.draw.rect(screen, (255,255,255), (17 + 80 * i, 265, 50, 50), 5)
if event.type == pygame.MOUSEBUTTONDOWN:
food_selected = foods[i]
screen.blit(check, (17 + 80 * i, 210, 50, 50))
if event.type == pygame.MOUSEBUTTONUP:
screen.blit(check, (17 + 80 * i, 210, 50, 50))
if click == 3:
click = 0
all my code:
import pygame
import pdb
import random as r
pygame.init()
screen = pygame.display.set_mode((325,325))
running = True
colors = [(199,203,132), (255,0,0), (0,255,0), (84,87,44)]
mouse_pos_x_left = [0.4, 1.98, 3.62, 5.22]
mouse_pos_x_right = [1.26, 2.86, 4.46, 6.06]
foods = ['bread', 'tomato', 'lettuce', 'ham']
food_selected = ''
customer1 = pygame.image.load('customer1.png')
food_box = pygame.image.load('food_box.png')
check = pygame.image.load('check.png')
same = False
#rand
random_num_1 = r.randint(1,4)
random_num_2 = r.randint(1,4)
randoms = [1,2,3,4]
food_1 = ''
food_2 = ''
click = 0
able = False
"""Bugs:
- when selected same food as customer and clicked on other food it removes checkmark
- when clicked on food shows checkmark for a sec
- when selected same food as customer and then chose another same food as customer first checkmark removes
"""
while running:
same_foods = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((255,255,255))
pos = pygame.mouse.get_pos()
mouse_x = pos[0] / 50
mouse_y = pos[1] / 50
mouse = pygame.mouse.get_pos()
#draw foods:
for i in range(4):
pygame.draw.rect(screen, (colors[i]), (17 + 80 * i, 265, 50, 50))
pygame.draw.rect(screen, (0,0,0), (17 + 80 * i, 265, 50, 50),5)
if mouse_x > mouse_pos_x_left[i] and mouse_x < mouse_pos_x_right[i]:
if mouse_y > 5.4 and mouse_y < 6.26:
pygame.draw.rect(screen, (255,255,255), (17 + 80 * i, 265, 50, 50), 5)
if event.type == pygame.MOUSEBUTTONDOWN:
food_selected = foods[i]
screen.blit(check, (17 + 80 * i, 210, 50, 50))
if event.type == pygame.MOUSEBUTTONUP:
screen.blit(check, (17 + 80 * i, 210, 50, 50))
if click == 3:
click = 0
#customer:
customer1_img = pygame.transform.scale(customer1, (150,150))
food_box_img = pygame.transform.scale(food_box, (100, 125))
screen.blit(customer1_img, (50,50))
screen.blit(food_box_img, (170,0))
#random food:
for i in range(4):
#if random number is == 1
if random_num_1 == randoms[i]:
#then food_1 == to the first food
food_1 = foods[i]
#then draw the first food
pygame.draw.rect(screen, (colors[i]), (195, 5, 50, 50))
if random_num_2 == randoms[i]:
food_2 = foods[i]
pygame.draw.rect(screen, (colors[i]), (195, 60, 50, 50))
if food_1 == food_2:
same_foods = True
if click == 1:
img = pygame.transform.scale(check, (30,30))
screen.blit(img, (285, 10))
if click == 2:
screen.blit(img, (285, 70))
if same_foods == False:
if food_selected == food_1:
same = True
img = pygame.transform.scale(check, (30,30))
screen.blit(img, (285, 10))
else:
same = False
if food_selected == food_2:
same = True
img = pygame.transform.scale(check, (30,30))
screen.blit(img, (285, 70))
else:
same = False
if same:
click = 0
pass
pygame.display.flip()
pygame.quit()