- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在用 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):
第二部分在上面的例子中总是False
。 hgbox
总是掉落,除非条件的第一部分也为 False (不是 hgbox.colliderect(landbox1))
,这意味着它只能站在左侧平台上。
尝试将其更改为:
if not hgbox.colliderect(landbox1) and not hgbox.colliderect(landbox2):
# Move downwards.
编辑:这里有一个完整的示例,向您展示我将如何编写移动和跳跃代码。使用 x_speed
和 y_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/
我曾尝试在 clojure 中使用 mini-kanren 的实现。但是一直在努力理解 cond-a cond-u 和 cond-e 之间的区别。我似乎很清楚 cond-e 但对 cond-a 和 c
我有一个基本上可以实现的功能 (cond ( (cond) (cond) ) ) 目前计划对我来说似乎是一门外语。我的问题很简单。仅当嵌套条件也返回 true 时,如何
我是 Java 的新手,正在尝试学习迭代器的概念。我在 Java 教程 Oracle 中遇到过这段代码。我的问题是,if (!cond(it.next())) 中的 cond 代表什么。文档没有指定
我正在阅读一个名为 Panda.js 的游戏引擎。核心中有一个方法检查浏览器是否支持全屏,但我不明白这段代码: if (this.system.canvas.requestFullscreen)
我试图了解这两个可观察量之间的区别。代码中唯一的区别是: /** * Inside rxjs pipe */ if(typeof x === 'number' && x > 3) { retu
这两个表达式生成的结果似乎相同。它们之间有什么区别吗? 最佳答案 如果您的 cond 条件为真,但您的 value 不是,则第一个表达式将转到 default_value,而您的第二个表达式只要 co
我正在通过解析类似 Excel 的公式来生成 sql 请求。 因此,对于给定的公式,我收到以下请求: SELECT IF(COL1='Y', SUM(EXPR),NULL) FROM Table 我没
当我执行 np.where(temp == True) 时,PyCharm 会发出警告 我的完整代码: from numpy import where, array a = array([[0.468
是否有任何隐藏的微妙之处,一个是首选,还是一个只是编写另一个的更短方式? Client = db.Clients.First(c=>c.Name == "Client 1") 和 Client = d
if user.item.purchase.status == True: ...使用 flake8 检查时产生错误: E712 comparison to True should be 'if co
我在行“added_parts = new_part_set[(new_part_set["duplicate"] == False) & (new_part_set["version"] ==
1. 简介 本文将介绍 Go 语言中的 sync.Cond 并发原语,包括 sync.Cond 的基本使用方法、实现原理、使用注意事项以及常见的使用使用场景。能够更好地理解和应用 Co
为了在 MongoDB 中对元素进行分组和计数,我已经筋疲力尽了。有很多帖子,但没有一个是我需要的。 这是一个基于以下内容的示例: styvane answer db.VIEW_HISTORICO.a
我正在尝试创建一个计算 状态 我的 Mongo 查询中的字段(状态:已创建、已收到付款、已发货、已收到、已完成)。 db.orders.aggregate( [ { $project: { st
在 Clojure 中的 cond 语句内格式化大型子句的推荐方法是什么? 例如: (cond (> (large-function large-arg1 l
我遇到过很多情况,如果满足特定条件,则需要“更新”两个(甚至三个)值的向量,否则就不用管。示例: (let [val1 some-value val2 some-other-value
scala 有与scheme 的 cond 等价的东西吗? 最佳答案 我猜您正在寻找match(或者只是if/else if/else)。 关于scala - Scala语言中的scheme cond
我正在构建一个 RNN 模型,其中 init_state 可能来自两种情况之一。 1) 通过 feed_dict 从之前的时间步长输出状态输入的静态 init_state。 2) 变量的一些函数,我称
根据https://golang.org/pkg/sync/#Cond.Wait ,必须将 Wait() 调用包装在 for 循环中,因为在第一次恢复时,据称 c.L 没有被锁定。这与上面的句子“..
我正在尝试通过 rmongodb 中的操作在组中执行求和。我求和的“列”包含 NaN 值,导致我的总和返回 NaN。在通过命令运行组之前,我想使用 $project 用零替换 NaN,但我被卡住了。
我是一名优秀的程序员,十分优秀!