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)

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值