| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2740 人关注过本帖
标题:move()方法中speed[0]和speed[1]什么意思?有什么作用?
只看楼主 加入收藏
shenggeyanwu
Rank: 2
等 级:论坛游民
帖 子:26
专家分:11
注 册:2017-1-25
结帖率:83.33%
收藏
 问题点数:0 回复次数:0 
move()方法中speed[0]和speed[1]什么意思?有什么作用?
import sys,pygame
from random import*

class MyBallClass(pygame.sprite.Sprite):
    def __init__(self,image_file,location,speed):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load(image_file)
        self.rect=self.image.get_rect()
        self.rect.left,self.rect.top=location
        self.speed=speed

    def move(self):
        self.rect=self.rect.move(self.speed)
        if self.rect.left<0 or self.rect.right>width:
            self.speed[0]=-self.speed[0]              #这个0是什么意思?有什么作用?
        if self.rect.top<0 or self.rect.bottom>height:
            self.speed[1]=-self.speed[1]              #这个1是什么意思?有什么作用?

def animate(group):
    screen.fill([255,255,255])
    for ball in group:
        group.remove(ball)
        if pygame.sprite.spritecollide(ball,group,False):
            ball.speed[0]=-ball.speed[0]
            ball.speed[1]=-ball.speed[1]

        group.add(ball)
        ball.move()
        screen.blit(ball.image,ball.rect)
    pygame.display.flip()
    pygame.time.delay(20)
size=width,height=1200,680
screen=pygame.display.set_mode(size)
screen.fill([255,255,255])
img_file='ppq.png'
group=pygame.sprite.Group()
for row in range(0,2):
    for column in range(0,2):
        location=[column*180+10,row*180+10]
        speed=[choice([2,-2]),choice([-2,2])]
        ball=MyBallClass(img_file,location,speed)
        group.add(ball)

running=True
while running:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            running=False
    animate(group)
pygame.quit()


请高手给我详细解答这个move()函数的原理,特别是其中的speed[0]和speed[1]到底什么意思?有什么作用?方括号里用红色标出的0和1是什么意思啊?

[此贴子已经被作者于2017-11-5 10:19编辑过]

搜索更多相关主题的帖子: move pygame location if group 
2017-11-04 09:58
快速回复:move()方法中speed[0]和speed[1]什么意思?有什么作用?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018820 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved