Python 如何在pygame中创建交互式对象?
在我正在创建的pygame程序中,我需要屏幕上的一个交互对象,当玩家角色移动到该对象上并按下enter键时,该对象将调用一个函数。以下是我目前掌握的代码:Python 如何在pygame中创建交互式对象?,python,pygame,Python,Pygame,在我正在创建的pygame程序中,我需要屏幕上的一个交互对象,当玩家角色移动到该对象上并按下enter键时,该对象将调用一个函数。以下是我目前掌握的代码: import pygame pygame.init() screen = pygame.display.set_mode((800, 600)) done = False x = 30 y = 30 clock = pygame.time.Clock() while not done: for event in pyga
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
done = False
x = 30
y = 30
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP] and y > 0: y -= 5
if pressed[pygame.K_DOWN] and y < 600 - 60: y += 5
if pressed[pygame.K_LEFT] and x > 0: x -= 5
if pressed[pygame.K_RIGHT] and x < 800 - 60: x += 5
screen.fill((0, 0, 0))
color = (0, 128, 255)
pygame.draw.rect(screen, color, pygame.Rect(x, y, 60, 60))
myfont = pygame.font.SysFont("monospace", 15)
label = myfont.render("Start the experiment simulator", 1, (255,255,255))
screen.blit(label, (100, 100))
label2 = myfont.render("Start the quiz", 1, (255,255,255))
screen.blit(label2, (550, 100))
label3 = myfont.render("Quit game", 1, (255,255,255))
screen.blit(label3, (350, 400))
pygame.draw.rect(screen, red, pygame.Rect(600, 125, 30, 30))
pygame.draw.rect(screen, red, pygame.Rect(225, 125, 30, 30))
pygame.draw.rect(screen, red, pygame.Rect(375, 425, 30, 30))
pygame.display.flip()
clock.tick(60)
导入pygame
pygame.init()
screen=pygame.display.set_模式((800600))
完成=错误
x=30
y=30
clock=pygame.time.clock()
虽然没有这样做:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
pressed=pygame.key.get_pressed()
如果按下[pygame.K_UP]且y>0:y-=5
如果按下[pygame.K_DOWN]且y<600-60:y+=5
如果按下[pygame.K_LEFT]且x>0:x-=5
如果按下[pygame.K_RIGHT]且x<800-60:x+=5
屏幕填充((0,0,0))
颜色=(0、128、255)
pygame.draw.rect(屏幕,颜色,pygame.rect(x,y,60,60))
myfont=pygame.font.SysFont(“monospace”,15)
label=myfont.render(“启动实验模拟器”,1,(255255))
屏幕blit(标签,(100100))
label2=myfont.render(“开始测验”,1,(255255))
屏幕blit(标签2,(550100))
label3=myfont.render(“退出游戏”,1,(255255))
屏幕blit(label3,(350400))
pygame.draw.rect(屏幕,红色,pygame.rect(600,125,30,30))
pygame.draw.rect(屏幕,红色,pygame.rect(225,125,30,30))
pygame.draw.rect(屏幕,红色,pygame.rect(375,425,30,30))
pygame.display.flip()
时钟滴答(60)
目前,该对象只是一个测试,因此最好是一个小的红色矩形,大约是播放器大小的一半,稍后我可以用图标替换。此矩形应放置在pygame窗口上的“退出游戏”标签下方,并在与交互时退出游戏。这是我迄今为止尝试过的一种方法:
if pressed[pygame.K_RETURN] and x >= 375 or x <= 405 and y >=425 or y <= 455:
pygame.display.quit()
pygame.quit()
sys.exit()
如果按下[pygame.K_RETURN]并且x>=375或x=425或y回答我自己的问题,我通过在本节中的x和y检查周围添加括号,成功地进行了第一次尝试:
and x >= 375 or x <= 405 and y >=425 or y <= 455:
和x>=375或x=425或y=375和x=425和y回答我自己的问题,我通过在本节中的x和y检查周围添加括号,成功地进行了第一次尝试:
and x >= 375 or x <= 405 and y >=425 or y <= 455:
和x>=375或x=425或y=375和x=425和y您可以使用pygame的colliderect检查碰撞
首先,创建三个表示三个选项矩形的矩形:
simulator_rect = pygame.Rect(600, 125, 30, 30)
quiz_rect = pygame.Rect(225, 125, 30, 30)
quit_rect = pygame.Rect(375, 425, 30, 30)
接下来,我们将创建一个表示蓝色选择器rect的rect:
selector_rect = pygame.Rect(50, 50, 60, 60)
所以现在你有了只创建一次的矩形,而不是每次都创建的未命名矩形
现在,对于实际碰撞检测:
# Check to see if the user presses the enter key
if pressed[pygame.K_RETURN]:
# Check to see if the selection rect
# collides with any other rect
for rect in option_rects:
if selector_rect.colliderect(rect):
if rect == simulator_rect:
# Do simulations stuff!
print('Simulating!')
elif rect == quiz_rect:
# Do quizzing stuff!
print('Quizzing!')
elif rect == quit_rect:
# Quit!
done = True
最终代码:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
done = False
x = 30
y = 30
clock = pygame.time.Clock()
# RGB values for red
red = (255, 0 ,0)
# Your three button rects :
simulator_rect = pygame.Rect(225, 125, 30, 30)
quiz_rect = pygame.Rect(600, 125, 30, 30)
quit_rect = pygame.Rect(375, 425, 30, 30)
# These represent your three option rects
option_rects = [simulator_rect, quiz_rect, quit_rect]
# Your blue selector rect
selector_rect = pygame.Rect(50, 50, 60, 60)
# The 50, 50 xy coords are temporary
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP] and y > 0: y -= 5
if pressed[pygame.K_DOWN] and y < 600 - 60: y += 5
if pressed[pygame.K_LEFT] and x > 0: x -= 5
if pressed[pygame.K_RIGHT] and x < 800 - 60: x += 5
# Set the slector rect's coords to x/y
selector_rect.x, selector_rect.y = x, y
screen.fill((0, 0, 0))
color = (0, 128, 255)
pygame.draw.rect(screen, color, selector_rect)
myfont = pygame.font.SysFont("monospace", 15)
label = myfont.render("Start the experiment simulator", 1, (255,255,255))
screen.blit(label, (100, 100))
label2 = myfont.render("Start the quiz", 1, (255,255,255))
screen.blit(label2, (550, 100))
label3 = myfont.render("Quit game", 1, (255,255,255))
screen.blit(label3, (350, 400))
# Use our created rects
pygame.draw.rect(screen, red, simulator_rect)
pygame.draw.rect(screen, red, quiz_rect)
pygame.draw.rect(screen, red, quit_rect)
# Check to see if the user presses the enter key
if pressed[pygame.K_RETURN]:
# Check to see if the selection rect
# collides with any other rect
for rect in option_rects:
# Add rects as needed
if selector_rect.colliderect(rect):
if rect == simulator_rect:
# Do simulations stuff!
print('Simulating!')
elif rect == quiz_rect:
# Do quizzing stuff!
print('Quizzing!')
elif rect == quit_rect:
# Quit!
done = True
pygame.display.flip()
clock.tick(60)
导入pygame
pygame.init()
screen=pygame.display.set_模式((800600))
完成=错误
x=30
y=30
clock=pygame.time.clock()
#红色的RGB值
红色=(255,0,0)
#您的三个按钮矩形:
模拟器_rect=pygame.rect(225、125、30、30)
quick_rect=pygame.rect(600、125、30、30)
quit_rect=pygame.rect(375425,30,30)
#这些代表三个选项矩形
选项\-rects=[模拟器\-rect,测验\-rect,退出\-rect]
#你的蓝色选择器矩形
选择器_rect=pygame.rect(50,50,60,60)
#50,50 xy坐标系为临时坐标系
虽然没有这样做:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
pressed=pygame.key.get_pressed()
如果按下[pygame.K_UP]且y>0:y-=5
如果按下[pygame.K_DOWN]且y<600-60:y+=5
如果按下[pygame.K_LEFT]且x>0:x-=5
如果按下[pygame.K_RIGHT]且x<800-60:x+=5
#将选择器的坐标设置为x/y
选择器_rect.x,选择器_rect.y=x,y
屏幕填充((0,0,0))
颜色=(0、128、255)
pygame.draw.rect(屏幕、颜色、选择器)
myfont=pygame.font.SysFont(“monospace”,15)
label=myfont.render(“启动实验模拟器”,1,(255255))
屏幕blit(标签,(100100))
label2=myfont.render(“开始测验”,1,(255255))
屏幕blit(标签2,(550100))
label3=myfont.render(“退出游戏”,1,(255255))
屏幕blit(label3,(350400))
#使用我们创建的矩形
pygame.draw.rect(屏幕,红色,模拟器)
pygame.draw.rect(屏幕、红色、小测验)
pygame.draw.rect(屏幕,红色,退出)
#检查用户是否按enter键
如果按下[pygame.K_RETURN]:
#检查选择是否正确
#与任何其他矩形冲突
对于选项_rects中的rect:
#根据需要添加矩形
如果选择器_rect.collide rect(rect):
如果rect==simulator\u rect:
#做些模拟的事情!
打印('模拟!')
elif rect==quick_rect:
#做测验!
打印('Quizzing!')
elif rect==退出:
#退出!
完成=正确
pygame.display.flip()
时钟滴答(60)
这确实给您的程序增加了一些复杂性,但至少这是一种坚如磐石的方法,您也可以添加功能,并且将保持健壮。您可以使用pygame的Collide Rect检查冲突
首先,创建三个表示三个选项矩形的矩形:
simulator_rect = pygame.Rect(600, 125, 30, 30)
quiz_rect = pygame.Rect(225, 125, 30, 30)
quit_rect = pygame.Rect(375, 425, 30, 30)
接下来,我们将创建一个表示蓝色选择器rect的rect:
selector_rect = pygame.Rect(50, 50, 60, 60)
所以现在你有了只创建一次的矩形,而不是每次都创建的未命名矩形
现在,对于实际碰撞检测:
# Check to see if the user presses the enter key
if pressed[pygame.K_RETURN]:
# Check to see if the selection rect
# collides with any other rect
for rect in option_rects:
if selector_rect.colliderect(rect):
if rect == simulator_rect:
# Do simulations stuff!
print('Simulating!')
elif rect == quiz_rect:
# Do quizzing stuff!
print('Quizzing!')
elif rect == quit_rect:
# Quit!
done = True
最终代码:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
done = False
x = 30
y = 30
clock = pygame.time.Clock()
# RGB values for red
red = (255, 0 ,0)
# Your three button rects :
simulator_rect = pygame.Rect(225, 125, 30, 30)
quiz_rect = pygame.Rect(600, 125, 30, 30)
quit_rect = pygame.Rect(375, 425, 30, 30)
# These represent your three option rects
option_rects = [simulator_rect, quiz_rect, quit_rect]
# Your blue selector rect
selector_rect = pygame.Rect(50, 50, 60, 60)
# The 50, 50 xy coords are temporary
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP] and y > 0: y -= 5
if pressed[pygame.K_DOWN] and y < 600 - 60: y += 5
if pressed[pygame.K_LEFT] and x > 0: x -= 5
if pressed[pygame.K_RIGHT] and x < 800 - 60: x += 5
# Set the slector rect's coords to x/y
selector_rect.x, selector_rect.y = x, y
screen.fill((0, 0, 0))
color = (0, 128, 255)
pygame.draw.rect(screen, color, selector_rect)
myfont = pygame.font.SysFont("monospace", 15)
label = myfont.render("Start the experiment simulator", 1, (255,255,255))
screen.blit(label, (100, 100))
label2 = myfont.render("Start the quiz", 1, (255,255,255))
screen.blit(label2, (550, 100))
label3 = myfont.render("Quit game", 1, (255,255,255))
screen.blit(label3, (350, 400))
# Use our created rects
pygame.draw.rect(screen, red, simulator_rect)
pygame.draw.rect(screen, red, quiz_rect)
pygame.draw.rect(screen, red, quit_rect)
# Check to see if the user presses the enter key
if pressed[pygame.K_RETURN]:
# Check to see if the selection rect
# collides with any other rect
for rect in option_rects:
# Add rects as needed
if selector_rect.colliderect(rect):
if rect == simulator_rect:
# Do simulations stuff!
print('Simulating!')
elif rect == quiz_rect:
# Do quizzing stuff!
print('Quizzing!')
elif rect == quit_rect:
# Quit!
done = True
pygame.display.flip()
clock.tick(60)
导入pygame
pygame.init()
screen=pygame.display.set_模式((800600))
完成=错误
x=30
y=30
clock=pygame.time.clock()
#红色的RGB值
红色=(255,0,0)
#您的三个按钮矩形:
模拟器_rect=pygame.rect(225、125、30、30)
quick_rect=pygame.rect(600、125、30、30)
quit_rect=pygame.rect(375425,30,30)
#这些代表三个选项矩形
option_rects=[simulator_rect,quit_rect,quit