性能 1:实现游戏整体界面显示
一、创立根本的构造
代码如下:
import time
import pygame
def main():
# 初始化 pygame
pygame.init()
# 创立用来显示画面的对象(了解为相框)screen = pygame.display.set_mode((750, 667))
# 主循环
while True:
time.sleep(0.1)
# 显示 screen 这个相框的内容(此时在这个相框中的内容像照片、文字等会显示进去)pygame.display.update()
if __name__ == '__main__':
main()
运行成果:
二、显示背景
1. 增加图片
点击如下地址下载素材
https://www.itprojects.cn/58.html
2. 显示一张图片
代码如下
import time
import pygame
def main():
# 初始化 pygame
pygame.init()
# 创立用来显示画面的对象(了解为相框)screen = pygame.display.set_mode((750, 667))
# 游戏背景图片
background_img = pygame.image.load("images/bg.jpg")
# 主循环
while True:
# 显示游戏背景
screen.blit(background_img, (0, 0))
time.sleep(0.1)
# 显示 screen 这个相框的内容(此时在这个相框中的内容像照片、文字等会显示进去)pygame.display.update()
if __name__ == '__main__':
main()
运行成果
3. 通过多张图片实现整体背景
import time
import pygame
def main():
# 初始化 pygame
pygame.init()
# 创立用来显示画面的对象(了解为相框)screen = pygame.display.set_mode((750, 667))
# 游戏背景图片
background_img = pygame.image.load("images/bg.jpg")
# 主循环
while True:
# 显示游戏背景
screen.blit(background_img, (0, 0))
screen.blit(background_img, (0, 270))
screen.blit(background_img, (0, 540))
time.sleep(0.1)
# 显示 screen 这个相框的内容(此时在这个相框中的内容像照片、文字等会显示进去)pygame.display.update()
if __name__ == '__main__':
main()
三、鼠标敞开窗口
import sys
import time
import pygame
def main():
# 初始化 pygame
pygame.init()
# 创立用来显示画面的对象(了解为相框)screen = pygame.display.set_mode((750, 667))
# 游戏背景图片
background_img = pygame.image.load("images/bg.jpg")
# 主循环
while True:
# 事件检测(例如点击了键盘、鼠标等)for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit() # 退出程序
# 显示游戏背景
screen.blit(background_img, (0, 0))
screen.blit(background_img, (0, 270))
screen.blit(background_img, (0, 540))
time.sleep(0.1)
# 显示 screen 这个相框的内容(此时在这个相框中的内容像照片、文字等会显示进去)pygame.display.update()
if __name__ == '__main__':
main()
运行成果
四、显示棋盘
代码
import sys
import time
import pygame
def main():
# 初始化 pygame
pygame.init()
# 创立用来显示画面的对象(了解为相框)screen = pygame.display.set_mode((750, 667))
# 游戏背景图片
background_img = pygame.image.load("images/bg.jpg")
# 游戏棋盘
chessboard_img = pygame.image.load("images/bg.png")
# 主循环
while True:
# 事件检测(例如点击了键盘、鼠标等)for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit() # 退出程序
# 显示游戏背景
screen.blit(background_img, (0, 0))
screen.blit(background_img, (0, 270))
screen.blit(background_img, (0, 540))
# 显示棋盘
screen.blit(chessboard_img, (50, 50))
time.sleep(0.1)
# 显示 screen 这个相框的内容(此时在这个相框中的内容像照片、文字等会显示进去)pygame.display.update()
if __name__ == '__main__':
main()
运行成果
五、显示棋子
代码
import sys
import time
import pygame
def main():
# 初始化 pygame
pygame.init()
# 创立用来显示画面的对象(了解为相框)screen = pygame.display.set_mode((750, 667))
# 游戏背景图片
background_img = pygame.image.load("images/bg.jpg")
# 游戏棋盘
chessboard_img = pygame.image.load("images/bg.png")
# 棋子
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:
# 将创立的棋子增加到属性 map 中
chessboard_map[row][col] = [pygame.image.load("images/" + chess_name + ".png"), (50 + col * 57, 50 + row * 57)]
else:
chessboard_map[row][col] = None
# 主循环
while True:
# 事件检测(例如点击了键盘、鼠标等)for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit() # 退出程序
# 显示游戏背景
screen.blit(background_img, (0, 0))
screen.blit(background_img, (0, 270))
screen.blit(background_img, (0, 540))
# 显示棋盘
screen.blit(chessboard_img, (50, 50))
# 显示棋盘上的所有棋子
for line_chess in chessboard_map:
for chess in line_chess:
if chess:
screen.blit(chess[0], chess[1])
time.sleep(0.1)
# 显示 screen 这个相框的内容(此时在这个相框中的内容像照片、文字等会显示进去)pygame.display.update()
if __name__ == '__main__':
main()
运行成果
更多步骤
更多步骤请拜访 https://book.itprojects.cn/01-b29fd8caf9b9ad57558c3a7c84c3de97/index.html