在 pygame 上制作足球游戏

问题描述 投票:0回答:3

我无法让足球发生碰撞。如何让球员移动球?我不知道该怎么做。我附上了用于与足球碰撞的代码。到目前为止,当球员触球时,球只是进入对角线而不会入网。有人可以帮忙吗?

# 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
python pygame
3个回答
0
投票

如果您使用足球图像,那么此代码将起作用!当用户触摸设备屏幕的左侧或右侧时,以下内容使足球(或图像)移动......

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;
        }
    });

0
投票

再尝试一件事......

@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;
        }
    });

0
投票

日语(我不在乎) 你们都是书呆子(真的)🤓

© www.soinside.com 2019 - 2024. All rights reserved.