零基础入门python中的68讲中的小乌龟程序运行错误问题
import pygameimport 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.lord('turtle.png')
#获得图像的位置矩形
position = turble.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 = -speed
if position.top < 0 or position.bottom > height:
speed = -speed
#填充背景
screen.fill(bg)
#更新图像
screen.blit(turtle, position)
#更新界面
pygame.display.flip()
#延迟10毫秒
pygame.time.delay(10)
运行之后出现
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
=================== RESTART: C:\Users\李\Desktop\小乌龟\小乌龟.py ===================
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "C:\Users\李\Desktop\小乌龟\小乌龟.py", line 17, in <module>
turtle = pygame.image.lord('turtle.png')
AttributeError: module 'pygame.image' has no attribute 'lord'
>>>
求助大佬怎么解决?
load 错误来自这这里:
#加载图片
turtle = pygame . image . load('turtle.png') # 楼主把 load() 写成了 lord() 改了还是不行呀,还有什么问题吗? 修改后
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.png')
#获得图像的位置矩形
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 = -speed
if position.top < 0 or position.bottom > height:
speed = -speed
#填充背景
screen.fill(bg)
#更新图像
screen.blit(turtle, position)
#更新界面
pygame.display.flip()
#延迟10毫秒
pygame.time.delay(10)
报错
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
=================== RESTART: C:\Users\李\Desktop\小乌龟\小乌龟.py ===================
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "C:\Users\李\Desktop\小乌龟\小乌龟.py", line 17, in <module>
turtle = pygame.image.load('turtle.png')
pygame.error: Couldn't open turtle.png
>>>
还是有问题,怎么解决? jackz007 发表于 2019-2-17 22:27
错误来自这这里:
代码运行无报错,但是图片不显示是因为什么呢?
页:
[1]