鱼C论坛

 找回密码
 立即注册
查看: 1099|回复: 3

[已解决]Pygame全屏问题

[复制链接]
发表于 2020-8-1 18:10:08 | 显示全部楼层 |阅读模式

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

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

x
跟随小甲鱼课程实践,小乌龟在窗口模式移动正常,但是全屏后小乌龟只在屏幕边缘移动,运动轨迹十分诡异
  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. # 初始化pygame
  5. pygame.init()

  6. size = width, height = 600, 400
  7. speed = [-2, 1]
  8. bg = (255, 255, 255) # RGB

  9. clock = pygame.time.Clock()
  10. fullscreen = False

  11. # 创建指定大小的窗口 Surface
  12. screen = pygame.display.set_mode(size)
  13. # 设置窗口标题
  14. pygame.display.set_caption('初次见面,请大家多多关照!')

  15. # 加载图片
  16. turtle = pygame.image.load('turtle.png')
  17. # 获得图像的位置矩形
  18. position = turtle.get_rect()

  19. l_head = turtle
  20. r_head = pygame.transform.flip(turtle, True, False)

  21. while True:
  22.     for event in pygame.event.get():
  23.         if event.type == QUIT:
  24.             sys.exit()

  25.         if event.type == KEYDOWN:
  26.             if event.key == K_LEFT:
  27.                 turtle = l_head
  28.                 speed = [-1, 0]
  29.             if event.key == K_RIGHT:
  30.                 turtle = r_head
  31.                 speed = [1, 0]
  32.             if event.key == K_UP:
  33.                 speed = [0, -1]
  34.             if event.key == K_DOWN:
  35.                 speed = [0, 1]

  36.             # 全屏(F11)
  37.             if event.key == K_F11:
  38.                 fullscreen = not fullscreen
  39.                 if fullscreen:
  40.                     SIZE = pygame.display.list_modes()[0]
  41.                     pygame.display.set_mode(SIZE, FULLSCREEN | HWSURFACE)
  42.                 else:
  43.                     pygame.display.set_mode(size)

  44.     # 移动图像
  45.     position = position.move(speed)

  46.     if position.left < 0 or position.right > width:
  47.         # 翻转图像
  48.         turtle = pygame.transform.flip(turtle, True, False)
  49.         # 反方向移动
  50.         speed[0] = -speed[0]

  51.     if position.top < 0 or position.bottom > height:
  52.         speed[1] = -speed[1]

  53.     # 填充背景
  54.     screen.fill(bg)
  55.     # 更新图像
  56.     screen.blit(turtle, position)
  57.     # 更新界面
  58.     pygame.display.flip()
  59.     # 延迟10毫秒
  60.     # pygame.time.delay(10)
  61.     clock.tick(100)
复制代码

最佳答案
2020-8-1 18:14:30
本帖最后由 zltzlt 于 2020-8-1 18:15 编辑

这样基本上可以了
  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. # 初始化pygame
  5. pygame.init()

  6. size = width, height = 600, 400
  7. speed = [-2, 1]
  8. bg = (255, 255, 255) # RGB

  9. clock = pygame.time.Clock()
  10. fullscreen = False

  11. # 创建指定大小的窗口 Surface
  12. screen = pygame.display.set_mode(size)
  13. # 设置窗口标题
  14. pygame.display.set_caption('初次见面,请大家多多关照!')

  15. # 加载图片
  16. turtle = pygame.image.load('turtle.png')
  17. # 获得图像的位置矩形
  18. position = turtle.get_rect()

  19. l_head = turtle
  20. r_head = pygame.transform.flip(turtle, True, False)

  21. while True:
  22.     for event in pygame.event.get():
  23.         if event.type == QUIT:
  24.             sys.exit()

  25.         if event.type == KEYDOWN:
  26.             if event.key == K_LEFT:
  27.                 turtle = l_head
  28.                 speed = [-1, 0]
  29.             if event.key == K_RIGHT:
  30.                 turtle = r_head
  31.                 speed = [1, 0]
  32.             if event.key == K_UP:
  33.                 speed = [0, -1]
  34.             if event.key == K_DOWN:
  35.                 speed = [0, 1]

  36.             # 全屏(F11)
  37.             if event.key == K_F11:
  38.                 fullscreen = not fullscreen
  39.                 if fullscreen:
  40.                     SIZE = width, height = pygame.display.list_modes()[0]
  41.                     pygame.display.set_mode(SIZE, FULLSCREEN | HWSURFACE)
  42.                 else:
  43.                     width, height = size
  44.                     pygame.display.set_mode(size)

  45.     # 移动图像
  46.     position = position.move(speed)

  47.     if position.left < 0 or position.right > width:
  48.         # 翻转图像
  49.         turtle = pygame.transform.flip(turtle, True, False)
  50.         # 反方向移动
  51.         speed[0] = -speed[0]

  52.     if position.top < 0 or position.bottom > height:
  53.         speed[1] = -speed[1]

  54.     # 填充背景
  55.     screen.fill(bg)
  56.     # 更新图像
  57.     screen.blit(turtle, position)
  58.     # 更新界面
  59.     pygame.display.flip()
  60.     # 延迟10毫秒
  61.     # pygame.time.delay(10)
  62.     clock.tick(100)
