Pygame 让图形动起来

Pygame 让图形动起来

通过改变Rect对象的x,y坐标值,从而实现图形动画效果。

import sys
import  pygame
from pygame.locals import *

pygame.init()

SCREEN_SIZE=SCREEN_WIDHT,SCREEN_HEIGHT = 800,600
FPS = 60
WHITE = 255,255,255
BLUE = 0,0,200
#移动速度
VEL_X = 2
VEL_Y = 1

clock = pygame.time.Clock()
screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption('移动距形')

rect = pygame.Rect(200,250,100,100)

while True:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    
    #移动距形
    rect.x += VEL_X
    rect.y += VEL_Y

    #边界判断
    if rect.x > SCREEN_WIDHT-100 or rect.x < 0:
        VEL_X =-VEL_X
    if rect.y  > SCREEN_HEIGHT -100  or rect.y  <0:
        VEL_Y = -VEL_Y
    
    screen.fill(BLUE)
    pygame.draw.rect(screen,WHITE,rect,0)
    pygame.display.update()

猜你喜欢

转载自blog.csdn.net/qq_67984864/article/details/129118610