利用pygame制作魔方的平面展示
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用pygame制作魔方的平面展示相关的知识,希望对你有一定的参考价值。
为了更方便地验证我们魔方变换代码的准确性,特地利用pyggame设置一个简单的平面展示,初步展示我们的魔方操作
利用pygame模块的函数制作界面
screen = pygame.display.set_mode((400,480)) //创建一个宽度400、高度480的窗口
pygame.draw.rect(screen,(255,255,255),(x,y,width,length),3) //用白色(255,255,255)填充矩形的外边框,3代表边框的宽度
pygame.draw.rect(screen,color,(x,y,34,34),0) //当设置为0时代表矩形全填充,34=40-3*2,这样做是因为做出来的界面好看一点,白边更方便我们定位魔方的四个格子
全部代码如下:
1 import pygame 2 from pygame.locals import * 3 4 blue = 114,187,255 5 white = 230,230,255 6 yellow = 175,232,65 7 red = 178,71,61 8 orange = 255,129,66 9 green = 0,203,40 10 11 colors = [red,yellow,blue,white,green,orange] 12 13 def initialize(screen,x,y): 14 pygame.draw.rect(screen,(255,255,255),(x,y,40,40),3) 15 pygame.draw.rect(screen,(255,255,255),(x+40,y,40,40),3) 16 pygame.draw.rect(screen,(255,255,255),(x,y+40,40,40),3) 17 pygame.draw.rect(screen,(255,255,255),(x+40,y+40,40,40),3) 18 19 def color_side(color,x,y,z,screen): 20 if(z==0 or z==1 or z==5): 21 pygame.draw.rect(screen,colors[color[0]],(x,y,34,34),0) 22 pygame.draw.rect(screen,colors[color[1]],(x+40,y,34,34),0) 23 pygame.draw.rect(screen,colors[color[3]],(x,y+40,34,34),0) 24 pygame.draw.rect(screen,colors[color[2]],(x+40,y+40,34,34),0) 25 elif(z==2): 26 pygame.draw.rect(screen,colors[color[1]],(x,y,34,34),0) 27 pygame.draw.rect(screen,colors[color[2]],(x+40,y,34,34),0) 28 pygame.draw.rect(screen,colors[color[0]],(x,y+40,34,34),0) 29 pygame.draw.rect(screen,colors[color[3]],(x+40,y+40,34,34),0) 30 elif(z==3): 31 pygame.draw.rect(screen,colors[color[2]],(x,y,34,34),0) 32 pygame.draw.rect(screen,colors[color[3]],(x+40,y,34,34),0) 33 pygame.draw.rect(screen,colors[color[1]],(x,y+40,34,34),0) 34 pygame.draw.rect(screen,colors[color[0]],(x+40,y+40,34,34),0) 35 elif(z==4): 36 pygame.draw.rect(screen,colors[color[3]],(x,y,34,34),0) 37 pygame.draw.rect(screen,colors[color[0]],(x+40,y,34,34),0) 38 pygame.draw.rect(screen,colors[color[2]],(x,y+40,34,34),0) 39 pygame.draw.rect(screen,colors[color[1]],(x+40,y+40,34,34),0) 40 41 def color_update(matrix,screen): 42 color_side(matrix[0],163,163,0,screen) 43 color_side(matrix[1],163,243,1,screen) 44 color_side(matrix[2],243,163,2,screen) 45 color_side(matrix[3],163,83,3,screen) 46 color_side(matrix[4],83,163,4,screen) 47 color_side(matrix[5],163,323,5,screen) 48 49 def paint(matrix): 50 screen = pygame.display.set_mode((400,480)) 51 screen.fill((155,155,155)) 52 initialize(screen,160,160) 53 initialize(screen,160,240) 54 initialize(screen,240,160) 55 initialize(screen,160,80) 56 initialize(screen,80,160) 57 initialize(screen,160,320) 58 color_update(matrix,screen) 59 pygame.display.update() 60 61 paint([[0,0,0,0],[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4],[5,5,5,5]])
最终得到结果如下:
以上是关于利用pygame制作魔方的平面展示的主要内容,如果未能解决你的问题,请参考以下文章
有没有办法用 MOUSEBUTTONDOWN 事件关闭 pygame 窗口?
Python游戏开发,pygame模块,Python实现打砖块小游戏