|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 wuqramy 于 2020-3-25 15:48 编辑
@DavidCT ,快来,pygame第二期!
首先下载pygame第三方库,在cmd中输入以下命令:
其次上附件:
注!:请把图片文件放在一个文件夹里,然后在图片名前面加上文件夹路径!
资源打包:
Bag2_images.zip
(170.07 KB, 下载次数: 6)
最后上代码:
本次增加的功能:可以通过键盘上的上下左右按键控制乌龟的移动import pygame
import pygame.transform
import sys
from pygame.locals import *
pygame.init()
size = width,height = 600,400
speed = [-2,1]
bg = (255,255,255)
# 加载帧数调节器
clock = pygame.time.Clock()
# 显示窗口
screen = pygame.display.set_mode(size)
# 加载背景
background = pygame.image.load('grass.jpg')
screen.blit(background,(0,0))
# 显示窗口标题
pygame.display.set_caption('running turtle')
# 加载图片
turtle = pygame.image.load('turtle.png')
# 获得图像的位置矩形
position = turtle.get_rect()
l_head = turtle
r_head = pygame.transform.flip(turtle,True,False)
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
turtle = l_head
speed = [-1,0]
if event.key == K_RIGHT:
turtle = r_head
speed = [1,0]
if event.key == K_UP:
speed = [0,-1]
if event.key == K_DOWN:
speed = [0,1]
# 移动图片
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(background,(0,0))
# 更新图片
screen.blit(turtle,position)
pygame.display.flip()
# 延时10毫秒
#pygame.time.delay(10)
# 设置帧数
clock.tick(100)
在附上一个飞檐走壁的龟兄代码:import pygame
import pygame.transform
import sys
from pygame.locals import *
pygame.init()
size = width,height = 600,400
bg = (255,255,255)
clock = pygame.time.Clock()
# 显示窗口
screen = pygame.display.set_mode(size)
# 显示窗口标题
pygame.display.set_caption('running turtle')
ratio = 1.0
# 加载图片
oturtle = pygame.image.load('turtle.png')
turtle = oturtle
# 获得图像的位置矩形
oturtle_rect = oturtle.get_rect()
position = turtle_rect = oturtle_rect
speed = [5,0]
turtle_right = pygame.transform.rotate(turtle,90)
turtle_top = pygame.transform.rotate(turtle,180)
turtle_left = pygame.transform.rotate(turtle,270)
turtle_bottom = turtle
turtle = turtle_top
fullscreen = False
l_head = turtle
r_head = pygame.transform.flip(turtle,True,False)
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
turtle = l_head
speed = [-1,0]
if event.key == K_RIGHT:
turtle = r_head
speed = [1,0]
if event.key == K_UP:
speed = [0,-1]
if event.key == K_DOWN:
speed = [0,1]
# 设置全屏
if event.key == K_F11:
fullscreen = not fullscreen
if fullscreen:
screen = pygame.display.set_mode((1366,768),FULLSCREEN | HWSURFACE)
width,height = 1366,768
else:
screen = screen = pygame.display.set_mode(size)
if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
if event.key == K_EQUALS and ratio < 2:
ratio += 0.1
if event.key == K_MINUS and ratio > 0.5:
ratio -= 0.1
if event.key == K_SPACE:
ratio = 1.0
turtle = pygame.transform.smoothscale(oturtle,\
(int(oturtle_rect.width * ratio),\
int(oturtle_rect.height * ratio)))
l_head = turtle
r_head = pygame.transform.flip(turtle,True,False)
# 用户调整窗口尺寸
if event.type == VIDEORESIZE:
size = event.size
width,height = size
screen = screen = pygame.display.set_mode(size,RESIZABLE)
if position.right > width:
turtle = turtle_right
position = turtle_rect = turtle.get_rect()
position.left = width - turtle_rect.width
speed = [0,5]
if position.bottom > height:
turtle = turtle_bottom
position = turtle_rect = turtle.get_rect()
position.left = width - turtle_rect.width
position.top = height - turtle_rect.height
speed = [-5,0]
if position.left < 0:
turtle = turtle_left
position = turtle_rect = turtle.get_rect()
position.top = height - turtle_rect.height
speed = [0,-5]
if position.top < 0:
turtle = turtle_top
position = turtle_rect = turtle.get_rect()
speed = [5,0]
# 移动图片
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()
# 延时10毫秒
#pygame.time.delay(10)
# 设置帧数
clock.tick(60)
哈哈,有不有趣呢?
如果你喜欢,一定别忘了:
|
评分
-
查看全部评分
|