python - Pygame 暂停功能
问题描述
我一直在尝试为我的乒乓球游戏添加暂停功能,但它不起作用。我可以暂停游戏一次然后它停止,但之后如果我按下暂停游戏的键,游戏将移动几帧然后停止。之后,我必须按住暂停按钮才能使游戏移动。我将链接下面的类代码:
class Game:
def __init__(self, start):
self.start = start
self.score1 = 0
self.score2 = 0
self.pause = False
self.over = False
def play(self, ai):
player2.AI = ai
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pong")
self.over = False
while self.start:
# quits if x is pressed
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
pressed_keys = pygame.key.get_pressed()
if pressed_keys[pygame.K_p]:
self.pauseMenu()
if self.pause == False:
# creates list of pressed keys and using that to determine movement
pressed_keys = pygame.key.get_pressed()
if pressed_keys[pygame.K_w]:
player1.moveUp(5)
if pressed_keys[pygame.K_s]:
player1.moveDown(5)
if player2.AI == True:
player2.AI_move(ball)
if pressed_keys[pygame.K_SPACE]:
pygame.quit()
else:
if pressed_keys[pygame.K_UP]:
player2.moveUp(5)
if pressed_keys[pygame.K_DOWN]:
player2.moveDown(5)
if pressed_keys[pygame.K_p]:
self.pauseMenu()
# scoring system if ball touches variable, increments score and resets ball to middle
if ball.rect.x >= 790:
self.score1 += 1
ball.rect.x = 400
ball.rect.y = 240
ball.x_velo = choice([-2, 2])
ball.y_velo = choice([-1, -2, 1, 2])
if ball.rect.x <= 0:
self.score2 += 1
ball.rect.x = 400
ball.rect.y = 240
ball.x_velo = choice([-2, 2])
ball.y_velo = choice([-1, -2, 1, 2])
# bounces ball of top or bottom wall
if ball.rect.y > 470 or ball.rect.y < 0:
ball.y_velo = -ball.y_velo
# detects collision and reflects ball off board
if pygame.sprite.collide_mask(ball, player1) or pygame.sprite.collide_mask(ball, player2):
ball.bounce()
# refresh method to move ball
ball.refresh()
# draws background and sprites onto screen each tick
screen.fill(BLACK)
pygame.draw.line(screen, WHITE, [400, 0], [400, 580], 1)
all_sprites_list.update()
all_sprites_list.draw(screen)
# draws score at top
font = pygame.font.Font('ARCADECLASSIC.TTF', 74)
text = font.render(str(self.score1), 1, BLUE)
screen.blit(text, (300, 10))
text = font.render(str(self.score2), 1, BLUE)
screen.blit(text, (470, 10))
if self.score1 >= 5:
self.victory("Player 1")
elif self.score2 >= 5:
self.victory("Player 2")
# refreshes display each tick
pygame.display.flip()
# sets number of ticks per second
clock.tick(60)
def victory(self, winner):
pass
def pauseMenu(self):
if self.pause == True:
self.pause = False
elif self.pause == False:
self.pause = True
解决方案
你可以试试这个:
pause=False
#your game code
for event in pygame.event.get():
if event.type==KEYUP:
if event.key==K_p:
pause = True
while pause == True:
for event in pygame.event.get():
if event.type==KEYUP:
if event.key==K_p:
pause = False
#your remaining code
您首先将暂停设为假,然后添加一个暂停屏幕的键。
推荐阅读
- r - 如何使用 ggplot 中的 facet 将 2 个类别与整个类别进行比较
- apache-kafka - 在 Kafka 的事件中获取统计信息的更好方法是什么?
- reactjs - 即使没有内容,如何强制 antd 选项卡全高(使用 React 和样式化组件)
- c# - Unity用文本替换登录字符串
- r - popupGraph - R,Leaflet:为什么我的弹出图是空白的?
- c# - C#如何通过反射初始化属性(没有setter)
- visual-studio - 如何在容器中成功安装 Visual Studio Build Tools 2019?
- c - 如果找到任何带有 0 的元素,则添加 0 并将其推送到最后一个元素
- sql - Spark 在一行中合并行
- spring-boot - 不支持 Spring Cloud 合同内容类型