鱼C论坛

 找回密码
 立即注册
查看: 2250|回复: 1

[已解决]pygame如何实现游戏重新开始的功能?

[复制链接]
发表于 2021-10-22 10:48:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. # 导入
  2. import pygame
  3. from pygame.locals import *
  4. from random import *
  5. import sys

  6. pygame.init()
  7. screen = pygame.display.set_mode((600, 500))


  8. # 绘制球和挡板
  9. def ball(ax, ay):
  10.     pygame.draw.circle(screen, (255, 255, 0), (ax, ay), 20)


  11. def fang(ax):
  12.     size = 300 + ax, 450, 100, 50
  13.     pygame.draw.rect(screen, (0, 255, 255), size)


  14. # 绘制文本的函数
  15. def print_text(font, text, ):
  16.     textImg = font.render(text, )
  17.     screen.blit(textImg, )


  18. # zhuangtai = False
  19. score = 0
  20. gameover = False
  21. fx = 0
  22. move_x = 0
  23. bx = randint(10, 590)
  24. by = 0
  25. # 循环
  26. while True:
  27.     # 搜索事件
  28.     for event in pygame.event.get():
  29.         if event.type == QUIT:
  30.             sys.exit()
  31.         elif event.type == MOUSEMOTION:
  32.             mouse_x, mouse_y = event.pos
  33.             move_x, move_y = event.rel

  34.     # 绘制画面
  35.     screen.fill((255, 255, 255))
  36.     # 逻辑判断
  37.     if by > 500:
  38.         by = 0
  39.         gameover = True

  40.     if 500 > by > 450 and fx + 100 > bx > fx:
  41.         score += 10
  42.         by = 0
  43.         bx = randint(10, 590)
  44.     # 绘制球
  45.     ball(bx, by)
  46.     if by == 500:
  47.         by = 0
  48.         bx = randint(10, 590)
  49.     by = by + 0.2
  50.     ball(bx, by)
  51.     # 绘制方块
  52.     fang(fx)
  53.     if move_x < 0:
  54.         fx -= 0.5
  55.     else:
  56.         fx += 0.5
  57.     if fx > 200:
  58.         fx = 200
  59.     if fx < -300:
  60.         fx = -300

  61.     if gameover:
  62.         while True:
  63.             for event in pygame.event.get():
  64.                 if event.type == K_SPACE:
  65.                     gameover = False
  66.                     by = 0
  67.                     break

  68.     pygame.display.update()
复制代码

如代码,使用了状态变量控制进入一个死循环进行游戏的暂停功能,但是问题来了,当我按下空格键不会跳出这个死循环,按下空格键时变量该修改的也修改了,求教大佬这里的逻辑错误~!
最佳答案
2021-10-22 18:58:51
本帖最后由 淡淡凉 于 2021-10-22 19:00 编辑

76行 if event.type == K_SPACE:有问题

  1. # 导入
  2. import pygame
  3. from pygame.locals import *
  4. from random import *
  5. import sys
  6. x,y=600,500
  7. pygame.init()
  8. screen = pygame.display.set_mode((x, y))


  9. # 绘制球和挡板
  10. def ball(ax, ay):
  11.     pygame.draw.circle(screen, (255, 255, 0), (ax, ay), 20)


  12. def fang(ax):
  13.     size = ax, 450, 100, 50
  14.     pygame.draw.rect(screen, (0, 255, 255), size)


  15. # 绘制文本的函数
  16. def print_text(font, text, ):
  17.     textImg = font.render(text, )
  18.     screen.blit(textImg, )


  19. # zhuangtai = False
  20. score = 0
  21. gameover = False
  22. fx = 0
  23. move_x = 0
  24. bx = randint(10, 590)
  25. by = 0
  26. # 循环
  27. while True:
  28.     # 搜索事件
  29.     for event in pygame.event.get():
  30.         if event.type == QUIT:
  31.             sys.exit()
  32.         elif event.type == MOUSEMOTION:
  33.             mouse_x, mouse_y = event.pos
  34.             move_x, move_y = event.rel

  35.         
  36.     # 绘制画面
  37.     screen.fill((255, 255, 255))
  38.     # 绘制方块
  39.     fang(fx)
  40.     if move_x < 0:
  41.         fx -= 0.2
  42.     else:
  43.         fx += 0.2
  44.     if fx > 500:
  45.         fx = 500
  46.     if fx < 0:
  47.         fx = 0
  48.       
  49.     # 逻辑判断
  50.     if by > 485:
  51.         #print(bx, by)
  52.         #ball(bx, by)
  53.         by = 0
  54.         gameover = True
  55.         
  56.         #print('-------')
  57.         '''
  58.     elif by == 540:
  59.         by = 0
  60.         bx = randint(10, 590)
  61.         ball(bx, by)'''   

  62.     elif 450 < by < 480 and fx < bx < fx + 100:
  63.         score += 10
  64.         by = 0
  65.         bx = randint(10, 590)
  66.         print(score)
  67.     # 绘制球
  68.         ball(bx, by)     

  69.     else:
  70.         by = by + 0.1
  71.         ball(bx, by)  
  72.     #print(fx,bx)
  73.     if gameover:
  74.         while gameover:
  75.                 for event in pygame.event.get():
  76.                         if event.type == QUIT:
  77.                                 sys.exit()
  78.                         if event.type == KEYDOWN:        #事件检测,事件类型KEYDOWN按下按键,KEYUP松开按键
  79.                                 if event.key == K_SPACE:    #判断是否按下空格键
  80.                                         gameover = False
  81.                                         by = 0
  82.                                         #print(gameover)
  83.                                         #break
  84.                
  85.     pygame.display.update()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-22 18:58:51 | 显示全部楼层    本楼为最佳答案   
