Python 创建一维和二维图像

标签 python image dimension

下面的代码是提供给我们的,它基本上打印了第二个函数中显示的图像:

import sys



# CONSTANTS
MIN_ROW = 0
MAX_ROW = 9
MIN_COLUMN = 0
MAX_COLUMN = 9
WALL = "#"
BUILDING = "b"
BUSH = "u"
PLAYER = "@"
EMPTY = " "
STAIRS = "X"


def display (city):
   r = 0
   c = 0
   print("CITY LEVEL")
   for r in range (0, (MAX_ROW+1), 1):   #LOOPS1
      for c in range (0, (MAX_COLUMN+1), 1):
         sys.stdout.write(city[r][c])
      print()
   print()

def initialize ():
   r = 0
   c = 0
   city = []

   for r in range (0, (MAX_ROW+1), 1): #LOOP2
      city.append([])#appends an empty list to city
      for c in range (0, (MAX_COLUMN+1), 1):
       city[r].append(" ")
   #               0   1   2   3   4   5   6   7   8   9 
   city  [0] =   ["#","#","#","#","#","#","#","#","#","#"]
   city  [1] =   ["#","@"," "," "," "," "," "," ","u","#"]
   city  [2] =   ["#"," "," ","b","b"," "," "," ","X","#"]
   city  [3] =   ["#"," "," ","b","b"," "," "," "," ","#"]
   city  [4] =   ["#"," "," "," "," "," "," "," ","b","#"]
   city  [5] =   ["#","u"," ","u","u","u","u","u","u","#"]
   city  [6] =   ["#","b"," "," "," "," "," "," "," ","#"]
   city  [7] =   ["#"," "," "," "," ","b"," ","b"," ","#"]
   city  [8] =   ["#"," "," "," ","b"," "," "," "," ","#"]
   city  [9] =   ["#","#","#","#","#","#","#","#","#","#"]
   return city   


# MAIN
def main ():
     level = initialize ()
     display (level)





main ()

现在我试图为一维图片重现这一点,但由于某种原因,我遇到了第一个函数的 sys.stdout.write() 的类型错误。它似乎试图打印整个列表,而不是仅打印其中的一个字符。谁能帮我调试一下吗?另外有人可以告诉我上面标记为 LOOPS1 和 LOOPS2 的代码中的循环发生了什么

import sys


def display(track):
    c=0
    for c in range(0,20,1):
        sys.stdout.write(track[c])
    print()

def initialize():
    c=0
    track= []
    for c in range(0,20,1):
        track.append([])
        track[c].append(" ")
    track[0]= ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t"]
    return track

level= initialize()
display(level)

如果有任何不清楚的地方,请告诉我,我会尽快修复。

编辑:我的程序代码:

import sys
import random


# CONSTANTS


PLAYER = "@"
EMPTY = " "


#Takes the information from the function initialize() and displays it. Outputs the fitness simulation.
def display (track):
    r = 0
    c = 0

    print("\nTRACK")
    for r in range (0, (4), 1):
        for c in range (0, (41), 1):
            sys.stdout.write(track[r][c])
        print()
    print()

def speedDisplay(speed):
    options=["(w)alk","(j)og","(r)un","(f)ast run"]
    for o in range(0,speed,1):
        print(options[o],"\n")




def inputs():#ioerror here?
    values= set("wWjJrRfFlLsSeE")
    while True:
        move=input("\nPlease select the speed you would like to travel at from the options listed:")
        for m in move:
            if m not in values:
                print("\nInadmissable entry, Please only use inputs valid in the options above.")
                break
        else:
            break
    if move=="w" or move=="W":
        usedEnergy=0#turn into random functions later
    elif move=="j" or move=="J":
        usedEnergy=1
    elif move=="r" or move=="R":
        usedEnergy=2
    elif move=="f" or move=="F":
        usedEnergy=5
    return usedEnergy

def remainingEnergy(energy,usedEnergy):
    energy= energy-usedEnergy
    print("\nRemaining Energy:",energy,"\n")
    return energy

def amountLeft(energy):
# enter ioexception error here somewhere?


    while True: 
        if energy <0 or energy >20:
            print("error")
        elif energy>=5:
            speed=4
        elif energy <5 and energy >=2:
            speed=3
        elif energy <2 and energy >=1:
            speed=2
        elif energy <1 and energy >=0:
            speed=1
        else:
            break

        return speed











