马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 学习编程中的Ben 于 2022-12-29 17:09 编辑
在线演示:
这是一个叫做疯狂的笑脸的动画
注意,若要运行,请下载图片文件并放于同一文件夹中
老规矩,先上代码,后上效果图:
import pygame
import sys
from random import *
windows_width = 1400
windows_height = 700
pygame.init()
screen = pygame.display.set_mode((windows_width, windows_height))
pygame.display.set_caption("PYTHON 疯狂的笑脸")
tank_speed_clock = pygame.time.Clock()
speed_x = randint(1, 3)
speed_y = randint(1, 3)
pic_x = pic_y = 0
i = 1
pic = pygame.image.load('smile.png')
p = 1
while i > 0:
for event in pygame.event.get():
if event.type == pygame.constants.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.constants.KEYDOWN:
if event.key == pygame.constants.K_ESCAPE:
pygame.quit()
sys.exit()
elif event.key == pygame.K_SPACE:
pygame.image.save(screen, "crazysmile"+str(p)+".png")
p += 1
pic_x += speed_x
pic_y += speed_y
if pic_y <= 0 or (pic_y >= windows_height-pic.get_height()):
speed_y = -speed_y
if pic_x <= 0 or (pic_x >= windows_width-pic.get_width()):
speed_x = -speed_x
pic_x += speed_x
pic_y += speed_y
screen.blit(pic, (pic_x, pic_y)) # blit 第一个参数:要显示的元素,第二个参数:坐标
pygame.display.update()
tank_speed_clock.tick(60)
喜欢的话就评个分吧!
|