鱼C论坛

 找回密码
 立即注册
查看: 1666|回复: 10

[已解决]为什么pygame不可以全屏?

[复制链接]
发表于 2021-1-31 11:36:12 | 显示全部楼层 |阅读模式

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

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

x
import pygame
import sys
from pygame.locals import *
pygame.init()#初始化

clock=pygame.time.Clock()#控制帧率

size=width,height=600,400
speed=[-2,1]
bg=(255,255,255)

fullscreen=False
#创建指定大小的窗口
screen=pygame.display.set_mode(size)
#设置窗口标题
pygame.display.set_caption("Hello pygame!")
#加载图片
cat=pygame.image.load('arrow.jpg')
#获得图像的位置矩形
position=cat.get_rect()
cat=pygame.transform.rotate(cat,180)
r_head=cat                                  #右
l_head=pygame.transform.rotate(cat,180)     #左
u_head=pygame.transform.rotate(cat,270)     #上
d_head=pygame.transform.rotate(cat,90)      #下

while 1:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()

    if event.type==KEYDOWN:
        if event.key==K_LEFT:
            speed=[-1,0]
            cat=r_head
        if event.key==K_RIGHT:
            speed=[1,0]
            cat=l_head
        if event.key==K_UP:
            speed=[0,-1]
            cat=u_head
        if event.key==K_DOWN:
            speed=[0,1]
            cat=d_head

        if event.key == K_F11:
            fullscreen = not fullscreen
            if fullscreen:
                screen = pygame.display.set_mode((1920,1080),FULLSCREEN | HWSURFACE)
            else:
                screen = pygame.display.set_mode(size)

    #开始移动
    position = position.move(speed)

    if position.left<0 or position.right>width:
        #翻转图像,水平翻转为True,竖直翻转为False
        cat=pygame.transform.flip(cat,True,False)
        #反方向移动
        speed[0]=-speed[0]

    if position.top<0 or position.bottom>height:
        cat = pygame.transform.flip(cat, False, True)
        speed[1] = -speed[1]


    #填充背景
    screen.fill(bg)
    #更新图像
    screen.blit(cat,position)
    #更新界面
    pygame.display.flip()
    #延迟画面(10ms)
    #pygame.time.delay(10)
    clock.tick(100)
最佳答案
2021-1-31 11:48:14


试试这个代码:


  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. pygame.init()#初始化

  5. clock=pygame.time.Clock()#控制帧率

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

  9. fullscreen=False
  10. #创建指定大小的窗口
  11. screen=pygame.display.set_mode(size)
  12. #设置窗口标题
  13. pygame.display.set_caption("Hello pygame!")
  14. #加载图片
  15. cat=pygame.image.load('arrow.jpg')
  16. #获得图像的位置矩形
  17. position=cat.get_rect()
  18. cat=pygame.transform.rotate(cat,180)
  19. r_head=cat                                  #右
  20. l_head=pygame.transform.rotate(cat,180)     #左
  21. u_head=pygame.transform.rotate(cat,270)     #上
  22. d_head=pygame.transform.rotate(cat,90)      #下

  23. while 1:
  24.     for event in pygame.event.get():
  25.         if event.type==pygame.QUIT:
  26.             sys.exit()

  27.         if event.type==KEYDOWN:
  28.             if event.key==K_LEFT:
  29.                 speed=[-1,0]
  30.                 cat=r_head
  31.             if event.key==K_RIGHT:
  32.                 speed=[1,0]
  33.                 cat=l_head
  34.             if event.key==K_UP:
  35.                 speed=[0,-1]
  36.                 cat=u_head
  37.             if event.key==K_DOWN:
  38.                 speed=[0,1]
  39.                 cat=d_head
  40.    
  41.             if event.key == K_F11:
  42.                 fullscreen = not fullscreen
  43.                 if fullscreen:
  44.                     screen = pygame.display.set_mode((1920,1080),FULLSCREEN | HWSURFACE)
  45.                 else:
  46.                     screen = pygame.display.set_mode(size)

  47.     #开始移动
  48.     position = position.move(speed)

  49.     if position.left<0 or position.right>width:
  50.         #翻转图像,水平翻转为True,竖直翻转为False
  51.         cat=pygame.transform.flip(cat,True,False)
  52.         #反方向移动
  53.         speed[0]=-speed[0]

  54.     if position.top<0 or position.bottom>height:
  55.         cat = pygame.transform.flip(cat, False, True)
  56.         speed[1] = -speed[1]


  57.     #填充背景
  58.     screen.fill(bg)
  59.     #更新图像
  60.     screen.blit(cat,position)
  61.     #更新界面
  62.     pygame.display.flip()
  63.     #延迟画面(10ms)
  64.     #pygame.time.delay(10)
  65.     clock.tick(100)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-31 11:48:14 | 显示全部楼层    本楼为最佳答案   


