首页 编程教程正文

python中pygame针对游戏窗口的显示方法实例分析(附源码)

piaodoo 编程教程 2020-02-02 12:31:34 919 0 python教程

这篇文章主要介绍了python中pygame针对游戏窗口的显示方法,以完整实例形式较为详细的分析了pygame响应键盘按键改变窗口显示效果的相关实现技巧,需要的朋友可以参考下

本文实例讲述了python中pygame针对游戏窗口的显示方法。分享给大家供大家参考,具体如下:

在这篇教程中,我将给出一个demo演示:

当我们按下键盘的‘f'键的时候,演示的窗口会切换到全屏显示和默认显示两种显示模式

并且在后台我们可以看到相关的信息输出:

嗨学网

上面给出了一个简单的例子,当然在pygame的官方文档中有对显示策略的更权威的说明:

http://www.pygame.org/docs/ref/display.html#pygame.display.set_mode

'''
  pygame.FULLSCREEN  create a fullscreen display
  pygame.DOUBLEBUF   recommended for HWSURFACE or OPENGL
  pygame.HWSURFACE   hardware accelerated, only in FULLSCREEN
  pygame.OPENGL    create an opengl renderable display
  pygame.RESIZABLE   display window should be sizeable
  pygame.NOFRAME    display window will have no border or controls
'''

代码部分:

#pygame fullscreen
import os, pygame
from pygame.locals import *
from sys import exit
'''
pygame.display.set_mode():
  pygame.FULLSCREEN  create a fullscreen display
  pygame.DOUBLEBUF   recommended for HWSURFACE or OPENGL
  pygame.HWSURFACE   hardware accelerated, only in FULLSCREEN
  pygame.OPENGL    create an opengl renderable display
  pygame.RESIZABLE   display window should be sizeable
  pygame.NOFRAME    display window will have no border or controls
'''
__author__ = {'name' : 'Hongten',
       'mail' : 'hongtenzone@foxmail.com',
       'Version' : '1.0'}
BG_IMAGE = 'C://py//bg.png'
SCREEN_DEFAULT_SIZE = (500, 500)
pygame.init()
#create the image path
bg_path = os.path.join('data', BG_IMAGE)
if not os.path.exists(bg_path):
  print('The BackGround Image does not exist!')
screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
bg = pygame.image.load(bg_path).convert()
#full screen flag
full_screen = False
while 1:
  for event in pygame.event.get():
    if event.type == QUIT:
      exit()
    if event.type == KEYDOWN:
      #when press the 'f',then change the screen display model
      if event.key == K_f:
        full_screen = not full_screen
        if full_screen:
          print('Open the Fullscreen model!')
        else:
          print('Open the Default model!')
      if full_screen:
        #full screen display model
        screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, FULLSCREEN, 32)
      else:
        #default model
        screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
    screen.blit(bg, (0, 0))
    pygame.display.update()

完整实例代码代码点击此处本站下载。

希望本文所述对大家Python程序设计有所帮助。

版权声明:

本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。

有关影视版权:本站只供百度云网盘资源,版权均属于影片公司所有,请在下载后24小时删除,切勿用于商业用途。本站所有资源信息均从互联网搜索而来,本站不对显示的内容承担责任,如您认为本站页面信息侵犯了您的权益,请附上版权证明邮件告知【754403226@qq.com】,在收到邮件后72小时内删除。本文链接:https://www.piaodoo.com/2169.html

评论

搜索