我的目标是创建一个类似于以下http://i49.tinypic.com/f39hxv.png的网格,其中每个数字代表在每个正方形中找到的“准备薄片”的数量(请参见下面的数据片段)
到目前为止的代码如下:
import csv
import os
import pygame
# the functions for display
def disp(phrase,loc,screen): # function to display phrase at loc on surface.
s = font.render(phrase, True, (255,255,255))
screen.blit(s, loc) #
def text_display_csv(filename,surface):
'''Display .csv file contents on surface'''
f = csv.reader(open("Flint catalogue v1.7 4 Feb 13 2013.csv")) # open the csv file in one line!
for row in f:
y = f.line_num # assign y as Row no.
for x,item in enumerate(row): # Get column number(x) and item
disp(item, (64*x+10,64*y+10), surface) # display item
pygame.init()
pygame.font.init()
font = pygame.font.SysFont("Courier",10) # font initialisation
screen = pygame.display.set_mode((1200,1200))
filename = 'the .csv file'
text_display_csv(filename,screen) # text is displayed
pygame.display.update() # screen updated
# Main loop, does nothing! :)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
break
if not running:
break
我希望网格看起来有点像这样(但我的麻烦是将数据库中的数据放入这个数组):
import csv
import os
import pygame
# Define some colors
black = ( 0, 0, 0)
white = ( 255, 255, 255)
green = ( 0, 255, 0)
red = ( 255, 0, 0)
# This sets the width and height of each grid location
width=20
height=20
# This sets the margin between each cell
margin=5
# Create a 2 dimensional array. A two dimesional
# array is simply a list of lists.
grid=[]
for row in range(10):
# Add an empty array that will hold each cell
# in this row
grid.append([])
for column in range(10):
grid[row].append(0) # Append a cell
# Set row 1, cell 5 to one. (Remember rows and
# column numbers start at zero.)
grid[1][5] = 1
grid[1][6] = 2
# Initialize pygame
pygame.init()
# Set the height and width of the screen
size=[255,255]
screen=pygame.display.set_mode(size)
# -------- Main Program Loop -----------
while done==False:
# Set the screen background
screen.fill(black)
# Draw the grid
for row in range(10):
for column in range(10):
color = white
if row[4]in c == 1:
color = green
pygame.draw.rect(screen,color,[(margin+width)*column+margin, (margin+height)*row+margin,width,height])
# Limit to 20 frames per second
clock.tick(20)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
pygame.quit()
数据如下:
http://tinypic.com/view.php?pic=2rdgn02&s=6
所以总结一下。我希望在网格中显示不同的“分类”(见数据),就像开始时的图片一样。
谢谢您的帮助
汤姆