我的目标是创建一个类似于以下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
总结一下。我希望不同的“分类”(见数据)显示在网格中,就像开始时的图片一样。
感谢您的帮助
汤姆
答案 0 :(得分:0)
这是一些代码,
这是除最多10个字符之外的代码(注释和删除除外),但会导致大更改。
import pygame,csv
def disp(phrase,loc,screen):
s = font.render(phrase, True, (255,255,255))
screen.blit(s, loc)
# not needed
def text_display_csv(filename,surface):
'''Display .csv file contents on surface'''
f = csv.reader(open(filename))
for row in f:
y = f.line_num
for x,item in enumerate(row):
disp(item, (60*x+10,12*y-2), surface) # changed y distance
pygame.init()
pygame.font.init()
font = pygame.font.SysFont("Courier",10) # Changed font size
screen = pygame.display.set_mode((640,480)) # changed window size
filename = "Flint catalogue v1.7 4 Feb 13 2013.csv"
text_display_csv(filename,screen)
pygame.display.update()
# 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
更新1 好的我明白了。 因此,您希望在Grid no S中显示P中的数据...
这也是代码。只需将text_display_csv
更改为此
def text_display_csv(filename,surface):
'''Display .csv file contents on surface, after processing'''
f = csv.reader(open(filename))
# The Processing
data = {}
for row in f:
y = f.line_num
if len(row) >= 19:
# See if S contains a number,
# if No: Print the msg
# else: add to dict, in form of S:P
try:
val = int(row[18]) # S is the 19th column
except ValueError:
print "Skipped row no",y,"as column S doesn't contain number"
continue
else: # if a number, add to the dict
data[val] = row[15] # P is the 16th column
print "File Loaded"
# the display part
width = 6
for loc in data:
item = data[loc] # get item (string to display)
y = loc//width # column
x = loc % width # row
disp(item, (60*x +10,30*y +10), surface)
这里发生了什么:
.csv
S
列,则为
S
的值是一个数字,请获取它的整数值
然后以S : P
的形式将信息添加到字典中
S
是整数P
列P
的字符串/值。现在唯一的问题是数字必须从零开始 要解决这个问题,请添加
loc += 1
之前
y = loc//width
您可以使用pygame.draw.line
绘制网格线,您可以解决这个问题。