Pygame Tutorial: Creating an Interactive OSI Model Visualization
This code creates a basic interactive visualization of the OSI model using Pygame. The user can use the left and right arrow keys to switch between two different views of the OSI model (regular and zoomed in). Here's a breakdown of the code and how it works:
import pygame
from pygame.locals import (
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_ESCAPE,
K_SPACE,
KEYDOWN,
KEYUP,
QUIT,
)
import sys
import time
class Game():
def __init__(self):
pygame.init()
pygame.display.set_caption('OSI&TCP')
self.row = 0
self.switch_cover = 0
self.screen = pygame.display.set_mode((800, 600))
self.clock = pygame.time.Clock()
self.count = [False, False]
self.OSI = pygame.image.load('C:/Users/HUAWEI/Desktop/计算机/pygame/picture/OSI.png').convert()
self.OSI.scroll(227, 529)
self.OSI_boost = pygame.transform.rotozoom(self.OSI, 0, 1.5)
def cover(self):
if self.count[1] - self.count[0] != 0:
self.switch_cover += 1
time.sleep(0.4)
if self.switch_cover == 1:
self.screen.blit(self.OSI, (0, 0))
if self.switch_cover == 0:
self.screen.blit(self.OSI_boost, (0, 0))
self.switch_cover = 0 # Reset switch_cover
def run(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_LEFT:
self.count[0] = True
if event.key == K_RIGHT:
self.count[1] = True
if event.type == KEYUP:
if event.key == K_LEFT:
self.count[0] = False
if event.key == K_RIGHT:
self.count[1] = False
self.screen.fill((0, 0, 0))
self.cover()
pygame.display.flip()
self.clock.tick(10)
game = Game()
game.run()
Explanation:
-
Import Necessary Modules:
pygame: The main Pygame library.pygame.locals: Constants for keyboard keys and events.sys: For exiting the program.time: For pausing the execution.
-
GameClass:-
__init__:- Initializes Pygame (
pygame.init()). - Sets the window title (
pygame.display.set_caption()). - Creates a game window (
pygame.display.set_mode()). - Sets up a clock for controlling frame rate (
pygame.time.Clock()). - Loads the OSI image and creates a zoomed version.
- Initializes Pygame (
-
cover:- Checks for key presses and updates
switch_coverto control which image is displayed. - Blit (draw) the appropriate image onto the screen.
- Resets
switch_coverafter displaying the zoomed image.
- Checks for key presses and updates
-
run:- The main game loop.
- Handles events (keyboard presses, window closing).
- Clears the screen (
self.screen.fill()). - Calls
self.cover()to update the displayed image. - Updates the display (
pygame.display.flip()). - Controls the frame rate (
self.clock.tick()).
-
-
Game Instantiation and Execution:
- Creates an instance of the
Gameclass. - Starts the game by calling
game.run().
- Creates an instance of the
Key Improvements:
- Correct Method Calling:
self.cover()is now called correctly. - Event Handling:
cover()is called outside the event loop, preventing flickering. - Path Handling: The image path is still specific to your system; consider using a relative path or storing the image in the same directory as the script.
- Switch Cover Reset:
switch_coveris reset to 0 after displaying the zoomed image.
This code provides a basic framework for creating interactive visualizations with Pygame. You can build upon it by adding more features like user input, game logic, and more complex animations. Remember to adapt the image paths and adjust the code to fit your specific needs.
原文地址: https://www.cveoy.top/t/topic/E5q 著作权归作者所有。请勿转载和采集!