pygame使用之第一个简单的动画效果实现

本文介绍了一个使用Python的Pygame库创建动画的基本示例。通过加载图片并控制其在窗口中移动,实现了一个简单的动画效果。代码展示了如何设置窗口、加载图片、控制图片位置以及响应退出事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import pygame,sys
from pygame.locals import *
pygame.init()
FPS = 30 #frames per second setting
fpsClock = pygame.time.Clock()
#set up the window
DISPLAYSURF = pygame.display.set_mode((400,300),0,32)
pygame.display.set_caption('Animation')
GREEN = (0,255,0)
catImg = pygame.image.load('cat.png')
catx=10
caty=10
direction='right'
while True:
	DISPLAYSURF.fill(GREEN)
	if direction == 'right':
		catx+=5
		if catx == 280:
			direction = 'down'
	elif direction=='down':
		caty+=5
		if caty == 220:
			direction='left'
	elif direction == 'left':
		catx-=5
		if catx==10:
			direction='up'
	elif direction == 'up':
		caty -=5
		if caty==10:
			direction = 'right'
	DISPLAYSURF.blit(catImg,(catx,caty))
	for event in pygame.event.get():
		if event.type == QUIT:
			pygame.quit()
			sys.exit()
	pygame.display.update()
	fpsClock.tick(FPS)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值