在pygam上进行足球比赛

2024-10-03 09:14:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难使足球相撞。如何让球员移动球?我不知道怎么做。我附上了我用来和足球碰撞的代码。到目前为止,当球员跑进球里时,球只会进入斜线,而不会进入网窝。谁能帮忙吗?在

# collision scenario for what happenes when second soccer player collides with ball
    blocks_hit_list_soccer2 = pygame.sprite.spritecollide(soccer_Avatar2, block_list, False)
    for i in blocks_hit_list_soccer2:
        soccer_Avatar2.rect.x -= soccer2_x_speed
        soccer_Avatar2.rect.y -= soccer2_y_speed
        soccer2_x_speed = 0
        soccer2_y_speed = 0


# collision scenario for what happens when ball collides with wall
    blocks_hit_list_ball = pygame.sprite.spritecollide(soccer_Ball, block_list, False)
    for i in blocks_hit_list_ball:
        soccer_Ball.rect.x == ball_x_speed
        soccer_Ball.rect.y == ball_y_speed
        ball_x_speed = 0
        ball_y_speed = 0


# collision scenario for what happens when ball collides with first soccer player
    blocks_hit_list_ball = pygame.sprite.spritecollide(soccer_Ball, player1_list, False)
    for i in blocks_hit_list_ball:
        soccer_Ball.rect.x += ball_x_speed
        soccer_Ball.rect.y += ball_y_speed
        ball_x_speed = 5
        ball_y_speed = 5


# collision scenario for what happens when ball collides with second soccer player
    blocks_hit_list_ball2 = pygame.sprite.spritecollide(soccer_Ball, player2_list, False)
    for i in blocks_hit_list_ball2:
        soccer_Ball.rect.x -= ball_x_speed
        soccer_Ball.rect.y -= ball_y_speed
        ball_x_speed = 5
        ball_y_speed = 5

Tags: rectforwhatlistwhenspeedscenariohit
2条回答

再试试这个。。。。。在

@Override
        public boolean onTouch(View v, MotionEvent event) {
            int screenWidth = getResources().getDisplayMetrics().widthPixels;
            int x = (int)event.getX();
            int buffer = lp.leftMargin;
            if( x > ( screenWidth/2) ) {
                int ScreenWidth = getResources().getDisplayMetrics().widthPixels;
                float Xtouch = event.getRawX();
                int sign = Xtouch > 0.5*ScreenWidth ? 1 : -1;
                float XToMove = 60; // or whatever amount you want
                int durationMs = 50;
                v.animate().translationXBy(sign*XToMove).setDuration(durationMs);
            }else {
                if( x < ( screenWidth/2) ) {
                    int ScreenWidth = getResources().getDisplayMetrics().widthPixels;
                    float xtouch = event.getRawX();
                    int sign = xtouch < 0.5 / ScreenWidth ? 1 : -1;
                    float xToMove = 60; // or whatever amount you want
                    int durationMs = 50;
                    v.animate().translationXBy(sign*xToMove).setDuration(durationMs);
                }
            }
            return false;
        }
    });

如果你使用的是足球图像,那么这个代码就可以工作了!当用户触摸设备屏幕的左侧或右侧时,以下操作将使足球(或图像)移动。。。。。在

public boolean onTouch(View v, MotionEvent event) {
            int screenWidth = getResources().getDisplayMetrics().widthPixels;
            int x = (int)event.getX();
            int buffer = lp.leftMargin;
            if( x < screenWidth-buffer ) {
                int ScreenWidth = getResources().getDisplayMetrics().widthPixels;
                float Xtouch = event.getRawX();
                int sign = Xtouch > 0.5*ScreenWidth ? 1 : -1;
                float XToMove = 60; // or whatever amount you want
                int durationMs = 50;
                v.animate().translationXBy(sign*XToMove).setDuration(durationMs);
            }else {
                if( x < ( screenWidth/2) ) {
                    int ScreenWidth = getResources().getDisplayMetrics().widthPixels;
                    float xtouch = event.getRawX();
                    int sign = xtouch < 0.5 / ScreenWidth ? 1 : -1;
                    float xToMove = 60; // or whatever amount you want
                    int durationMs = 50;
                    v.animate().translationXBy(sign*xToMove).setDuration(durationMs);
                }
            }
            return false;
        }
    });

相关问题 更多 >