为什么一样的代码运行后是一片黑色?
小甲鱼0基础学习python中第18章,18.2的代码,我按照原代码一字不差的敲进IDLE,只是用的图片不一样,但显示出来的效果完全不一样,换过很多种格式的图片也不行,就是一个黑黑的方框。我的pygame版本是1.9.6,不应是版本的问题吧。代码如下:import pygame
import sys
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("1.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.filp()
pygame.time.delay(10)
到底是什么原因,有哪位大师知道么?
缩进错了,试试这样:
import pygame
import sys
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("1.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.filp()
pygame.time.delay(10) zltzlt 发表于 2020-7-22 12:00
缩进错了,试试这样:
谢谢,这个问题解决了,书上面翻页后缩进看不出来啊 除了楼上提出的 BUG 外,还需要补充以下几点,请看代码:
import pygame
import sys
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("1.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)
# bug1原代码 speed = -speed
speed -= speed
if position.top < 0 or position.bottom > height:
# bug2原代码 speed = -speed
speed -= speed
screen.fill(bg)
screen.blit(turtle, position)
pygame.display.filp()
pygame.time.delay(10)
nahongyan1997 发表于 2020-7-22 12:27
除了楼上提出的 BUG 外,还需要补充以下几点,请看代码:
感谢!
页:
[1]