复制代码

小乌龟只在红色区域移动,还穿来穿去

小乌龟只在红色区域移动,还穿来穿去
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-8-1 18:14:30 | 显示全部楼层    本楼为最佳答案   
本帖最后由 zltzlt 于 2020-8-1 18:15 编辑

这样基本上可以了
  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. # 初始化pygame
  5. pygame.init()

  6. size = width, height = 600, 400
  7. speed = [-2, 1]
  8. bg = (255, 255, 255) # RGB

  9. clock = pygame.time.Clock()
  10. fullscreen = False

  11. # 创建指定大小的窗口 Surface
  12. screen = pygame.display.set_mode(size)
  13. # 设置窗口标题
  14. pygame.display.set_caption('初次见面,请大家多多关照!')

  15. # 加载图片
  16. turtle = pygame.image.load('turtle.png')
  17. # 获得图像的位置矩形
  18. position = turtle.get_rect()

  19. l_head = turtle
  20. r_head = pygame.transform.flip(turtle, True, False)

  21. while True:
  22.     for event in pygame.event.get():
  23.         if event.type == QUIT:
  24.             sys.exit()

  25.         if event.type == KEYDOWN:
  26.             if event.key == K_LEFT:
  27.                 turtle = l_head
  28.                 speed = [-1, 0]
  29.             if event.key == K_RIGHT:
  30.                 turtle = r_head
  31.                 speed = [1, 0]
  32.             if event.key == K_UP:
  33.                 speed = [0, -1]
  34.             if event.key == K_DOWN:
  35.                 speed = [0, 1]

  36.             # 全屏(F11)
  37.             if event.key == K_F11:
  38.                 fullscreen = not fullscreen
  39.                 if fullscreen:
  40.                     SIZE = width, height = pygame.display.list_modes()[0]
  41.                     pygame.display.set_mode(SIZE, FULLSCREEN | HWSURFACE)
  42.                 else:
  43.                     width, height = size
  44.                     pygame.display.set_mode(size)

  45.     # 移动图像
  46.     position = position.move(speed)

  47.     if position.left < 0 or position.right > width:
  48.         # 翻转图像
  49.         turtle = pygame.transform.flip(turtle, True, False)
  50.         # 反方向移动
  51.         speed[0] = -speed[0]

  52.     if position.top < 0 or position.bottom > height:
  53.         speed[1] = -speed[1]

  54.     # 填充背景
  55.     screen.fill(bg)
  56.     # 更新图像
  57.     screen.blit(turtle, position)
  58.     # 更新界面
  59.     pygame.display.flip()
  60.     # 延迟10毫秒
  61.     # pygame.time.delay(10)
  62.     clock.tick(100)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-1 18:37:50 | 显示全部楼层
本帖最后由 fishc098 于 2020-8-1 22:00 编辑

运动区域正常了,但是全屏后小乌龟还是超出屏幕边缘

全屏

全屏
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-3 16:36:56 | 显示全部楼层
屏幕分辨率的问题,好吧我解决了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 18:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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