我很难完成足球的碰撞。你如何让球员移动球?我不知道该怎么做。我附上了用于与足球碰撞的代码。到目前为止,当球员进入球时,它只是进入对角线并且不进入网。有人可以帮忙吗?
# 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
答案 0 :(得分: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;
}
});
答案 1 :(得分: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;
}
});