Python pygame Display Method for game windows instance analysis (with source code), pythonpygame

Source: Internet
Author: User

Python pygame Display Method for game windows instance analysis (with source code), pythonpygame

This article describes how pygame displays game windows in python. We will share this with you for your reference. The details are as follows:

In this tutorial, I will give a demo:

When we press the 'F' key on the keyboard, the displayed window will switch to the full screen display mode and the default display mode.

In addition, we can see the relevant information output in the background:

The above is a simple example. Of course, there is a more authoritative description of the display policy in pygame's official documentation:

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'''

Code Section:

#pygame fullscreenimport os, pygamefrom 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 pathbg_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 flagfull_screen = Falsewhile 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()

Click here to download the complete instance code.

I hope this article will help you with Python programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.