pygame的Rect参数类型

背景:

1. 在学习时,发现数据类型转换,比如 int(5/2), 会明显降低效率。而int 和 float在显示上有不同。(只到显示,不涉及实际存储)

2. pygame.Rect(left, top, width, height)的参数应是int数字。 (pygame.Rect的帮助文档中没有找到)。

测试结果:

可以直接给pygame.Rect()的各参数传入int, float, 或者 带有除法的参数,pygame.Rect会自动取整。

import pygame
from pygame.locals import *
import sys

print('5/2:', 5/2)
print('4/2:', 4/2)
print('int(5/2):', int(5/2))
print('int(4/2):', int(4/2))


FPS=30

mainClock=pygame.time.Clock()

pygame.init()

WINDOWSURF=pygame.display.set_mode((400,300))
pygame.display.set_caption("rect parameter can be int / int?")

rect1=pygame.Rect(2.6,5,41/2, 40)
rectSurf=pygame.draw.rect(WINDOWSURF, (255,255,255), rect1)
print('rect1 size, left, right, top, bottom:',rect1.size, rect1.left, rect1.right, rect1.top, rect1.bottom, flush=True)


def myterminate():
   pygame.quit()
   sys.exit()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            myterminate()
        if event.type == KEYUP:
            if event.key == K_ESCAPE:
                myterminate()

    pygame.display.update()
    mainClock.tick(FPS)

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章