最近沉迷pygame无法自拔,可是总是对动画这块有点懵逼
所以将这段学习到的代码记录下来
第一段:最基本的动画
import pygame
from pygame import *
from sys import exit
"""
一段最基本的动画代码
让飞机从屏幕左边飞到右边
"""
air_image = "D:\\workspace\\game\\data\\jet.png"
WIDTH, HEIGHT = 600, 480
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
air = pygame.image.load(air_image)
clock = pygame.time.Clock()
x = 0
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
screen.fill((255, 255, 255))
screen.blit(air, (x, 100))
# 设置FPS
clock.tick(60)
x += 2
if x > 600:
x -= 640
pygame.display.update()
运行以后的效果图:
你可以找一张图片替换我这张图片,然后自己试一下,
想一下 这个是从到右运动,那么从上到下呢 我们只要保持x的值不变
让y的值一直变化就可以了,让y的值到达屏幕下方的时候把它归0