鱼C论坛

 找回密码
 立即注册
查看: 1946|回复: 4

小甲鱼pygame81讲中退出全屏BUG,求解

[复制链接]
发表于 2022-5-21 22:40:55 | 显示全部楼层 |阅读模式

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

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

x
  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. pygame.init()

  5. listmode = pygame.display.list_modes()

  6. size = width,height = 600,400

  7. speed = [-2,1]

  8. bg = (255,255,255)

  9. fullscreen = False


  10. screen = pygame.display.set_mode(size,RESIZABLE)

  11. pygame.display.set_caption('word game')

  12. turtle = pygame.image.load('btn_tili.png')

  13. position = turtle.get_rect()

  14. l_head = turtle
  15. r_head = pygame.transform.flip(turtle,True,False)

  16. while True:
  17.    
  18.     for event in pygame.event.get():
  19.         
  20.         if event.type == pygame.QUIT:
  21.             sys.exit()
  22.         if event.type == KEYDOWN:
  23.             if event.key == K_LEFT:
  24.                 turtle = l_head
  25.                 speed = [-1,0]

  26.             if event.key == K_RIGHT:
  27.                 turtle = r_head
  28.                 speed = [1,0]
  29.             if event.key == K_UP:
  30.                 speed = [0,-1]
  31.             if event.key == K_DOWN:
  32.                 speed = [0,1]
  33.             
  34.             #全屏(F11)
  35.                 if event.key == K_F11:
  36.                 fullscreen = not fullscreen
  37.                 if fullscreen:
  38.                     screen = pygame.display.set_mode((listmode[0]),FULLSCREEN|HWSURFACE)
  39.                     width,height = listmode[0]
  40.                     
  41.                 else:
  42.                     screen = pygame.display.set_mode(size)
  43.                     

  44.         if event.type == VIDEORESIZE:
  45.             size == event.size
  46.             width,height = size
  47.             screen = pygame.display.set_mode(size,RESIZABLE)
  48.             
  49.     position = position.move(speed)

  50.     if position.left < 0 or position.right > width:

  51.         turtle = pygame.transform.flip(turtle,True,False)

  52.         speed[0] = -speed[0]

  53.     if position.top < 0 or position.bottom > height:

  54.         speed[1] = -speed[1]

  55.     screen.fill(bg)

  56.     screen.blit(turtle,position)

  57.     pygame.display.flip()

  58.     pygame.time.delay(10)
复制代码



标红的为全屏的代码

当乌龟跑超过了 width,height = 600,400;再退出全屏游戏就会报错,请问如何优化这个BUG?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-22 10:51:39 | 显示全部楼层
有点忘了, 应该可以检测点击全屏和退出全屏的按钮, 提供一个思路, 我记得是可行的
  1. if 全屏按钮:
  2.     if 全屏状态:
  3.         重置小乌龟位置
  4.         重置游戏框大小
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-23 09:54:33 | 显示全部楼层
白two 发表于 2022-5-22 10:51
有点忘了, 应该可以检测点击全屏和退出全屏的按钮, 提供一个思路, 我记得是可行的

