pygame.KEYDOWN没有任何反应
我正在为一个项目尝试编写一份自定义版本的Blackjack(二十一点),但KEYDOWN命令没有任何响应,游戏也就跑不起来。我也尝试过通过一个函数来执行同样的命令,或者用一个在按键按下时会变化的条件的while循环,但都没能让它跑起来。
请原谅这个乱糟糟的样子,这只是一个高中项目(所以我不是专业人士),我的首要任务是让它能跑起来。代码里可能夹杂着之前尝试的一些片段,所以如果有些东西看起来完全没用,那很可能就是那样。
import pygame
import random
WINDOW_SIZE = [950, 650]
pygame.init()
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("MinusJack")
font1 = pygame.font.Font(None, 25)
font2 = pygame.font.Font(None, 20)
#cards n such
Q = 10
K = 10
J = 10
A = 11
_Q = -10
_K = -10
_J = -10
_A = -11
Deck = [-2,-3,-4,-5,-6,-7,-8,-9,_Q,_K,_J,_A,2,3,4,5,6,7,8,9,Q,K,J,A]
pHand = 0
bHand = 0
runs = ""
running = True
WHITE = ( 255, 255, 255)
RED = ( 200, 0, 0)
#responses
dislose = font1.render("You Lose!", True, RED)
diswin = font1.render("You Win!", True, RED)
distie = font1.render("It's a tie!", True, RED)
disE = font1.render("Easter Egg", True, RED)
dissoon = font1.render("You folded too soon!", True, RED)
disgood = font1.render("Good call", True, RED)
Welcome = font1.render("Welcome to minusjack! Try to get to 21 before the bot does, but beware of the minuscards.",True, WHITE)
blank = ""
instruc = font1.render("For instructions, press i",True, WHITE)
screen.blit(Welcome, [10, 50])
screen.blit(instruc, [10, 100])
gamepl = font1.render("Press ENTER to draw. Press f to fold", True, RED)
screen.blit(gamepl, [10,150])
pygame.display.update()
keys = pygame.key.get_pressed()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if keys[pygame.K_i]:
i1 = font2.render("When Enter is hit, you receive a card. The card can have a positive or negative number. This card is added to your hand.",True, WHITE)
i2 = font2.render("If all cards in your hand add to hand 21, you win!",True, WHITE)
i3 = font2.render("But beware: you play against a bot. If it reaches 21 before you, you lose.",True, WHITE)
i4 = font2.render("The bounds are 40 and -30. Do not let your hand leave these.",True, WHITE)
i5 = font2.render("If it does, you lose. Same goes for the bot.",True, WHITE)
i6 = font2.render(" *Technical things: Q/K/K = 10, A = 11",True, WHITE)
i7 = font2.render("The goal is to try to get as close to 21 as possible. If you think you're closer than the bot, fold.",True, WHITE)
i8 = font2.render("Good luck!",True, RED)
screen.blit(i1, [10, 200])
screen.blit(i2, [10, 220])
screen.blit(i3, [10, 240])
screen.blit(i4, [10, 260])
screen.blit(i5, [10, 280])
screen.blit(i6, [10, 300])
screen.blit(i7, [10, 320])
screen.blit(i8, [10, 340])
elif keys[pygame.K_RETURN]:
#playerdraw
pNewcard = random.choice(Deck)
dispNewcard = font1.render("New card: ", str(pNewcard), True, WHITE)
screen.blit(dispNewcard, [10,100])
#playerhand
pHand = pHand + pNewcard
dispHand = font1.render("Your hand: ", str(pHand), True, WHITE)
screen.blit(dispHand, [50, 100])
#botdraw
bNewcard = random.choice(Deck)
#bothand
bHand = bHand + bNewcard
disbHand = font1.render("Bot hand: ", str(bHand), True, WHITE)
#win/lose
if bHand == 21 and pHand == 21 or pHand == -21 and bHand == -21 or pHand == 21 and bHand == -21 or pHand == -21 and bHand == 21: #happened once
screen.blit(dispHand, [20, 200])
screen.blit(disbHand, [20, 220])
print("..huh?")
break
elif bHand == 21: #b win
screen.blit(dispHand, [20, 200])
screen.blit(disbHand, [20, 220])
screen.blit(dislose, [30, 240])
break
elif pHand == 21: #p win
screen.blit(dispHand, [20, 200])
screen.blit(disbHand, [20, 220])
screen.blit(diswin, [30, 240])
break
elif pHand == -21 or bHand == -21:
screen.blit(dispHand, [20, 200])
screen.blit(disbHand, [20, 220])
screen.blit(disE, [30, 240])
break
elif bHand > 40 and pHand > 40 or bHand > 40 and pHand < -30 or bHand < -30 and pHand > 40 or bHand < -30 and pHand < -30: #exceed
screen.blit(dispHand, [20, 200])
screen.blit(disbHand, [20, 220])
screen.blit(distie, [30, 240])
break
elif bHand > 40 or bHand < -30: #b exceed
screen.blit(dispHand, [20, 200])
screen.blit(disbHand, [20, 220])
screen.blit(diswin, [30, 240])
break
elif pHand > 40 or pHand < -30: #p exceed
screen.blit(dispHand, [20, 200])
screen.blit(disbHand, [20, 220])
screen.blit(dislose, [30, 240])
break
else:
screen.blit(gamepl, [10,150])
elif keys[pygame.K_f]:
screen.blit(dispHand, [20, 200])
screen.blit(disbHand, [20, 220])
if bHand - 21 < pHand - 21 and pHand < 21 and bHand < 21: #<21
screen.blit(disgood, [30, 240])
elif bHand - 21 > pHand - 21 and pHand < 21 and bHand < 21:
screen.blit(dissoon, [30, 240])
elif bHand - 21 > pHand - 21 and pHand > 21 and bHand > 21: #>21
screen.blit(disgood, [30, 240])
elif bHand - 21 < pHand - 21 and pHand > 21 and bHand > 21:
screen.blit(dissoon, [30, 240])
elif ((bHand - 21)*-1) > pHand - 21 and pHand > 21 and bHand < 21: #b<21
screen.blit(disgood, [30, 240])
elif ((bHand - 21)*-1) < pHand - 21 and pHand > 21 and bHand < 21:
screen.blit(dissoon, [30, 240])
elif bHand - 21 > ((pHand - 21)*-1) and pHand < 21 and bHand > 21: #b>21
screen.blit(disgood, [30, 240])
elif bHand - 21 < ((pHand - 21)*-1) and pHand < 21 and bHand > 21:
screen.blit(dissoon, [30, 240])
else:
screen.blit(distie, [30, 240])
else:
runs == ""
pygame.display.update()
pygame.quit()
解决方案
据我所知:.get_pressed() 如果你不先运行 pygame.event.get() 就不起作用(因为 event.get() 会更新在 .get_pressed() 中使用的特殊数组)。
如果你想获得关于按键的实际信息,那么必须在 while-循环中进行——它会更新这个数组。
但是如果把它放在 if event.type == pygame.KEYDOWN: 中运行,那么 if event.key == pygame.K_i: 就更有意义。
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_i:
# code
elif event.key == pygame.K_RETURN:
# code
elif event.key == pygame.K_f:
# code
顺便说一句,.get_pressed() 在 for event-循环之后更有用,当你想要一直检查按键是否处于按下状态时——它可以一遍又一遍重复同样的代码,直到你松开按键(例如按住箭头键来移动对象)。当你只是想执行某段代码一次时,它就没用了。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。