本帖最后由 淡淡凉 于 2021-10-22 19:00 编辑

76行 if event.type == K_SPACE:有问题

  1. # 导入
  2. import pygame
  3. from pygame.locals import *
  4. from random import *
  5. import sys
  6. x,y=600,500
  7. pygame.init()
  8. screen = pygame.display.set_mode((x, y))


  9. # 绘制球和挡板
  10. def ball(ax, ay):
  11.     pygame.draw.circle(screen, (255, 255, 0), (ax, ay), 20)


  12. def fang(ax):
  13.     size = ax, 450, 100, 50
  14.     pygame.draw.rect(screen, (0, 255, 255), size)


  15. # 绘制文本的函数
  16. def print_text(font, text, ):
  17.     textImg = font.render(text, )
  18.     screen.blit(textImg, )


  19. # zhuangtai = False
  20. score = 0
  21. gameover = False
  22. fx = 0
  23. move_x = 0
  24. bx = randint(10, 590)
  25. by = 0
  26. # 循环
  27. while True:
  28.     # 搜索事件
  29.     for event in pygame.event.get():
  30.         if event.type == QUIT:
  31.             sys.exit()
  32.         elif event.type == MOUSEMOTION:
  33.             mouse_x, mouse_y = event.pos
  34.             move_x, move_y = event.rel

  35.         
  36.     # 绘制画面
  37.     screen.fill((255, 255, 255))
  38.     # 绘制方块
  39.     fang(fx)
  40.     if move_x < 0:
  41.         fx -= 0.2
  42.     else:
  43.         fx += 0.2
  44.     if fx > 500:
  45.         fx = 500
  46.     if fx < 0:
  47.         fx = 0
  48.       
  49.     # 逻辑判断
  50.     if by > 485:
  51.         #print(bx, by)
  52.         #ball(bx, by)
  53.         by = 0
  54.         gameover = True
  55.         
  56.         #print('-------')
  57.         '''
  58.     elif by == 540:
  59.         by = 0
  60.         bx = randint(10, 590)
  61.         ball(bx, by)'''   

  62.     elif 450 < by < 480 and fx < bx < fx + 100:
  63.         score += 10
  64.         by = 0
  65.         bx = randint(10, 590)
  66.         print(score)
  67.     # 绘制球
  68.         ball(bx, by)     

  69.     else:
  70.         by = by + 0.1
  71.         ball(bx, by)  
  72.     #print(fx,bx)
  73.     if gameover:
  74.         while gameover:
  75.                 for event in pygame.event.get():
  76.                         if event.type == QUIT:
  77.                                 sys.exit()
  78.                         if event.type == KEYDOWN:        #事件检测,事件类型KEYDOWN按下按键,KEYUP松开按键
  79.                                 if event.key == K_SPACE:    #判断是否按下空格键
  80.                                         gameover = False
  81.                                         by = 0
  82.                                         #print(gameover)
  83.                                         #break
  84.                
  85.     pygame.display.update()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 18:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表