Python:如何从屏幕侧面反弹
好的,我正在使用inventwithpython自学如何编写代码。我试图以自己的方式重用代码,以了解其工作原理,但这一部分给我带来了麻烦: 中有一组动画代码,其中不同大小的框从屏幕侧面反弹Python:如何从屏幕侧面反弹,python,pygame,Python,Pygame,好的,我正在使用inventwithpython自学如何编写代码。我试图以自己的方式重用代码,以了解其工作原理,但这一部分给我带来了麻烦: 中有一组动画代码,其中不同大小的框从屏幕侧面反弹 for b in blocks: # move the block data structure if b['dir'] == DOWNLEFT: b['rect'].left -= MOVESPEED b['rect'].top += MOVESPEED
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
块中b的:
#移动块数据结构
如果b['dir']==左下角:
b['rect'].左-=移动速度
b['rect'].top+=MOVESPEED
如果b['dir']==完全正确:
b['rect'].左+=移动速度
b['rect'].top+=MOVESPEED
如果b['dir']==UPLEFT:
b['rect'].左-=移动速度
b['rect'].top-=移动速度
如果b['dir']==竖直:
b['rect'].左+=移动速度
b['rect'].top-=移动速度
#检查挡块是否已移出车窗
如果b['rect'].top<0:
#积木已移过顶部
如果b['dir']==UPLEFT:
b['dir']=左下角
如果b['dir']==竖直:
b['dir']=完全正确
如果b['rect'].bottom>WINDOWHEIGHT:
#积木已移过底部
如果b['dir']==左下角:
b['dir']=1英尺
如果b['dir']==完全正确:
b['dir']=直立
如果b['rect'].左<0:
#街区已移过左侧
如果b['dir']==左下角:
b['dir']=完全正确
如果b['dir']==UPLEFT:
b['dir']=直立
如果b['rect'].right>WINDOWWIDTH:
#街区已移过右侧
如果b['dir']==完全正确:
b['dir']=左下角
如果b['dir']==竖直:
b['dir']=1英尺
我想创建它,使我的块左右移动,并从屏幕的每一侧反弹
然而,当我尝试自己对代码进行更改时,所发生的只是块从屏幕上飞出而没有反弹。我试过几种不同的方法,结果完全一样。现在看起来是这样的:
编辑:更新完整代码
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
从pygame.locals导入*
#设置pygame
pygame.init()
#开窗
窗宽=480
窗高=800
windowSurface=pygame.display.set_模式((窗口宽度,窗口高度),0,32)
pygame.display.set_标题('Jumper'))
#方向
左=4
右=6
上升=8
向下=2
移动速度=4
#设置颜色
黑色=(0,0,0)
红色=(255,0,0)
绿色=(0,255,0)
蓝色=(0,0255)
b1={'rect':pygame.rect(240700,20,20),'color':绿色,'dir':左侧}
区块=[b1]
#运行游戏循环
尽管如此:
#检查退出事件
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()
#在曲面上绘制黑色背景
窗口表面填充(黑色)
对于块中的b:
#移动块数据结构
如果b['dir']==左:
b['rect'].左-=移动速度
如果b['dir']==右侧:
b['rect'].左+=移动速度
如果b['rect'].左<0:
b['dir']=右
如果b['rect'].right>WINDOWWIDTH:
b['dir']=左
#将块绘制到曲面上
pygame.draw.rect(windowSurface,b['color'],b['rect']))
#把窗户拉到屏幕上
pygame.display.update()
睡眠时间(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)
我还为块和方向创建了类,而不是使用内联代码。请注意,主“移动并绘制”循环现在只是三行代码,而不是原始代码中的巨大复杂混乱
你应该熟悉你的C#背景的课程。Python中的符号有点不同,例如,\uuuuu init\uuuu()
是构造函数,但概念非常相似。如果代码中有任何不清楚的地方,请告诉我
还有一个小错误修复:原始代码允许块在反转之前经过屏幕边缘,因此我调整了边缘测试,使其在经过边缘之前反转。这就是为什么它说,例如,
if self.rect.left
,而不是if self.rect.left<0
在哪里更新矩形结构的.bottom
和.right
字段?据我所知,.right随.left一起更新。left?已更新为包含完整代码除了错误的缩进之外,您的左右移动代码工作正常。缩进是否会导致问题?我不习惯Python,来自c#非常感谢,现在我可以看到我出了什么问题(比如一些左右的东西嵌套在方向中),现在大部分都在我的脑海中。。。然而,关于最后一点,将'rect.left<0'更改为'rect.left<1'是否也有效?实际上,我刚刚回答了我自己的问题。使用速度,因为这样,如果我更改速度,当更新将其从边缘移动到该值时,速度将始终保持不变
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 )