好的,我正在使用inventwith
python来教我自己如何编码.我试图以我自己的方式重用代码来理解它是如何工作的,但这部分给了我麻烦:
在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等.
现在发生了一件美妙的事情考虑检查左边缘反弹的代码(如果b [‘rect’].左< 0).我们可以忽略y方向,只需将x = -1更改为x = 1,而不必使特殊情况将DOWNLEFT更改为DOWNRIGHT或UPLEFT更改为UPRIGHT. 除了我们将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 __()是构造函数 – 但概念非常相似.如果代码中的任何内容不清楚,请告诉我.
还有一个小错误修复:原始代码让块在反转之前经过屏幕边缘,因此我在边缘测试越过边缘之前调整边缘测试.这就是为什么它说,例如,如果self.rect.left< SPEED而不是self.rect.left< 0.