-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path078小乌龟.py
47 lines (36 loc) · 1.02 KB
/
078小乌龟.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import pygame
import sys
#初始化Pygame
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("turtle.jpg")
#获取图片的位置矩形
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.bottom > height or position.top < 0:
speed[1] = -speed[1]
#填充背景
screen.fill(bg)
#更新图像
screen.blit(turtle, position)
#更新界面
pygame.display.flip()
#延迟10毫秒
pygame.time.delay(10)