美文网首页Pygame101
001 Memory Puzzle - step2 绘制图形

001 Memory Puzzle - step2 绘制图形

作者: 爱学习的洋仔 | 来源:发表于2018-06-13 16:04 被阅读0次

    在游戏中, 图形使用pygame的画图功能实现, 总共有5种图形(甜甜圈, 菱形, 正方形, 线条, 和椭圆形), 有7种颜色(最多一共有70种组合)


    (1) 首先, 我们看一下图形绘制的方法, 使用的是pygame的图形绘制函数, 方法如下:
    # syntax suger
    quarter = int(boxsize * 0.25); half = int(boxsize * 0.5)
    
    # 绘制甜甜圈形状
    pygame.draw.circle(displaysurf, RED, (left+half, top+half), half-5)
    pygame.draw.circle(displaysurf, BGCOLOR, (left+half, top+half), quarter-5)
    
    # 绘制菱形
    pygame.draw.polygon(displaysurf, GREEN, ((left + half, top),
                                                     (left + boxsize - 1, top + half),
                                                     (left + half, top + boxsize - 1),
                                                     (left, top + half)))
    # 绘制正方形
    pygame.draw.rect(displaysurf, YELLOW, (left + quarter, top + quarter,
                                                  boxsize - half, boxsize - half))
    
    # 绘制线条
    for i in range(0, boxsize, 4):
        pygame.draw.line(displaysurf, BLUE, (left, top+i), (left + i, top))
        pygame.draw.line(displaysurf, BLUE, (left+i, top+boxsize-1),
                                 (left+boxsize-1, top+i))
    
    # 绘制椭圆形
    pygame.draw.ellipse(displaysurf, PUPPLE, (left, top + quarter, boxsize, half))
    
    
    (2) 然后, 我们使用这些绘制方法, 绘制所有的图形

    全部代码(main.py)

    # Memory Puzzle
    import random, pygame, sys
    from pygame.locals import *
    
    # 配置基础 ------------------------------------------------------------
    FPS = 30                    # 设置帧数为30
    WINDOW_WIDTH = 640          # 窗口宽度640像素
    WINDOW_HEIGHT = 480         # 窗口告诉480像素
    
    # 游戏中需要用到的颜色设置 -----------------------------------------------
    # R G B 颜色
    GRAY = (100,100,100)
    NAVYBLUE = (60,60,100)
    WHITE = (255,255,255)
    RED = (255,0,0)
    GREEN = (0,255,0)
    BLUE = (0,0,255)
    YELLOW = (255,255,0)
    ORANGE = (255,128,0)
    PUPPLE = (255,0,255)
    CYAN = (0,255,255)
    BGCOLOR = NAVYBLUE # 背景色设置为navyblue
    
    
    # 定义main函数 ---------------------------------------------------------
    def main():
        pygame.init()                           # pygame初始化
        fpsclock = pygame.time.Clock()
        displaysurf = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
        pygame.display.set_caption("Memory Game")   # 设置窗口标题
        displaysurf.fill(BGCOLOR)
        while True:                                 # 游戏主题循环
            displaysurf.fill(BGCOLOR)              # 用背景色填充窗口
            for event in pygame.event.get():        # 获取游戏事件
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
    
    # 下面的代码用来展示如何绘制图形 -----------------------------------------------------------------------------
            left = 50;      # 起始的绘制位置
            top = 50;       # 距离顶部50像素,距离左侧50像素
            boxsize = 40;   # 将要绘制的图形限制在一个40*40像素的范围内
            quarter = int(boxsize * 0.25);      # syntax suger
            half = int(boxsize * 0.5)
    
            # 绘制圆形
            pygame.draw.circle(displaysurf, RED, (left + half, top + half), half - 5)
            pygame.draw.circle(displaysurf, BGCOLOR, (left + half, top + half), quarter - 5)
    
            # 右移50像素绘制菱形
            left += 50
            pygame.draw.polygon(displaysurf, GREEN, ((left + half, top),
                                                     (left + boxsize - 1, top + half),
                                                     (left + half, top + boxsize - 1),
                                                     (left, top + half)))
    
            # 右移50像素绘制方形
            left += 50
            pygame.draw.rect(displaysurf, YELLOW, (left + quarter, top + quarter,
                                                   boxsize - half, boxsize - half))
    
            # 右移50像素绘制斜线方形
            left += 50
            for i in range(0, boxsize, 4):
                pygame.draw.line(displaysurf, BLUE, (left, top + i), (left + i, top))
                pygame.draw.line(displaysurf, BLUE, (left + i, top + boxsize - 1),
                                 (left + boxsize - 1, top + i))
    
            # 右移动50像素绘制椭圆形
            left += 50
            pygame.draw.ellipse(displaysurf, PUPPLE, (left, top + quarter, boxsize, half))
    
    # ------------------------------------------------------------------------------------------------------
            pygame.display.update()
            fpsclock.tick(FPS)
    
    
    if __name__ == '__main__':
        main()
    

    运行程序, 结果如下:
    ScreenClip [3].png

    Memory Puzzle下一节:绘制一组图形

    相关文章

      网友评论

        本文标题:001 Memory Puzzle - step2 绘制图形

        本文链接:https://www.haomeiwen.com/subject/ajgjeftx.html