# This function is used to initialize the game track that will later be displayed. 
def initialize ():
    r = 0
    c = 0
    track = []
    #Creates each row and column. A "for" loop initiates which creates and appends an empty list to the list "track". Then, taking the current row into consideration, the respective number of columns are created via the inner "for loop and a space is appended to the end of the current row. The loop re-initiates and the process is repeated for all 4 required rows. This results in 4 rows and 41 coloumns.
    for r in range (0, (4), 1):
    #appends an empty list to track
        track.append([])
        for c in range (0, (41), 1):
    #appends a space to the current row
            track[r].append(" ")
    # the actual rows and columns are created below.
    #               0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y
    track  [0] =   [" ","0"," ","1"," ","2"," ","3"," ","4"," ","5"," ","6"," ","7"," ","8"," ","9"," ","A"," ","B"," ","C"," ","D"," ","E"," ","F"," ","G"," ","H"," ","I"," ","J"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "]
    track  [1] =   [" ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," "]
    track  [2] =   ["|","@","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"]
    track  [3] =   [" ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," "]
    return track   






def move (sRow, sCol, dRow, dCol, track):
    EMPTY= " "
    PLAYER= "@"
    DIVIDER= "|"
    track[sRow][sCol] = EMPTY
    track[dRow][dCol] = PLAYER






# MAIN
def main ():
    track = initialize ()
    display (track)
    print("\n(w)alk\n\n(j)og\n\n(r)un\n\n(f)ast run")
    usedEnergy=inputs()
    energy=20
    energy=remainingEnergy(energy,usedEnergy)
    while energy<20:
        usedEnergy=inputs()
        speed= amountLeft(energy)
        speedDisplay(speed)
        energy=remainingEnergy(energy,usedEnergy)



main ()

最佳答案

嗯,这有效:

import sys

def display(track):
    c=0
    for c in range(0,20,1):
        sys.stdout.write(track[c])
    print()

def initialize():
    c=0
    track = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t"]
    return track

level= initialize()
display(level)

您的问题是您处理track的方式不匹配。在initialize中,您将其创建为列表的列表,但在display中,您将其访问为就好像它只是一个字符列表。制作两个字符列表更简单,制作两个字符列表更通用(允许将相同的函数用于 1D 和 2D 情况)。

第一个循环发生了什么?

从第一个代码块中的display:

for r in range (0, (MAX_ROW+1), 1):   #LOOPS1
     for c in range (0, (MAX_COLUMN+1), 1):
         sys.stdout.write(city[r][c])
     print()
 print()

R 是行索引,范围为 0 到 MAX_ROW,0 - 9。C 是列索引,范围为 0 到 MAX_COLUMN,0 - 9。

sys.stdout.write(city[r][c]) 写出 city 的当前行和列(已设置为单个字符),不带换行符。

由于 print() 语句,您的代码可能是 Python 3。如果您提到或标记它可能会有所帮助。

我正在运行 Python 2.7,因此我将使用旧的打印语法,以便我可以测试我发布的代码。您可能必须将其恢复(添加括号)。

我会把它写成:

def display (city):
   print("CITY LEVEL")
   for row in city:
      for c in row:
         print c,
      print
   print

在 Python 2 中,打印末尾的逗号会抑制换行符。

第二个循环发生了什么?

从第一个代码块中的initialize:

for r in range (0, (MAX_ROW+1), 1):

r 将为 0 到 MAX_ROW,0-9

    city.append([])#appends an empty list to city

city 是一个列表。这会将一个空列表附加到城市。

    for c in range (0, (MAX_COLUMN+1), 1):

c 将 b 0 到 MAX_COLUMN, 0-9

        city[r].append(" ")

将空格字符附加到当前行。

我会把它写成:

def initialize():
   city = [
       # 0   1   2   3   4   5   6   7   8   9
       ["#","#","#","#","#","#","#","#","#","#"], # 0
       ["#","@"," "," "," "," "," "," ","u","#"], # 1
       ["#"," "," ","b","b"," "," "," ","X","#"], # 2
       ["#"," "," ","b","b"," "," "," "," ","#"], # 3
       ["#"," "," "," "," "," "," "," ","b","#"], # 4
       ["#","u"," ","u","u","u","u","u","u","#"], # 5
       ["#","b"," "," "," "," "," "," "," ","#"], # 6
       ["#"," "," "," "," ","b"," ","b"," ","#"], # 7
       ["#"," "," "," ","b"," "," "," "," ","#"], # 8
       ["#","#","#","#","#","#","#","#","#","#"], # 9
   ]
   return city

关于city.append([])

initialize中有一行(在第一个for循环中):

city.append([])#appends an empty list to city

进入循环之前city是:

[]

在第一个附加city之后是:

[[]]

这是一个包含一项的列表,一个空列表。

如果该行是:

城市.append(0)

这将是:

[0]

或者:

city.append("Jim")

它会给你:

["Jim"]

但是我们在列表中粘贴另一个列表,创建一个列表列表。

我希望这会有所帮助。

关于Python 创建一维和二维图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13487502/

相关文章:

image - wordpress 是否应该通过 https 自动加载图像?

Mysql-时间维度表

html - 调整图像大小以保持纵横比

python - 如何从 s3 存储桶获取 excel 文件并将文件再次上传到 s3 存储桶而不使用 pandas - Python

python - 如何将 Pytorch 模型导入 MATLAB

python - 如何在django中将下拉列表值保存到数据库

php - 使用 php 在 <img> 标签中加载图像

data-warehouse - 向现有数据仓库添加新维度

java - IndexOutOfBoundsException : Index: 0, 大小:0 2D Arraylist

python - scipy中的简单计算: minima