试试这个代码:


  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. pygame.init()#初始化

  5. clock=pygame.time.Clock()#控制帧率

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

  9. fullscreen=False
  10. #创建指定大小的窗口
  11. screen=pygame.display.set_mode(size)
  12. #设置窗口标题
  13. pygame.display.set_caption("Hello pygame!")
  14. #加载图片
  15. cat=pygame.image.load('arrow.jpg')
  16. #获得图像的位置矩形
  17. position=cat.get_rect()
  18. cat=pygame.transform.rotate(cat,180)
  19. r_head=cat                                  #右
  20. l_head=pygame.transform.rotate(cat,180)     #左
  21. u_head=pygame.transform.rotate(cat,270)     #上
  22. d_head=pygame.transform.rotate(cat,90)      #下

  23. while 1:
  24.     for event in pygame.event.get():
  25.         if event.type==pygame.QUIT:
  26.             sys.exit()

  27.         if event.type==KEYDOWN:
  28.             if event.key==K_LEFT:
  29.                 speed=[-1,0]
  30.                 cat=r_head
  31.             if event.key==K_RIGHT:
  32.                 speed=[1,0]
  33.                 cat=l_head
  34.             if event.key==K_UP:
  35.                 speed=[0,-1]
  36.                 cat=u_head
  37.             if event.key==K_DOWN:
  38.                 speed=[0,1]
  39.                 cat=d_head
  40.    
  41.             if event.key == K_F11:
  42.                 fullscreen = not fullscreen
  43.                 if fullscreen:
  44.                     screen = pygame.display.set_mode((1920,1080),FULLSCREEN | HWSURFACE)
  45.                 else:
  46.                     screen = pygame.display.set_mode(size)

  47.     #开始移动
  48.     position = position.move(speed)

  49.     if position.left<0 or position.right>width:
  50.         #翻转图像,水平翻转为True,竖直翻转为False
  51.         cat=pygame.transform.flip(cat,True,False)
  52.         #反方向移动
  53.         speed[0]=-speed[0]

  54.     if position.top<0 or position.bottom>height:
  55.         cat = pygame.transform.flip(cat, False, True)
  56.         speed[1] = -speed[1]


  57.     #填充背景
  58.     screen.fill(bg)
  59.     #更新图像
  60.     screen.blit(cat,position)
  61.     #更新界面
  62.     pygame.display.flip()
  63.     #延迟画面(10ms)
  64.     #pygame.time.delay(10)
  65.     clock.tick(100)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-31 12:01:28 | 显示全部楼层
while 1 循环的第二个if的缩进导致的。第二个if也应该在for循环内。
修改这个就没有问题了

另外下次发代码请用代码格式,那样更容易帮你看程序问题。
发帖的时候,编辑框上方工具区有个"<>”按钮,是发代码用的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-31 12:45:50 | 显示全部楼层
sunrise085 发表于 2021-1-31 12:01
while 1 循环的第二个if的缩进导致的。第二个if也应该在for循环内。
修改这个就没有问题了

谢谢大佬提醒
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-31 12:50:49 | 显示全部楼层

大佬,你这个也不行,按f11没有任何反应,感觉可能是我的电脑不支持全屏
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-31 12:52:41 | 显示全部楼层
zcn123 发表于 2021-1-31 12:50
大佬,你这个也不行,按f11没有任何反应,感觉可能是我的电脑不支持全屏


好吧,你运行代码后有点击下弹出的窗口吗?点击完再试试 F11
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-31 13:02:09 | 显示全部楼层
Twilight6 发表于 2021-1-31 12:52
好吧,你运行代码后有点击下弹出的窗口吗?点击完再试试 F11

我点了,没用
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-31 13:14:35 | 显示全部楼层
Twilight6 发表于 2021-1-31 12:52
好吧,你运行代码后有点击下弹出的窗口吗?点击完再试试 F11

我摸索出来了,我笔记本home键与f11键重合了,而按键触发的是home键效果,K_F11改成K_HOME就可以了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-31 13:16:14 | 显示全部楼层
zcn123 发表于 2021-1-31 13:14
我摸索出来了,我笔记本home键与f11键重合了,而按键触发的是home键效果,K_F11改成K_HOME就可以了



原来如此
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-31 13:19:31 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-31 13:20:24 | 显示全部楼层

出现这种情况我也挺无语的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-29 11:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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