参考:[https://gitbook.cn/gitchat/column/5b1a31bc862a01660e35955c/topic/5b1a6d8e3d723c686c30f85e]
2019-06-17
任务:绘制游戏区域(1.绘制一条线;2.绘制边界线;3.绘制网格线)
最终效果图:

注意:每个小方格都是40×40像素,游戏区域为宽10×方格,高20×方格。
步骤开始:
1.绘制一条线:
...#与第一节内容相同
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(bg_color)
draw_game_area(screen)
pygame.display.flip()
def draw_game_area(screen):
pygame.draw.line(screen, (0, 0, 0), (100, 100), (200, 200))
if __name__ =='__main__':
main()
draw_line()函数应该在flip()之前,否则线无法显示;
pygame.draw.line()是画线,有四个参数:第一个是在screen上画线,第二个是颜色(黑色),第三与第四分别是画线起点、终点。
效果图:

2.绘制边界线
最终效果如下:

显然,需要绘制四条线,上边下边左边右边。标定四个点,我们只需要知道四个各自的坐标,即可用 pygame.draw.line()画出这四条线。
给定几个常量:
SCREEN_WIDTH, SCREEN_HEIGHT = 1200, 900 窗口的宽度与高度;
CELL_WIDTH = 40 每个方格40像素;
GAME_AREA_WIDTH, GAME_AREA_HEIGHT = CELL_WIDTH * 10, CELL_WIDTH * 20 游戏区域宽10×方格,高20×方格;
GAME_AREA_LEFT = (SCREEN_WIDTH - GAME_AREA_WIDTH) // 2 游戏区域左侧空白宽度为整个窗口宽度减去游戏区域宽度,并除以2;
GAME_AREA_TOP = SCREEN_HEIGHT - GAME_AREA_HEIGHT 游戏区域上边空白高度为整个窗口高度减去游戏区域高度;
EDGE_COLOR = (0, 0, 0) 边界线颜色黑色;
BG_COLOR = (230, 230, 230) 背景颜色自定义;
以上各常量定义在def main():之前。
由以上常量定义可得上图中点1坐标(GAME_AREA_LEFT, GAME_AREA_TOP)点2坐标(GAME_AREA_LEFT + GAME_AREA_WIDTH, GAME_AREA_TOP)点3坐标(GAME_AREA_LEFT, SCREEN_HEIGHT)点4坐标(GAME_AREA_LEFT + GAME_AREA_WIDTH, SCREEN_HEIGHT)
所以四条线就可以这样画:(定义在draw_game_area()中)
def draw_game_area(screen):
# 上边线
pygame.draw.line(screen, EDGE_COLOR, (GAME_AREA_LEFT, GAME_AREA_TOP), (GAME_AREA_LEFT + GAME_AREA_WIDTH, GAME_AREA_TOP))
# 下边线
pygame.draw.line(screen, EDGE_COLOR, (GAME_AREA_LEFT, SCREEN_HEIGHT), (GAME_AREA_LEFT + GAME_AREA_WIDTH, SCREEN_HEIGHT))
# 左边线
pygame.draw.line(screen, EDGE_COLOR, (GAME_AREA_LEFT, GAME_AREA_TOP), (GAME_AREA_LEFT, SCREEN_HEIGHT))
# 右边线
pygame.draw.line(screen, EDGE_COLOR, (GAME_AREA_LEFT + GAME_AREA_WIDTH, GAME_AREA_TOP), (GAME_AREA_LEFT + GAME_AREA_WIDTH, SCREEN_HEIGHT))
3.绘制游戏区域网格线

我们可以采用for循环画网格,需要在draw_game_area(screen)下一行再定义一个函数draw_grid_line(screen)
定义函数:
def draw_grid_line(screen):
for i in range(1, 10):
pygame.draw.line(screen, EDGE_COLOR, (GAME_AREA_LEFT + CELL_WIDTH * i, GAME_AREA_TOP), (GAME_AREA_LEFT + CELL_WIDTH * i, SCREEN_HEIGHT))
for j in range(1, 21):
pygame.draw.line(screen, EDGE_COLOR, (GAME_AREA_LEFT, GAME_AREA_TOP + CELL_WIDTH * j), (GAME_AREA_LEFT + GAME_AREA_WIDTH, GAME_AREA_TOP + CELL_WIDTH * j))
截至目前所有代码:
import sys
import pygame
SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 830
CELL_WIDTH = 40
GAME_AREA_WIDTH = CELL_WIDTH * 10
GAME_AREA_HEIGHT = CELL_WIDTH * 20
GAME_AREA_TOP = SCREEN_HEIGHT - GAME_AREA_HEIGHT
GAME_AREA_LEFT = (SCREEN_WIDTH - GAME_AREA_WIDTH) // 2
EDGE_COLOR = (0, 0, 0)
BG_COLOR = (230, 230, 230)
def main():
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("俄罗斯方块")
bg_color = BG_COLOR
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(bg_color)
draw_game_area(screen)
draw_grid_line(screen)
pygame.display.flip()
def draw_game_area(screen): # (400, 30) (800, 30)
pygame.draw.line(screen, EDGE_COLOR, (GAME_AREA_LEFT, GAME_AREA_TOP), (GAME_AREA_LEFT + GAME_AREA_WIDTH, GAME_AREA_TOP))
# (400, 830) (800, 830)
pygame.draw.line(screen, EDGE_COLOR, (GAME_AREA_LEFT, SCREEN_HEIGHT), (GAME_AREA_LEFT + GAME_AREA_WIDTH, SCREEN_HEIGHT))
# (400, 30) (400, 830)
pygame.draw.line(screen, EDGE_COLOR, (GAME_AREA_LEFT, GAME_AREA_TOP), (GAME_AREA_LEFT, SCREEN_HEIGHT))
# (800, 30) (800, 830)
pygame.draw.line(screen, EDGE_COLOR, (GAME_AREA_LEFT + GAME_AREA_WIDTH, GAME_AREA_TOP), (GAME_AREA_LEFT + GAME_AREA_WIDTH, SCREEN_HEIGHT))
def draw_grid_line(screen):
for i in range(1, 10):
pygame.draw.line(screen, EDGE_COLOR, (GAME_AREA_LEFT + CELL_WIDTH * i, GAME_AREA_TOP), (GAME_AREA_LEFT + CELL_WIDTH * i, SCREEN_HEIGHT))
for j in range(1, 21):
pygame.draw.line(screen, EDGE_COLOR, (GAME_AREA_LEFT, GAME_AREA_TOP + CELL_WIDTH * j), (GAME_AREA_LEFT + GAME_AREA_WIDTH, GAME_AREA_TOP + CELL_WIDTH * j))
if __name__ == '__main__':
main()