首页 > 解决方案 > 如何阻止我的 if 语句运行以减少 pygame 中的变量一

问题描述

嗨,我有一个动画,我希望它在我单击错误的按钮时显示,但我希望健康减少 10 并且停止不减少,直到数百万我可以做些什么来停止我试图做的 if 语句,food_selected_1 = food_1但那不是我想要什么,我想要food_selected_1 = ''什么,这就是我能做些什么来阻止它应该工作的 if 语句,但不是???我的问题是:

    elif food_selected_1 != '' and food_selected_1 != food_1:
        health_decreased = True
        health -= 10
        print(food_selected_1)

我所有的代码:

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_1 = False
same_2 = 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]
change = False
bar = 115
bar_available = True
health = 100
img = pygame.transform.scale(check, (30,30))
time = 200
click = False
timer = 0
y = 10
pos = []
health_decreased = False
"""To work on:
- GAME OVER screen
- time begining to get faster
- after we select all foods and same as customer change the types of foods
"""
"""Working on:
- after we select all foods and same as customer change the types of foods
"""
while running:
    same_foods = False
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            click = True
        if event.type == pygame.MOUSEBUTTONUP:
            click = False
        pass
    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:
    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))
    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 on the customer
            pygame.draw.rect(screen, (colors[i]), (180, 5, 50, 50))
        if random_num_2 == randoms[i]:
            food_2 = foods[i]
            pygame.draw.rect(screen, (colors[i]), (180, 60, 50, 50))
        if bar_available:
            if food_1 == food_2:
                if food_selected_1 == food_1:
                    same_foods = True
                    if click_1 == 1:
                        same_1 = True
                        screen.blit(img, (230, 10))
                if food_selected_2 == food_2:
                    if click_2 == 1:
                        same_2 = True
                        screen.blit(img, (230, 70))
            if same_foods == False:
                if food_selected_1 == food_1:
                    same_1 = True
                    screen.blit(img, (230, 10))
                else:
                    same_1 = False
                if food_selected_2 == food_2:
                    same_2 = True
                    screen.blit(img, (230, 70))
                else:
                    same_2 = False
                if same_1 or same_2:
                    click_1 = 0
                    click_2 = 0
        #random food:
    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 click:
                    #if the food_selected is not the same as food_1 keep changing
                    if food_selected_1 != food_1:
                        #when it is == to food_1 stay whatever it is on
                        food_selected_1 = foods[i]
                    if food_selected_2 != food_2:
                        food_selected_2 = foods[i]
                    #clicked[i] is either the first image or second and...
                    clicked[i] = True
                    if food_selected_1 == food_1:
                        click_1 += 1
                        click = False
        if click_1 == 2:
            click_2 = 1
            click_1 = 1
        if same_1 and same_2:
            timer += 2 / time
            if timer >= 3:
                bar_available = False
            pass
        if bar_available:
            if clicked[i]:
                screen.blit(check, (17 + 80 * i, 210, 50, 50))
        pass
    pass
    bar -= 10 / time
    if bar <= 0 and bar >= -1:
        if food_selected_1 != food_1:
            if food_selected_2 != food_2:
                bar_available = False
                health -= 10
                bar = -1
    if food_selected_1 != food_1 and food_selected_1 == '':
        health = health
    elif food_selected_1 != '' and food_selected_1 != food_1:
        health_decreased = True
        health -= 10
        print(food_selected_1)
    if bar <= -1:
        health_decreased = True
    if bar_available != True:
        bar = -1
    pygame.draw.rect(screen, (0,255,0), (265, 2, 25, bar))
    pygame.draw.rect(screen, (0,0,0), (265, 2, 25, 115), 5)

    health_text = '%d'% health
    pygame.draw.rect(screen, (200,0,0), (5, 5, health, 25))

    font = pygame.font.Font('OpenSans-Regular.ttf', 20)
    health_display = font.render(health_text, True, (0,0,0))
    pygame.draw.rect(screen, (0,0,0), (5, 5, 100, 25), 5)
    screen.blit(health_display, (110, 2))
    if bar_available != True:
        if health_decreased == True:
            if food_selected_1 != food_1:
                if food_selected_2 != food_2:
                    y -= 0.2 / 8
                    health_decreased_display = font.render('-10', True, (255,0,0))
                    screen.blit(health_decreased_display, (142, y))
                    if y <= -25:
                        y = -25
    pygame.display.flip()
pygame.quit()

标签: pythonpygame

解决方案


推荐阅读