search

Home  >  Q&A  >  body text

python - 一个程序中的切片问题

下面标记的地方,原文是这样讲的,#这个切片操作保证了event_text里面只保留一个屏幕的文字,但是操作过程中报错TypeError,

#coding: GBK
import pygame 
from pygame.locals import *
from sys import exit

pygame.init()
SCREEN_SIZE = (640,480)
screen = pygame.display.set_mode(SCREEN_SIZE,0,32)

font = pygame.font.SysFont("arial",16)
font_height = font.get_linesize()
event_text = []
while True:
    event = pygame.event.wait()
    event_text.append(str(event))
    
    event_text = event_text[-SCREEN_SIZE[1]/font_height:] #提示TypeError错误,看不懂
    
    if event.type == QUIT:
        exit()
    screen.fill((0,255,0))
    
    y = SCREEN_SIZE[1]-font_height
    
    for text in reversed(event_text):
        screen.blit(font.render(text,True,(0,255,0)),(0,y))
        y -= font_height
        
    pygame.display.update()
ringa_leeringa_lee2786 days ago736

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:30:37

    Are you using Python 3? In Python 3, a/b are both floating point numbers, you can change it to SCREEN_SIZE[1]//font_height!

    reply
    0
  • Cancelreply