In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to use the minimum development framework of Pygame based on python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the python-based Pygame minimum development framework.
1. Minimum development framework code import sys import pygamepygame.init () size=w,h = (800600) screen = pygame.display.set_mode (size) pygame.display.set_caption ("hello world") screen.fill ((255255255)) while True: for event in pygame.event.get (): if event.type = = pygame.QUIT: sys.exit () pygame.display.update () 2. The key code introduces the standard library of sys and pygame module import sys # python, initializes and creates the internal functional modules. The system module import pygame # pygame is a cross-platform Python module, specially designed for electronic games, including images, sounds and so on. Initialize init () and set pygame.init () # computer call interface, check basic functions (hardware environment) initialization
Pygame.init () is to check whether there is something wrong with some hardware calling interfaces and basic functions on the computer, and if so, it will give feedback before the program runs to facilitate troubleshooting. To put it simply, it initializes the interface called by the computer.
Screen size, title, and color settings size=w,h = (800600) screen = pygame.display.set_mode (size) # pygame.display.set_mode a binary tuple that represents width and height, respectively. Pygame.display.set_caption ("hello world") # the color of the window's title content screen.fill ((255255255)) # screen in parentheses, filled with background color, where the parameter is (RMageGprit B), with a range of: 0 percent (255), expressed as an integer.
Pygame.display.set_mode (resolution= (0J0), flags=0, depth=0)-> Surface (object).
The resolution parameter is a binary that represents width and height.
The flags parameter is a collection of attachment options.
The flags parameter specifies the type of display you want. For example: full screen display, double buffer mode, hardware acceleration and so on.
The depth parameter indicates the color depth used.
Generally speaking, there is no need to pass the depth parameter. Because the default Pygame chooses the best and fastest color depth based on the current operating system.
Event loop while True: for event in pygame.event.get (): # traversing the event list
The while True loops indefinitely until the event triggered from the keyboard or mouse exits the loop.
Pygame.event.get () takes the event (list) from the event queue and removes the event from the queue, for example, keyboard press is an event
In pygame, all used events are stored in the event list, and you can fetch an event using pygame.event.get ().
Quit the game if event.type = = pygame.QUIT: # pygame.QUIT = = 12 sys.exit () # quit the game
Event.type gets the event type and responds one by one
Pygame.QUIT is the exit event constant defined in pygame
Window refresh:
Pygame.display.update () # updates the display window and redraws all the default windows
Note:
When you want to use pygame to make game code, you usually use Pycharm to write code instead of python.
When you want to add some events, add the corresponding code directly in the for loop.
Be sure to configure environment variables when programming with Pycharm, otherwise an error will run.
When programming with Pycharm, you can also use methods such as classes, inheritance, polymorphism, and so on.
At this point, I believe you have a deeper understanding of "how to use the minimum development framework of Pygame based on python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.