好的,我正在使用inventwithpython自学如何编码。我试图以我自己的方式重用代码来理解它是如何工作的,但这部分给我带来了麻烦:
在 Chapter 17有一组动画代码,其中不同大小的框从屏幕一侧反弹
for b in blocks:
# move the block data structure
if b['dir'] == DOWNLEFT:
b['rect'].left -= MOVESPEED
b['rect'].top += MOVESPEED
if b['dir'] == DOWNRIGHT:
b['rect'].left += MOVESPEED
b['rect'].top += MOVESPEED
if b['dir'] == UPLEFT:
b['rect'].left -= MOVESPEED
b['rect'].top -= MOVESPEED
if b['dir'] == UPRIGHT:
b['rect'].left += MOVESPEED
b['rect'].top -= MOVESPEED
# check if the block has move out of the window
if b['rect'].top < 0:
# block has moved past the top
if b['dir'] == UPLEFT:
b['dir'] = DOWNLEFT
if b['dir'] == UPRIGHT:
b['dir'] = DOWNRIGHT
if b['rect'].bottom > WINDOWHEIGHT:
# block has moved past the bottom
if b['dir'] == DOWNLEFT:
b['dir'] = UPLEFT
if b['dir'] == DOWNRIGHT:
b['dir'] = UPRIGHT
if b['rect'].left < 0:
# block has moved past the left side
if b['dir'] == DOWNLEFT:
b['dir'] = DOWNRIGHT
if b['dir'] == UPLEFT:
b['dir'] = UPRIGHT
if b['rect'].right > WINDOWWIDTH:
# block has moved past the right side
if b['dir'] == DOWNRIGHT:
b['dir'] = DOWNLEFT
if b['dir'] == UPRIGHT:
b['dir'] = UPLEFT
我想创建它,以便我的块左右移动并从屏幕的每一侧反弹。
但是,当我尝试自己更改此代码时,所发生的只是块飞出屏幕而不会弹跳。尝试了几种变体,结果完全相同。目前它看起来像这样:
编辑:更新完整代码
import pygame, sys, time
from pygame.locals import *
# set up pygame
pygame.init()
# set up the window
WINDOWWIDTH = 480
WINDOWHEIGHT = 800
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Jumper')
#Directions
LEFT = 4
RIGHT = 6
UP = 8
DOWN = 2
MOVESPEED = 4
# set up the colors
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
b1 = {'rect':pygame.Rect(240, 700, 20, 20), 'color':GREEN, 'dir':LEFT}
blocks = [b1]
# run the game loop
while True:
# check for the QUIT event
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# draw the black background onto the surface
windowSurface.fill(BLACK)
for b in blocks:
# move the block data structure
if b['dir'] == LEFT:
b['rect'].left -= MOVESPEED
if b['dir'] == RIGHT:
b['rect'].left += MOVESPEED
if b['rect'].left < 0:
b['dir'] = RIGHT
if b['rect'].right > WINDOWWIDTH:
b['dir'] = LEFT
# draw the block onto the surface
pygame.draw.rect(windowSurface, b['color'], b['rect'])
# draw the window onto the screen
pygame.display.update()
time.sleep(0.02)
最佳答案
您代码中的问题只是缩进混淆。如果修复缩进,它可以正常工作:
import pygame, sys, time
from pygame.locals import *
# set up pygame
pygame.init()
# set up the window
WINDOWWIDTH = 480
WINDOWHEIGHT = 800
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Jumper')
#Directions
LEFT = 4
RIGHT = 6
UP = 8
DOWN = 2
MOVESPEED = 4
# set up the colors
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
b1 = {'rect':pygame.Rect(240, 700, 20, 20), 'color':GREEN, 'dir':LEFT}
blocks = [b1]
# run the game loop
while True:
# check for the QUIT event
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# draw the black background onto the surface
windowSurface.fill(BLACK)
for b in blocks:
# move the block data structure
if b['dir'] == LEFT:
b['rect'].left -= MOVESPEED
if b['dir'] == RIGHT:
b['rect'].left += MOVESPEED
if b['rect'].left < 0:
b['dir'] = RIGHT
if b['rect'].right > WINDOWWIDTH:
b['dir'] = LEFT
# draw the block onto the surface
pygame.draw.rect(windowSurface, b['color'], b['rect'])
# draw the window onto the screen
pygame.display.update()
time.sleep(0.02)
Python 使用缩进来告诉什么嵌套在什么里面。在 C# 和其他使用大括号或其他块分隔符的语言中,只要将大括号放在正确的位置,就可以避免错误的缩进。但是在 Python 中,缩进是至关重要的。 (在所有其他语言中,适当的缩进对于代码可读性仍然至关重要,即使编译器不关心它。)
将您的代码版本与上面的代码进行比较,您将看到需要修复缩进的位置。
现在来解决一个更大的问题。来自 Inventing With Python 站点的示例代码是……说得好,不太好。
假装您对编程一无所知,只需查看您开始使用的原始代码即可。那里似乎没有大量重复?
并且非常棘手的重复。看看它设法使用的所有不同方式
DOWNLEFT
, DOWNRIGHT
, UPLEFT
, UPRIGHT
,以及代码如何必须如此小心才能每次都正确。每当您想编写这种棘手的代码时,请停下来,坐下来问问自己,“难道没有更简单的方法来做到这一点吗?”
事实上,你肯定是在正确的轨道上去除一堆复杂的代码并为你的实验做一些更简单的事情。
但只是为了好玩,让我们回到他们的代码,看看我们如何简化它,但仍然做它所做的一切。
从这四个名字开始,
DOWNLEFT
等等。为什么每个都有自己的名字?他们真的不是基本上一样的东西吗?它们都是对角线,只是不同方向的对角线。顾名思义,对角线实际上是垂直和水平两个方向的组合。
现在,我们如何指定屏幕/窗口上的位置?我们没有给每一个可能的位置命名,我们给它一个坐标
x
和 y
值。最左边、最上面的像素是 x=0, y=0
我们从那里开始,加一个向右或向下移动,减去一个向左或向上移动。因此,与其为四个对角线方向编造名称,不如使用实际数字!我们将代表
DOWNRIGHT
如 x=1, y=1
, UPLEFT
如 x=-1, y=-1
, 等等。现在奇妙的事情发生了。考虑检查左边缘反弹的代码 (
if b['rect'].left < 0
)。我们可以忽略 y
,而不必进行特殊情况将 DOWNLEFT 更改为 DOWNRIGHT 或 UPLEFT 更改为 UPRIGHT。方向并简单地改变 x=-1
至 x=1
.从右边缘反弹也是如此,除了我们更改
x=1
至 x=-1
.事实上,你可以通过乘以 x
来处理这两种情况。来自 -1
.所以我们可以这样做:如果我们碰到左边或右边,我们乘以
x
方向 -1
.顶部或底部边缘以及 y
也是如此方向。这是执行此操作的原始代码的修订版,并进行了一些其他简化:
import pygame, sys, time
from pygame.locals import *
class Block( object ):
def __init__( self, rect, color, dir ):
self.rect = rect
self.color = color
self.dir = dir
def move( self ):
# reverse direction if the block will move out of the window
if self.rect.left < SPEED or self.rect.right > WIN_WIDTH - SPEED:
self.dir.x *= -1
if self.rect.top < SPEED or self.rect.bottom > WIN_HEIGHT - SPEED:
self.dir.y *= -1
# move the block
self.rect.left += self.dir.x * SPEED
self.rect.top += self.dir.y * SPEED
def draw( self ):
pygame.draw.rect( windowSurface, self.color, self.rect )
class Direction( object ):
def __init__( self, x, y ):
self.x = x
self.y = y
# set up pygame
pygame.init()
# set up the window
WIN_WIDTH = 400
WIN_HEIGHT = 400
windowSurface = pygame.display.set_mode( ( WIN_WIDTH, WIN_HEIGHT ), 0, 32 )
pygame.display.set_caption( 'Animation' )
# set up the movement speed
SPEED = 4
# set up the colors
BLACK = ( 0, 0, 0 )
RED = ( 255, 0, 0 )
GREEN = ( 0, 255, 0 )
BLUE = ( 0, 0, 255 )
# set up the block objects
blocks = [
Block( pygame.Rect( 300, 80, 50, 100 ), RED, Direction( -1, 1 ) ),
Block( pygame.Rect( 200, 200, 20, 20 ), GREEN, Direction( -1, -1 ) ),
Block( pygame.Rect( 100, 150, 60, 60 ), BLUE, Direction( 1, -1 ) ),
]
# run the game loop
while True:
# check for the QUIT event
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# draw the black background onto the surface
windowSurface.fill( BLACK )
# move and draw each block
for block in blocks:
block.move()
block.draw()
# draw the window onto the screen
pygame.display.update()
time.sleep( 0.02 )
我还为块和方向创建了类,而不是使用内联代码完成所有工作。注意主要的“移动和绘制”循环现在只有三行代码,而不是原始代码中巨大的复杂困惑。
从您的 C# 背景来看,您应该熟悉类。 Python 中的符号有点不同 - 例如,
__init__()
是构造函数 - 但概念非常相似。如果代码中的任何内容不清楚,请告诉我。还有一个小错误修复:原始代码让块在反转之前通过屏幕边缘,所以我调整了边缘测试以在它通过边缘之前反转。这就是为什么它说,例如,
if self.rect.left < SPEED
而不是 if self.rect.left < 0
.
关于Python:如何从屏幕的一侧反弹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29879484/