pygame求助第二段循环中无法识别键盘按下(急,赶ddl中)
import pygame,syspygame.init()
desk_width = pygame.display.get_desktop_sizes()
desk_height = pygame.display.get_desktop_sizes()
WIN_WIDTH=1440
WIN_HEIGHT=800
window=pygame.display.set_mode((desk_width,desk_height))
pygame.display.set_caption('The Cemetroy')
window.fill((0,0,0))
FPS=60
image_start=r'C:/Users/26896/Desktop/THE CEMETERY/start.jpg'
image_game0=r"C:\Users\26896\Desktop\THE CEMETERY\Cemetery-dusk.png"
image_game1=r"C:\Users\26896\Desktop\THE CEMETERY\Cemetery-dusk(2).png"
textbottom=r"C:/Users/26896/Desktop/THE CEMETERY/HYShangWeiHuangYeW.ttf"
elder_left = pygame.image.load(r"C:\Users\26896\Desktop\THE CEMETERY\elder_left.png", )
elder_right = pygame.image.load(r"C:\Users\26896\Desktop\THE CEMETERY\elder_right.png")
def textbox( content,rfiles, color, size, x,y):
font=pygame.font.Font(rfiles,size)
text=font.render('content',True,color)
window.blit(text,(x,y))
pygame.display.update()
def imagebox(rfiles,angle=0,scale=1):
image=pygame.image.load(rfiles)
w,h=image.get_size()
window.blit(image,(desk_width/2-w/2,desk_height/2-h/2))
pygame.display.flip()
image=pygame.transform.rotozoom(image,angle,scale)
window.blit(image,(desk_width/2-w/2,desk_height/2-h/2))
pygame.display.update()
#按钮
def bottombox(text_rfiles,text_size,text_color,text_content,rectangle_color,rx,ry,rw,rh,ractangles_width=0,border_radius=20,border_top_left_radius=-1,border_top_right_radius=-1,border_bottom_left_radius=-1,border_bottom_right_radius=-1,rectangle_color_beneath=(50,35,35)):
#中心坐标
font=pygame.font.Font(text_rfiles,text_size)
pygame.draw.rect(window, rectangle_color_beneath, (rx-rw*3/5,ry-rh*5/8,rw*6/5,rh*5/4),ractangles_width,border_radius,border_top_left_radius,border_top_right_radius,border_bottom_left_radius,border_bottom_right_radius)
pygame.draw.rect(window, rectangle_color, (rx-rw/2,ry-rh/2,rw,rh),ractangles_width,border_radius,border_top_left_radius,border_top_right_radius,border_bottom_left_radius,border_bottom_right_radius)
text=font.render(text_content,True,text_color)
tw,th = text.get_size()
window.blit(text,(rx-tw/2, ry-th/2))
pygame.display.update()
for event in pygame.event.get():
#按下按钮动画
def mousebottomdown(rx,ry,rw,rh,text_rfiles,text_size,text_color,text_content,rectangle_color,rectangle_width=0,border_radius=20,border_top_left_radius=-1,border_top_right_radius=-1,border_bottom_left_radius=-1,border_bottom_right_radius=-1):
pygame.draw.rect(window,(100,100,100),(rx-rw/2,ry-rh/2,rw,rh),0,border_radius,border_top_left_radius,border_top_right_radius,border_bottom_left_radius,border_bottom_right_radius)
font=pygame.font.Font(text_rfiles,text_size)
text=font.render(text_content,True,(255,0,0))
tw,th = text.get_size()
window.blit(text,(rx-tw/2, ry-th/2))
pygame.display.update()
pygame.time.wait(300)
def mousebottomup(rx,ry,rw,rh,text_rfiles,text_size,text_color,text_content,rectangle_color,rectangle_width=0,border_radius=20,border_top_left_radius=-1,border_top_right_radius=-1,border_bottom_left_radius=-1,border_bottom_right_radius=-1):
pygame.draw.rect(window,rectangle_color,(rx-rw/2,ry-rh/2,rw,rh),0,border_radius,border_top_left_radius,border_top_right_radius,border_bottom_left_radius,border_bottom_right_radius)
font=pygame.font.Font(text_rfiles,text_size)
text=font.render(text_content,True,text_color)
tw,th = text.get_size()
window.blit(text,(rx-tw/2, ry-th/2))
pygame.display.update()
print("succeed")
pygame.time.wait(300)
#开始页
imagebox(image_start)
#开始,退出选项
bottombox(textbottom,70,(200,235,235),'start',(100,65,65),desk_width/2,desk_height/2,170,80)
bottombox(textbottom,70,(200,235,235),'exit',(100,65,65),desk_width/2,desk_height/2+WIN_HEIGHT/4,170,80)
def main():
clock=pygame.time.Clock()
#game loop
start = True
game = False
end = False
#print("start=",start)
while start:
clock.tick(FPS)
events=pygame.event.get()
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN :
mx,my = event.pos
if (desk_width/2-170/2<=mx<=desk_width/2+170/2 and desk_height/2-80/2<=my<=desk_height/2+80/2):
#print("1")
start=False
game=True
mousebottomdown(desk_width/2,desk_height/2,170,80,textbottom,70,(200,235,235),'start',(100,65,65))
#print("down1")
mousebottomup(desk_width/2,desk_height/2,170,80,textbottom,70,(200,235,235),'start',(100,65,65))
if (desk_width / 2 - 170 / 2 <= mx <= desk_width / 2 + 170 / 2 and desk_height / 2 + WIN_HEIGHT / 4 - 80 / 2 <= my <= desk_height / 2 + WIN_HEIGHT / 4 + 80 / 2):
mousebottomdown(desk_width / 2, desk_height / 2 + WIN_HEIGHT / 4, 170, 80, textbottom, 70,(200, 235, 235), 'exit', (100, 65, 65))
start = False
#print("2")
mousebottomup(desk_width/2,desk_height/2+WIN_HEIGHT/4,170,80,textbottom,70,(200,235,235),'exit',(100,65,65))
return
# goto.loop2
if event.type == pygame.MOUSEBUTTONUP :
#print("up")
pass
n=0
is_move = False
x_speed = 0
y_speed = 0
window.fill((0,0,0))
imagebox(image_game0)
pygame.display.update()
print("game",game)
while game:
pygame.event.pump()
clock.tick(FPS)
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
if n==0:
pygame.time.wait(1000)
imagebox(image_game1)
n=1
# 放置角色
elder = elder_right
elder_x, elder_y = desk_width/2-WIN_WIDTH/2+150, desk_height/2-WIN_HEIGHT/2+150
window.blit(elder, (elder_x, elder_y))
pygame.display.update()
else :pass
//以上运行都没问题
if event.type == pygame.KEYDOWN:
print("3",event) //从这里无法输出
if event.type == pygame.KEYDOWN:
char=chr(event.key)
print(char)
if char == 'w' and desk_width/2-WIN_WIDTH/2+555<=elder_x<=desk_width/2-WIN_WIDTH/2+565 and elder_y<=desk_height/2-WIN_HEIGHT/2+570:
is_move=True
imagebox(image_game1)
x_speed = 0
y_speed = -5
elder_x+=x_speed
elder_y+=y_speed
pygame.time.wait(300)
window.blit(elder,(elder_x,elder_y))
elif char == 'a' and (desk_width/2-WIN_WIDTH/2+100<=elder_x and (desk_height/2-WIN_HEIGHT/2+165<=elder_y<=desk_height/2-WIN_HEIGHT/2+175 or desk_height/2-WIN_HEIGHT/2+510<=elder_y<=desk_height/2-WIN_HEIGHT/2+570)) or (desk_width/2-WIN_WIDTH/2+555<=elder_x and desk_height/2-WIN_HEIGHT/2+165<=elder_y<=desk_height/2-WIN_HEIGHT/2+570):
is_move=True
imagebox(image_game1)
x_speed=-5
y_speed=0
elder_x += x_speed
elder_y += y_speed
elder=elder_left
window.blit(elder,(elder_x,elder_y))
elif char == 's' and desk_width/2-WIN_WIDTH/2+555<=elder_x<=desk_width/2-WIN_WIDTH/2+565 and desk_height/2-WIN_HEIGHT/2+170<=elder_y:
is_move=True
imagebox(image_game1)
x_speed=0
y_speed=5
elder_x += x_speed
elder_y += y_speed
window.blit(elder,(elder_x,elder_y))
elif char == 'd' and (elder_x<=desk_width/2+WIN_WIDTH/2-100 and (desk_height/2-WIN_HEIGHT/2+165<=elder_y<=desk_height/2-WIN_HEIGHT/2+175 or desk_height/2-WIN_HEIGHT/2+510<=elder_y<=desk_height/2-WIN_HEIGHT/2+570)) or (elder_x<=desk_width/2-WIN_WIDTH/2+565 and desk_height/2-WIN_HEIGHT/2+165<=elder_y<=desk_height/2-WIN_HEIGHT/2+570):
is_move=True
imagebox(image_game1)
x_speed=5
y_speed=0
elder_x += x_speed
elder_y += y_speed
elder=elder_right
window.blit(elder,(elder_x,elder_y))
pygame.display.update()
#键盘弹起
elif event.type == pygame.KEYUP:
is_move=False
pygame.display.update()
elif event.type == pygame.QUIT:
pygame.quit()
exit()
return
if __name__=="__main__":
main()
pygame.quit()
sys.exit()
各位大佬,救救{:10_266:}
万分感谢 第151行{:10_266:} 解决了uu们,果然还是得靠活人
问题出在系统默认键盘事件类型为TEXTEDITING,加上pygame.key.stop_text_input()就好了
给有相同问题的uu指路原文:
(csdn)pygame获取键盘输入失败{:10_279:} 你的问题是在第二个循环中,pygame无法识别键盘按下的事件。这个问题可能是由于你在处理事件的时候,没有正确地获取和处理事件。
在你的代码中,你在主循环while game:中使用了for event in events:,但是你没有在这个循环中更新events。这意味着你在处理的是在进入while game:循环时获取的事件,而不是在每个循环迭代中获取的新事件。
你应该在while game:循环的开始处添加events = pygame.event.get(),这样你就可以在每次迭代中获取新的事件。这样修改后,你的代码应该是这样的:
while game:
events = pygame.event.get()# 获取新的事件
pygame.event.pump()
clock.tick(FPS)
for event in events:
# 处理事件的代码
这样,你的程序就应该能够正确地检测到键盘按下的事件了。希望这个答案能够帮助你解决问题!如果你还有其他问题,欢迎继续提问。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案 问题出在你的代码中的事件处理部分。你只在开始的时候获取了一次事件列表,然后在循环中重复使用这个事件列表。这样做是不正确的,因为在每次循环中,你需要获取最新的事件列表。
你需要将获取事件列表的代码放在循环内部,像这样:
while start:
clock.tick(FPS)
events = pygame.event.get()
for event in events:
# 处理事件的代码
这样,每次循环都会获取最新的事件列表,并对每个事件进行处理。
另外,你的代码还有一些其他问题,比如没有正确地缩进和定义函数时没有使用正确的缩进。请确保你的代码缩进正确,以避免出现语法错误。
希望这些解释对你有帮助!如果你还有其他问题,请随时提问。 本帖最后由 mothvv 于 2023-10-20 17:39 编辑
谢谢,但是加上之后还是没有执行print("3",event)
并且更无法通过键盘移动人物{:10_291:} {:10_266:} 找到一个小bug,修复了可允许移动范围,但是还是无法移动人物{:10_266:} 求活人,ai靠不住{:10_266:} 如果你的程序在第二段循环中无法识别键盘按下的事件,可能是因为你的代码没有正确地处理键盘事件。在很多编程语言和框架中,你需要不断地监听键盘事件,并在检测到按键后执行相应的操作。
以下是一个使用Python和Pygame库处理键盘事件的示例代码:
python
import pygame
import sys
pygame.init()
# 设置窗口尺寸
window_width = 800
window_height = 600
# 创建窗口
window = pygame.display.set_mode((window_width, window_height))
# 设置窗口标题
pygame.display.set_caption('键盘按下示例')
# 循环检测键盘事件
while True:
# 检查是否退出游戏
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 检查键盘按下事件
keys = pygame.key.get_pressed()
if keys:# 如果按下空格键
print("空格键被按下")
# 在这里执行你想要的操作
# 更新窗口
pygame.display.flip()
在这个示例代码中,我们使用pygame.key.get_pressed()函数来获取键盘的按下状态。这个函数返回一个元组,其中每个元素代表一个按键的状态,如果该键被按下,则对应的元素为True,否则为False。我们可以通过检查keys来检测空格键是否被按下,如果为True,则执行相应的操作。
请注意,这只是一个简单的示例代码,你需要根据你的实际需求来修改和扩展它。如果你使用的是其他编程语言或框架,你需要查找相应的键盘事件处理方法。
来个活人,我给你设为最佳答案,不想给ai
页:
[1]