0

我的目标是创建一个类似于以下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

所以总结一下。我希望在网格中显示不同的“分类”(见数据),就像开始时的图片一样。

谢谢您的帮助

汤姆

4

1 回答 1

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 好的,我明白了。因此,您想在 P 中的 Grid no S 处显示数据...

这也是它的代码。改成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)

这里发生了什么事:

  1. 加载.csv
  2. 如果一行至少有S列的项目,
    • 如果值 atS是一个数字,则获取它的整数值。
      然后将信息以 的形式添加到字典中S : P,其中
      • S是整数
      • P列的字符串/值P
    • 否则打印一条消息,然后继续下一行(如果有)
  3. 在各自的位置显示数据。

现在唯一的问题是数字必须从零开始。
为了克服这个问题,只需添加

loc += 1

y = loc//width

您的网格线可以使用 绘制pygame.draw.line,您可以弄清楚。

于 2013-02-06T14:01:27.467 回答