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

yipeiwu_com5年前Python基础

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

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

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

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

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

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

'''
  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程序设计有所帮助。

相关文章

更新pip3与pyttsx3文字语音转换的实现方法

我使用的python版本是3.5.2 今天想做个语音读取的小脚本,在网上查了一下发现python里有个pyttsx可以识别文字, 打算通过pip3 install pyttsx安装包,结...

对python特殊函数 __call__()的使用详解

__call__ 在Python中,函数其实是一个对象: >>> f = abs >>> f.__name__ 'abs' >>>...

python使用rsa非对称加密过程解析

python使用rsa非对称加密过程解析

这篇文章主要介绍了python使用rsa非对称加密过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1、安装rsa 支持pyth...

python 实现将txt文件多行合并为一行并将中间的空格去掉方法

有一个txt文本如下: 151 151 1234561 156421 214156 1523132 031320 现希望将两行合并为一行,并将中间所有的空格都去掉: (pyth...

Python调用C++程序的方法详解

前言 大家都知道Python的优点是开发效率高,使用方便,C++则是运行效率高,这两者可以相辅相成,不管是在Python项目中嵌入C++代码,或是在C++项目中用Python实现外围功能...