完整的绘图不显示在屏幕上的pygame

时间:2013-10-03 13:41:28

标签: python python-3.x drawing coordinates pygame

我有以下python / pygame代码:

import sys, pygame 
 from array import * 
 pygame.init() 

  height = 600 
  width = 800 

设置显示窗口

 DISPLAYSURFACE = pygame.display.set_mode((width, height)) 
   pygame.display.set_caption('Graphics Assignment Part 1') 

打开文件

 file = open('F:\\school\\Graphics\\PIXA.DAT', 'r') 

将文件读入列表并关闭它

 vlist = file.readlines() 
#close file 
 file.close 
#remove last value from list 
 del vlist[-1] 

将vlist中的值存储到点中,但将J更改为JUMP位,并使用

分割整数
  points = [list(map(int,v.split())) if v.strip().lower() != "j" else "JUMP" for v in vlist] 

设置循环以将vlist拆分为3个x,y,z坐标列表

Xs,Ys,Zs= zip(*points) 
      unique_Xs = [] 
    [unique_Xs.append(val) for val in Xs if val != "J"] 
      unique_Ys = [] 
    [unique_Ys.append(val) for val in Ys if val != "U"] 
      unique_Zs = [] 
    [unique_Zs.append(val) for val in Zs if val != "M"] 
      draw = list(zip(unique_Xs,unique_Ys)) 

将屏幕上的点列表绘制为一系列行

 drawing = pygame.draw.lines(DISPLAYSURFACE,(0,255,172),False, draw, 1) 

更新显示并退出pygame

 pygame.display.update() 
  running = True 
   while running: 
     for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
           running = False

我正在读取坐标列表并将它们存储到x,y,z坐标列表中以在pygame中绘制一组行。

我可以绘制图像,但整个事物并没有出现在屏幕上,因为这些点在-2000到2000之间。

如何在我的python窗口中更改我的坐标范围以在800x600显示窗口中显示我的整个图像

1 个答案:

答案 0 :(得分:0)

缩放和平移坐标。编写一个函数,将一个范围的值映射到另一个范围。见Mapping a range of values to another

def mapVal(val, amin, amax, bmin, bmax):
    return bmin + (val - amin) * (bmax - bmin) / (amax - amin)
Xs,Ys,Zs= zip(*points) 
   
unique_Xs = [mapVal(val, -2000, 2000, 0, 800) for val in Xs if val != "J"] 
unique_Ys = [mapVal(val, -2000, 2000, 0, 600) for val in Ys if val != "U"]  
unique_Zs = [val for val in Zs if val != "M"]

draw = list(zip(unique_Xs, unique_Ys)) 
相关问题