这个逻辑我知道,但是写了不行,不知道为什么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-23 12:13:50 | 显示全部楼层
这是我以前写的, 你参考一下

  1. import pygame
  2. import sys
  3. from pygame.constants import *


  4. def change_pos(position, width, height):
  5.     if position.right > width:
  6.         position.right = width
  7.                     
  8.     if position.bottom > height:
  9.         position.bottom = height

  10. pygame.init()
  11. # size = width, height = (600, 400)
  12. size = width, height = (1536, 864)
  13. speed = [5, 0]
  14. bg = (255, 255, 255)
  15. clock = pygame.time.Clock()
  16. screen = pygame.display.set_mode(size, RESIZABLE)
  17. pygame.display.set_caption("初次见面,请多多关照!")
  18. # post the turtle
  19. turtle = pygame.image.load(r"D:\user\Desktop\pyPractice\game\turtle\turtle.png").convert_alpha()
  20. position = turtle.get_rect()

  21. for i in range(position.width):
  22.     for j in range(position.height):
  23.         at = turtle.get_at((i, j))
  24.         if at[0] > 100 and at[2] > 200 and at[1] > 100:
  25.             at[3] = 0
  26.         turtle.set_at((i, j),at)

  27. turtle_right = pygame.transform.rotate(turtle, 90)
  28. turtle_top = pygame.transform.rotate(turtle, 90*2)
  29. turtle_left = pygame.transform.rotate(turtle, 90*3)
  30. turtle_bottom = turtle
  31. turtle = turtle_top
  32. fullscreen = False
  33. while True:
  34.     for event in pygame.event.get():
  35.         if event.type == pygame.QUIT:
  36.             sys.exit()
  37.         
  38.         if event.type == VIDEORESIZE:
  39.             if (event.size[0] < 200):
  40.                 event.size = (200, event.size[1])
  41.                 size = event.size
  42.                 width, height = size
  43.                 screen = pygame.display.set_mode(size, RESIZABLE)
  44.                 change_pos(position, width, height)
  45.             
  46.             if (event.size[1] < 200):
  47.                 event.size = (event.size[0], 200)
  48.                 size = event.size
  49.                 width, height = size
  50.                 screen = pygame.display.set_mode(size, RESIZABLE)
  51.                 change_pos(position, width, height)
  52.             else:   
  53.                 size = event.size
  54.                 width, height = size
  55.                 screen = pygame.display.set_mode(size, RESIZABLE)
  56.                 change_pos(position, width, height)

  57.         if event.type == KEYDOWN:   
  58.             if event.key == pygame.K_F11:
  59.                 fullscreen = not fullscreen
  60.                 if fullscreen:
  61.                     # size = width, height = pygame.display.list_modes()[0]
  62.                     size = width, height = (1536, 864)
  63.                     screen = pygame.display.set_mode(size, pygame.HWSURFACE)

  64.                 else:
  65.                     size = width, height = (600, 400)
  66.                     screen = pygame.display.set_mode(size)
  67.                     change_pos(position, width, height)

  68.     position = position.move(speed)
  69.     if position.right > width:
  70.         turtle = turtle_right
  71.         position = turtle_rect = turtle.get_rect()
  72.         position.right = width
  73.         speed = [0, 5]

  74.     if position.bottom > height:
  75.         turtle = turtle_bottom
  76.         position = turtle_rect = turtle.get_rect()
  77.         position.right = width
  78.         position.bottom = height
  79.         speed = [-5, 0]
  80.    
  81.     if position.left < 0:
  82.         turtle = turtle_left
  83.         position = turtle_rect = turtle.get_rect()
  84.         position.bottom = height
  85.         speed = [0, -5]
  86.    
  87.     if position.top < 0:
  88.         turtle = turtle_top
  89.         position = turtle_rect = turtle.get_rect()
  90.         speed = [5, 0]

  91.     screen.fill(bg)
  92.     screen.blit(turtle, position)
  93.     pygame.display.flip()
  94.     pygame.time.delay(10)
  95.     clock.tick(200)


复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-23 12:50:01 | 显示全部楼层
白two 发表于 2022-5-23 12:13
这是我以前写的, 你参考一下
  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. from pygame.constants import *

  5. pygame.init()



  6. size = width,height  = 600,400

  7. speed = [1,1]

  8. bg = (255,255,255) #RGB颜色

  9. fullscreen = False


  10. screen = pygame.display.set_mode(size,RESIZABLE)

  11. pygame.display.set_caption('word game')

  12. turtle = pygame.image.load('btn_tili.png').convert_alpha()

  13. position = turtle.get_rect()

  14. l_head = turtle
  15. r_head = pygame.transform.flip(turtle,True,False)

  16. def change_pos(position, width, height):
  17.     if position.right > width:
  18.         position.right = width
  19.                     
  20.     if position.bottom > height:
  21.         position.bottom = height

  22. while True:
  23.    
  24.    
  25.     for event in pygame.event.get():
  26.         
  27.         if event.type == pygame.QUIT:
  28.             sys.exit()
  29.         if event.type == KEYDOWN:
  30.             if event.key == K_LEFT:
  31.                 turtle = l_head
  32.                 speed = [-1,0]

  33.             if event.key == K_RIGHT:
  34.                 turtle = r_head
  35.                 speed = [1,0]
  36.             if event.key == K_UP:
  37.                 speed = [0,-1]
  38.             if event.key == K_DOWN:
  39.                 speed = [0,1]
  40.             
  41.             #全屏(F11)
  42.             if event.key == K_F11:
  43.                 fullscreen = not fullscreen
  44.                 if fullscreen:
  45.                     listmode = pygame.display.list_modes()
  46.                     width, height =  listmode[0]
  47.                     screen = pygame.display.set_mode((listmode[0]),pygame.FULLSCREEN)
  48.                     
  49.                 else:
  50.                     size = width, height = (600, 400)
  51.                     screen = pygame.display.set_mode(size)
  52.                     change_pos(position, width, height)
  53.                     

  54.         if event.type == VIDEORESIZE:
  55.             size == event.size
  56.             width,height = size
  57.             screen = pygame.display.set_mode(size,RESIZABLE)
  58.             
  59.     position = position.move(speed)

  60.     if position.left < 0 or position.right > width:

  61.         turtle = pygame.transform.flip(turtle,True,False)

  62.         speed[0] = -speed[0]

  63.     if position.top < 0 or position.bottom > height:

  64.         speed[1] = -speed[1]

  65.     screen.fill(bg)

  66.     screen.blit(turtle,position)

  67.     pygame.display.flip()

  68.     pygame.time.delay(10)

  69.         
复制代码


按照你这个调了缩小可以了,但是还有一个BUG F11,只可以按一次,退出后第二次按就不可以全屏,这个是什么原因?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-29 05:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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