1-Python小游戏,一个会动的小乌龟
效果预览
代码示例
import pygame
import sys
file_path = r'./turtle.jpg' # # 这里是你的图片路径
pygame.init()
size = width, height = 600, 400
speed = [-2, 1]
bg = (255, 255, 255)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("初次见面大家多多关照!")
turtle = pygame.image.load(file_path)
position = turtle.get_rect()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
position = position.move(speed)
if position.left < 0 or position.right > width:
turtle = pygame.transform.flip(turtle, True, False)
speed[0] = -speed[0]
if position.top < 0 or position.bottom > height:
speed[1] = -speed[1]
screen.fill(bg)
screen.blit(turtle, position)
pygame.display.flip()
pygame.time.delay(10)
代码由黑科技的孙凯同学提供。
欢迎斧正,that's all