First, create the basic structure
The code is as follows:
import time
import pygame
def main() :
Initialize PyGame
pygame.init()
Create an object to display the image.
screen = pygame.display.set_mode((750.667))
# main loop
while True:
time.sleep(0.1)
# Display the contents of the picture frame (now the contents of the picture frame like photos, text, etc.)
pygame.display.update()
if __name__ == '__main__':
main()
Copy the code
Operation effect:
Second, display background
1. Add images
Click the address below to download the material
www.itprojects.cn/58.html
2. Display an image
The following code
import time
import pygame
def main() :
Initialize PyGame
pygame.init()
Create an object to display the image.
screen = pygame.display.set_mode((750.667))
# Game background images
background_img = pygame.image.load("images/bg.jpg")
# main loop
while True:
# Display the game background
screen.blit(background_img, (0.0))
time.sleep(0.1)
# Display the contents of the picture frame (now the contents of the picture frame like photos, text, etc.)
pygame.display.update()
if __name__ == '__main__':
main()
Copy the code
Running effect
3. Create an overall background with multiple images
import time
import pygame
def main() :
Initialize PyGame
pygame.init()
Create an object to display the image.
screen = pygame.display.set_mode((750.667))
# Game background images
background_img = pygame.image.load("images/bg.jpg")
# main loop
while True:
# Display the game background
screen.blit(background_img, (0.0))
screen.blit(background_img, (0.270))
screen.blit(background_img, (0.540))
time.sleep(0.1)
# Display the contents of the picture frame (now the contents of the picture frame like photos, text, etc.)
pygame.display.update()
if __name__ == '__main__':
main()
Copy the code
Three, the mouse close the window
import sys
import time
import pygame
def main() :
Initialize PyGame
pygame.init()
Create an object to display the image.
screen = pygame.display.set_mode((750.667))
# Game background images
background_img = pygame.image.load("images/bg.jpg")
# main loop
while True:
# Event detection (e.g. keyboard, mouse, etc.)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit() # exit program
# Display the game background
screen.blit(background_img, (0.0))
screen.blit(background_img, (0.270))
screen.blit(background_img, (0.540))
time.sleep(0.1)
# Display the contents of the picture frame (now the contents of the picture frame like photos, text, etc.)
pygame.display.update()
if __name__ == '__main__':
main()
Copy the code
Running effect
Display the checkerboard
code
import sys
import time
import pygame
def main() :
Initialize PyGame
pygame.init()
Create an object to display the image.
screen = pygame.display.set_mode((750.667))
# Game background images
background_img = pygame.image.load("images/bg.jpg")
# Game board
chessboard_img = pygame.image.load("images/bg.png")
# main loop
while True:
# Event detection (e.g. keyboard, mouse, etc.)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit() # exit program
# Display the game background
screen.blit(background_img, (0.0))
screen.blit(background_img, (0.270))
screen.blit(background_img, (0.540))
# display checkerboard
screen.blit(chessboard_img, (50.50))
time.sleep(0.1)
# Display the contents of the picture frame (now the contents of the picture frame like photos, text, etc.)
pygame.display.update()
if __name__ == '__main__':
main()
Copy the code
Running effect
Five, display chess pieces
code
import sys
import time
import pygame
def main() :
Initialize PyGame
pygame.init()
Create an object to display the image.
screen = pygame.display.set_mode((750.667))
# Game background images
background_img = pygame.image.load("images/bg.jpg")
# Game board
chessboard_img = pygame.image.load("images/bg.png")
# pieces
chessboard_map = [
["b_c"."b_m"."b_x"."b_s"."b_j"."b_s"."b_x"."b_m"."b_c"],
[""."".""."".""."".""."".""],
[""."b_p".""."".""."".""."b_p".""],
["b_z".""."b_z".""."b_z".""."b_z".""."b_z"],
[""."".""."".""."".""."".""],
[""."".""."".""."".""."".""],
["r_z".""."r_z".""."r_z".""."r_z".""."r_z"],
[""."r_p".""."".""."".""."r_p".""],
[""."".""."".""."".""."".""],
["r_c"."r_m"."r_x"."r_s"."r_j"."r_s"."r_x"."r_m"."r_c"]]for row, line in enumerate(chessboard_map):
for col, chess_name in enumerate(line):
if chess_name:
# Add the created chess piece to the property map
chessboard_map[row][col] = [pygame.image.load("images/" + chess_name + ".png"), (50 + col * 57.50 + row * 57)]
else:
chessboard_map[row][col] = None
# main loop
while True:
# Event detection (e.g. keyboard, mouse, etc.)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit() # exit program
# Display the game background
screen.blit(background_img, (0.0))
screen.blit(background_img, (0.270))
screen.blit(background_img, (0.540))
# display checkerboard
screen.blit(chessboard_img, (50.50))
Display all the pieces on the board
for line_chess in chessboard_map:
for chess in line_chess:
if chess:
screen.blit(chess[0], chess[1])
time.sleep(0.1)
# Display the contents of the picture frame (now the contents of the picture frame like photos, text, etc.)
pygame.display.update()
if __name__ == '__main__':
main()
Copy the code
Running effect
More steps
For more steps, visit book.itprojects.cn/01-b29fd8ca…