“如果cond1或cond2”语句没有在Python中运行第二个条件

时间:2017-05-02 15:02:23

标签: python if-statement pygame

到目前为止,我正在Python制作一个看起来像这样的游戏:

import pygame, sys, time, random, threading
from threading import Timer
from pygame.locals import *
pygame.init()
WINDOWHEIGHT = 720
WINDOWWIDTH = 1280
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Hitman Grandma')
plorp = 'true'
white = (255,255,255)
red = (255,0,0)
black = (0,0,0)
green = (0,255,0)
blue = (0,0,255)
cyan = (0,255,255)
windowSurface.fill(white)
pygame.display.update()
mainClock = pygame.time.Clock()
hgleft = False
hgright = False
hgup = False
speed = 4
hgair = True
hgjumpallowed = False
level = 0
def stop() :
    hgbox.move_ip(0,0)
    return
hgbox = pygame.Rect(0 ,13 ,36 ,72)
hitmangrandma = pygame.image.load('hgrd1copy.jpg')
hg = pygame.transform.scale(hitmangrandma, (36,72))
landbox1 = pygame.Rect(0,400,200,50)
li = pygame.image.load('hgland1.png')
land1 = pygame.transform.scale(li,(200,50))
landbox2 = pygame.Rect(230,400,200,50)
land2 = pygame.transform.scale(li,(200,50))
land = landbox1,landbox2
while True:
    windowSurface.fill(white)
    windowSurface.blit(land1,landbox1)
    windowSurface.blit(land2,landbox2)
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_LEFT or event.key == K_a:
                hgright = False
                hgleft = True
            if event.key == K_RIGHT or event.key == K_d:
                hgleft = False
                hgright = True
            if event.key == K_UP or event.key == K_w:
                hgair = True
                hgup = True
                hgupkey = True
        if event.type == KEYUP:
            if event.key == K_ESCAPE or K_q and pygame.key.get_mods() & pygame.KMOD_CTRL:
                pygame.quit()
                sys.exit()
                exit
            if event.key == K_LEFT or K_a:
                hgleft = False
            if event.key == K_RIGHT or K_d:
                hgright = False
    if hgup and hgbox.top > 0 and hgupkey == True and hgair == True and hgjumpallowed == True:
        hgbox.top -= 100
        hgair = True
    if hgleft and hgbox.left > 0:
        hgbox.left -= speed
    if hgright and hgbox.right < WINDOWWIDTH:
        hgbox.right += speed
    if not hgbox.colliderect(landbox1) or not hgbox.colliderect(landbox2) and hgair == True:
        hgair = False
        hgbox.top += speed
        hgjumpallowed = False
    if hgbox.colliderect(landbox1) or hgbox.colliderect(landbox2):
        hgjumpallowed = True
        stop()
    windowSurface.blit(hg, hgbox)
    pygame.display.update()
    mainClock.tick(40)

然而,当我运行我的脚本时,hgbox没有检测到与landbox2的碰撞,只是一直在下降。我认为这个问题是由于它只运行if语句的第一部分,而不是检查其他部分。我应该怎么做才能检测到if语句的其他部分?

2 个答案:

答案 0 :(得分:2)

要确保Python以正确的顺序评估逻辑运算符,请添加()。

if (not hgbox.colliderect(landbox1) or not hgbox.colliderect(landbox2)) and hgair == True:

如果与landbox1landbox2以及hgair == True没有发生冲突,则评估为True。

答案 1 :(得分:2)

使用mcve我的意思是这样的(我们可以复制,粘贴和运行的最小但完整的例子):

import sys
import pygame


pygame.init()

screen = pygame.display.set_mode((640, 480))

def stop() :
    hgbox.move_ip(0, 0)

hgbox = pygame.Rect(0 ,13 ,36 ,72)
landbox1 = pygame.Rect(0,400,200,50)
landbox2 = pygame.Rect(230,400,200,50)
hgair = True
hgjumpallowed = False
hgup = False
hgupkey = False
speed = 9

