gpt4 book ai didi

python - "if cond1 or cond2"语句未在 Python 中运行第二个条件

转载 作者:太空宇宙 更新时间:2023-11-04 10:00:43 25 4
gpt4 key购买 nike

我正在用 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 语句的其他部分?

最佳答案

对于 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 总是掉落,除非条件的第一部分也为 False (不是 hgbox.colliderect(landbox1)),这意味着它只能站在左侧平台上。

尝试将其更改为:

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

编辑:这里有一个完整的示例,向您展示我将如何编写移动和跳跃代码。使用 x_speedy_speed 每帧移动玩家(同时加速 y_speed),并在事件循环中将速度设置为所需值.如果玩家触摸平台,将他设置为平台矩形的 .top 并将 hgjumpallowed 设置为 True

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) 或 (K_d) 并且 K_d 是一个真值。

关于python - "if cond1 or cond2"语句未在 Python 中运行第二个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43741238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com