我在足球的碰撞中遇到了困难。你如何让球员移动球?我不知道该怎么做。我附上了我的代码,我用来碰撞足球。到目前为止,当球员跑到球上时,球只是进入一条对角线,而不是进入球网。有人能帮帮忙吗?
# 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发布于 2018-06-18 04:36:46
如果你使用的是足球的图像,那么这个代码就可以工作了!当用户触摸设备屏幕的左侧或右侧时,下面的命令会使足球(或图像)移动。
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;
}
});发布于 2018-06-18 04:59:35
再试一次……
@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;
}
});https://stackoverflow.com/questions/50900151
复制相似问题