弹跳小球
import sys
import pygame
pygame.init() #初始化pygame
size = width,height = 640,480 #设置窗口大小
screen = pygame.display.set_mode(size)#显示窗口
color = (0,0,0)
ball = pygame.image.load('ball.png')
image = pygame.transform.scale(ball,(100,100))
ballrect = ball.get_rect()
speed=[500,500]#x,y轴
clock = pygame.time.Clock()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT():
sys.exit()
ballrect=ballrect.move(speed)
#碰到左右边缘
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
screen.fill(color)#颜色
screen.blit(image,ballrect)#将图片滑到窗口
pygame.display.flip()#更新全部显示
pygame.quit()
标签:ball,ballrect,image,小球,pygame,弹跳,speed,event From: https://www.cnblogs.com/zyyyywzsyx/p/16943090.html