clock = pygame.time.Clock()
done = False

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                hgup = True
                hgbox.x += 50
                hgbox.y -= 90

    if hgup and hgbox.top > 0 and hgupkey == True and hgair == True and hgjumpallowed == True:
        hgbox.top -= 100
        hgair = True
    if not hgbox.colliderect(landbox1) or not hgbox.colliderect(landbox2) and hgair == True:
        hgair = False
        hgbox.top += speed
        hgjumpallowed = False
    if hgbox.colliderect(landbox1) or hgbox.colliderect(landbox2):
        hgjumpallowed = True
        stop()

    screen.fill(pygame.Color('gray12'))
    pygame.draw.rect(screen, (120, 70, 70), landbox1)
    pygame.draw.rect(screen, (120, 70, 70), landbox2)
    pygame.draw.rect(screen, (50, 70, 170), hgbox)

    pygame.display.flip()
    clock.tick(30)

pygame.quit()
sys.exit()

问题是由这一行引起的:

if not hgbox.colliderect(landbox1) or not hgbox.colliderect(landbox2) and hgair == True:

评估为

if (not hgbox.colliderect(landbox1)) or (not hgbox.colliderect(landbox2) and hgair == True):

在上面的示例中,第二部分始终为Falsehgbox总是会掉落,除非条件的第一部分也为假(not hgbox.colliderect(landbox1)),这意味着它只能站在左侧平台上。

尝试将其更改为:

if not hgbox.colliderect(landbox1) and not hgbox.colliderect(landbox2):
    # Move downwards.

编辑:这是一个完整的示例,向您展示如何编写移动和跳转代码。使用x_speedy_speed每帧移动播放器(也加速y_speed),在事件循环中将速度设置为所需的值。如果玩家触摸平台,则将其设置为平台rect的.tophgjumpallowedTrue

import pygame, sys
from pygame.locals import *
from pygame.color import THECOLORS


pygame.init()

windowSurface = pygame.display.set_mode((1280, 720), 0, 32)

pygame.display.update()
mainClock = pygame.time.Clock()

hitmangrandma = pygame.Surface((36, 72))
hitmangrandma.fill((250, 160, 50))
hgbox = hitmangrandma.get_rect(topleft=(10, 10))

y_speed = 0
x_speed = 0
hgjumpallowed = False

land_img = pygame.Surface((200, 50))
land_img.fill((50, 100, 250))
land = pygame.Rect(0,400,200,50), pygame.Rect(230,400,200,50)

done = False

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:  # Quit game by pressing on the "x" button.
            done = True
        if event.type == KEYDOWN:
            if event.key in (K_LEFT, K_a):
                # Just set the x_speed and then move
                # the rect in the while loop each frame.
                x_speed = -4
            if event.key in (K_RIGHT, K_d):
                x_speed = 4
            if (event.key == K_UP or event.key == K_w) and hgjumpallowed:
                y_speed = -17
                hgjumpallowed = False
        if event.type == KEYUP:
            if event.key == K_ESCAPE or K_q and pygame.key.get_mods() & pygame.KMOD_CTRL:
                done = True
            if event.key in (K_LEFT, K_a):
                x_speed = 0
            if event.key in (K_RIGHT, K_d):
                x_speed = 0

    y_speed += 1  # Accelerate downwards.
    # Move the player.
    hgbox.x += x_speed
    hgbox.y += y_speed
    # Check if player is on ground and can jump.
    hgjumpallowed = False
    for box in land:
        if hgbox.colliderect(box):  # If player touches ground.
            hgjumpallowed = True
            hgbox.bottom = box.top
            y_speed = 0

    windowSurface.fill(THECOLORS['white'])
    for box in land:
        windowSurface.blit(land_img, box)
    windowSurface.blit(hitmangrandma, hgbox)
    pygame.display.update()
    mainClock.tick(40)


pygame.quit()
sys.exit()

事件循环中也存在错误:event.key == K_RIGHT or K_d始终为True,因为它被评估为(event.key == K_RIGHT) or (K_d)K_d是真值。