Common methods of Pygame

''' 
import pygame

# Initialize the pygame library and prepare the computer hardware for
pygame.init()


# ---------- Window related operations -----------
# Create a window
window = pygame .display.set_mode([window width, window height])

# set the window title
pygame.display.set_caption("window title")

# load the resource image and return the image object
image = pygame.image.load("res/game.ico ")
# Set the window icon
pygame.display.set_icon(image)


# Specify the coordinates and draw the picture to the window
window.blit(image, (0, 0))


# ----------Image related operations- ----------
# Load the image file and return the image object
image = pygame.image.load("image path")

# Get the image rectangle object -> Rect(x, y, width, height)
# Default In this case, the coordinates of the upper left corner are (0, 0)
rect = image.get_rect(centerx=x, centery=y)


# On the basis of the original position, move the specified offset (x, y increase)
rect.move_ip(num1, num2)


# Determine whether the two rectangles intersect, and the intersection returns True, otherwise returns False
flag = pygame.Rect.colliderect(rect1, rect2)

# Scale the image object according to the specified width and height, and return a new image object
trans_image = pygame.transform.scale( image, (WINDOWWIDTH, WINDOWHEIGHT))


# ----------Event related operations-----------
# Common event types:
# QUIT closes the window
# KEYDOWN keyboard key
# Get all the current Keep pressing bools_tuple


# Get a list of all events
event_list = pygame.event.get()

for event in event_list:
# 1. Mouse click to close the window event
if event.type == pygame.QUIT:
print("Close the window")
sys .exit()

# 2. Keyboard press event
if event.type == pygame.KEYDOWN:

# Determine whether the key pressed by the user is the a key
if event.key == pygame.K_a:
print("Pressed a ")

if event.key == pygame.k_UP:
print("Press the up arrow key")


# 3. Get the status of all keys on the current keyboard (pressed, not pressed), return a bool tuple
pressed_keys = pygame.key.get_pressed ()
(0, 0, 0, 0, 1, 0, 0, 0, 0)

if pressed_keys[pygame.K_w] or pressed_keys[pygame.K_UP]:
print("W key is pressed, or the arrow key is up")


# ----------Sound related operations -----------
# Load background music
pygame.mixer.music.load("./res/bg2.ogg")
# Loop playback Background music
pygame.mixer.music.play(-1)
# Stop background music
pygame.mixer.music.stop()

# Load sound effects
boom_sound = pygame.mixer.Sound("./res/baozha.ogg")
# Play sound effects
boom_sound.play()

boom_sound.stop()


Three primary colors: Red Green Blue

0 ~ 255

# -------- Text display operation

# Set font and size
font = pygame.font.SysFont('SimHei', 42)

# render(text(text content), antialias(antialiasing), color(RGB)), return text object
textobj = font.render("Airplane Wars", 1 , (255, 255, 255))

# Set the text rectangle object position
textrect = textobj.get_rect(centerx=300, centery=300)

# Draw the specified text object at the specified position
window.blit(textobj, textrect)
'''

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325870836&siteId=291194637