无法判断我的变量是否为真,然后在pygame中使其为真?



当我在游戏中选择一种食物时,它应该查看我选择的食物是否与顾客订购的食物相似。如果是相同的,我希望它保持为客户订购的东西,因为我要显示一个图像,并且只有当两者相同时才显示图像。在我选择另一种食物后,我显示的图像被删除,因为变量food_selected现在与顾客订购的不一样。让我给你一个更清楚的解释:


当我打印:
顾客食物:西红柿,生菜
玩家摘了什么:西红柿,西红柿(它把两个西红柿都放了,然后我还需要摘生菜)

顾客的食物:西红柿,生菜
玩家摘了什么:生菜,生菜(现在它改变了它们)

我想要什么:
(当他挑选顾客点的第一种食物时)
顾客的食物:西红柿,生菜
玩家挑选的:西红柿,西红柿

(当他挑选顾客点的第二种食物时)
顾客的食物:西红柿,生菜
玩家挑选的:西红柿,生菜

问题发生的地方:

if food_selected_1 == food_1:
same = True
img = pygame.transform.scale(check, (30,30))
screen.blit(img, (285, 10))
else:
same = False
if food_selected_2 == food_2:
same = True
img = pygame.transform.scale(check, (30,30))
screen.blit(img, (285, 70))
else:
same = False
if same:
click_1 = 0
click_2 = 0

我所有的代码:

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_1 = ''
food_selected_2 = ''
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_1 = 0
click_2 = 0
able = False
clicked = [False,False,False,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:
click_1 += 1
food_selected_1 = foods[i]
food_selected_2 = foods[i]
#clicked[i] is either the first image or second and...
clicked[i] = True
if clicked[i]:
screen.blit(check, (17 + 80 * i, 210, 50, 50))  
if click_1 == 2:
click_2 = 1
click_1 = 1
if click_1 == 2:
click_2 = 1
#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 == 1:
img = pygame.transform.scale(check, (30,30))
screen.blit(img, (285, 10))
if click_2 == 1:
screen.blit(img, (285, 70))
if same_foods == False:
if food_selected_1 == food_1:
same = True
img = pygame.transform.scale(check, (30,30))
screen.blit(img, (285, 10))
else:
same = False
if food_selected_2 == food_2:
same = True
img = pygame.transform.scale(check, (30,30))
screen.blit(img, (285, 70))
else:
same = False
if same:
click_1 = 0
click_2 = 0
print(food_selected_1, food_1)
print(food_selected_2, food_2)
pygame.display.flip()
pass
pygame.quit()

我不明白你在说什么,但我从你写的代码中猜测,当food_1food_2与所选的相同时,你希望same仅为True

你需要这样写:

if food_selected_1 == food_1:
same_1 = True
img = pygame.transform.scale(check, (30,30))
screen.blit(img, (285, 10))
else:
same_1 = False
if food_selected_2 == food_2:
same_2 = True
img = pygame.transform.scale(check, (30,30))
screen.blit(img, (285, 70))
else:
same_2 = False
if same_1 and same_2:
click_1 = 0
click_2 = 0

最新更新