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)

 

 

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