import pygame
BLACK = pygame.color.Color('Black')
YELLOW = pygame.color.Color('Yellow')
BLUE = pygame.color.Color('Blue')
pygame.init()
screen = pygame.display.set_mode([700,500])
screen_rect = screen.get_rect()
pygame.display.set_caption("Trial to make PONG")
blue_rect = pygame.Rect(10, 250, 20, 60)
yellow_rect = pygame.Rect(670, 250, 20, 60)
ball_rect = pygame.Rect(50, 50, 50, 50)
ball_x_speed = 5
ball_y_speed = 5
clock = pygame.time.Clock()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# check all pressed keys and move the paddles
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP]: blue_rect.move_ip(0, -5)
if pressed[pygame.K_DOWN]: blue_rect.move_ip(0, 5)
if pressed[pygame.K_w]: yellow_rect.move_ip(0, -5)
if pressed[pygame.K_s]: yellow_rect.move_ip(0, 5)
# ensure paddles stay on screen
blue_rect.clamp_ip(screen_rect)
yellow_rect.clamp_ip(screen_rect)
# move the ball
ball_rect.move_ip(ball_x_speed, ball_y_speed)
# check if the ball needs to change direction
if ball_rect.x + ball_rect.width > screen_rect.width or ball_rect.x < 0:
ball_x_speed = ball_x_speed * -1
if ball_rect.y + ball_rect.height> screen_rect.height or ball_rect.y < 0:
ball_y_speed = ball_y_speed * -1
# draw everything
screen.fill(BLACK)
pygame.draw.ellipse(screen, BLUE, ball_rect)
pygame.draw.rect(screen,BLUE, blue_rect)
pygame.draw.rect(screen,YELLOW, yellow_rect)
pygame.display.flip()
clock.tick(60)
pygame.quit()
我在比赛中有两个球,一个球在弹跳。当球撞到球拍时,我试图制造一个碰撞点。我试图重建乒乓球。碰撞点没有用(可能是因为我没有正确地构造它)。
我想知道如何在球拍(长方形蓝色和黄色)和球(ball_rect
)之间形成一个碰撞点,以便球从球拍中反弹?
答案 0 :(得分:1)
这应该有效:
# Inside the main loop.
if ball_rect.collidelist([blue_rect, yellow_rect]) > -1:
ball_x_speed = -ball_x_speed
希望它有所帮助!
答案 1 :(得分:0)
这是我在Java中的ball类...........
public class Ball {
public int width = 30;
public int height = 30;
public int motionX;
public int motionY;
public int x, y;
private RectAnimation pong;
public Random random;
public int amountOfHits;
public Ball(RectAnimation pong) {
this.random = new Random();
this.pong = pong;
spawn();
}
public void update(paddle paddle1, paddle paddle2) {
int speed = 5;
this.x += motionX * speed;
this.y += motionY * speed;
if (this.y + height > pong.height || this.y < 0) {
if (this.motionY < 0) {
this.y = 0;
this.motionY = random.nextInt(4);
if (motionY == 0) {
motionY = 1;
}
} else {
this.y = pong.height - height - motionY;
this.motionY = -random.nextInt(4);
if (motionY == 0) {
motionY = -1;
}
}
}
if (checkCollision(paddle1) == 1) {
this.motionX = 1 + (amountOfHits / 5);
this.motionY = -2 + random.nextInt(4);
if (motionY == 0) {
motionY = 1;
}
amountOfHits++;
} else if (checkCollision(paddle2) == 1) {
this.motionX = -1 - (amountOfHits / 5);
this.motionY = -2 + random.nextInt(4);
if (motionY == 0) {
motionY = -1;
}
}
if (checkCollision(paddle1) == 2) {
paddle2.score++;
spawn();
} else if (checkCollision(paddle2) == 2) {
paddle1.score++;
spawn();
}
}
public void spawn() {
this.amountOfHits = 0;
this.x = pong.width / 2 - this.width / 2;
this.y = pong.height / 2 - this.height / 2;
this.motionX = -1 + random.nextInt(4);
if (motionY == 0) {
motionY = 1;
}
if (random.nextBoolean()) {
motionX = 1;
} else {
motionX = -1;
}
}
public int checkCollision(paddle pad) {
if (this.x < pad.x + pad.width && this.x + width > pad.x && this.y < pad.y + pad.height && this.y + height > pad.y) {
return 1; //bounce
} else if (pad.x > x + width && pad.paddleNumber == 1 || pad.x < x && pad.paddleNumber == 2) {
return 2; //score
}
return 0;
}
public void render(Graphics g) {
g.setColor(Color.WHITE);
g.fillOval(x, y, width, height);
}
}
您如何询问并实例化它。...
public class RectAnimation implements ActionListener, KeyListener {
public static RectAnimation rectAnimation;
public gamePong renderer;
public paddle player1;
public paddle player2;
public Ball ball;
public int width = 700, height = 700;
public boolean bot = false;
public boolean w = false;
public boolean s = false;
public boolean up = false;
public boolean down = false;
public int gameStatus = 0;
public int botMoves;
public int botCoolDown = 0;
public int botDifficulty = 0;
public RectAnimation() {
Timer timer = new Timer(20, this);
JFrame frame = new JFrame("Rectangle Animation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(true);
frame.setSize(width + 15, height + 30);
frame.setLocation(375, 55);
renderer = new gamePong();
frame.add(renderer);
frame.addKeyListener(this);
timer.start();
}
public void render(Graphics2D g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, width, height);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (gameStatus == 0) {
g.setColor(Color.WHITE);
g.setFont(new Font("Ariel", 1, 50));
g.drawString("PONG", width / 2 - 70, 50);
g.setFont(new Font("Ariel", 1, 20));
g.drawString("Press Space to Play", width / 2 - 90, height / 2 + 50);
}
if (gameStatus == 1 || gameStatus == 2) {
g.setColor(Color.WHITE);
g.setStroke(new BasicStroke(10));
g.drawLine(width / 2, 0, width / 2, height);
g.drawOval(width / 2 - 100, height / 2 - 100, 200, 200);
g.setFont(new Font("Ariel", 1, 50));
g.drawString(String.valueOf(player1.score), width / 2 - 70, 50);
g.setFont(new Font("Ariel", 1, 50));
g.drawString(String.valueOf(player2.score), width / 2 + 65, 50);
player1.render(g);
player2.render(g);
ball.render(g);
}
if (gameStatus == 1) {
g.setColor(Color.WHITE);
g.setFont(new Font("Ariel", 1, 50));
g.drawString("PAUSED", width / 2 - 100, 50);
}
}
public void start() {
player1 = new paddle(this, 1);
player2 = new paddle(this, 2);
ball = new Ball(this);
gameStatus = 2;
}
public void update() {
if (w) {
player1.move(true);
} else if (s) {
player1.move(false);
} else if (!bot) {
if (up) {
player2.move(true);
}
if (down) {
player2.move(false);
}
} else {
if (botCoolDown > 0) {
botCoolDown--;
if (botCoolDown == 0) {
botMoves = 0;
}
}
if (botMoves < 10) {
if (player2.y + player2.height < ball.y) {
player2.move(false);
botMoves++;
}
if (player2.y + player2.height > ball.y) {
player2.move(true);
botMoves++;
}
if(botDifficulty == 0)
botCoolDown = 30;
else if(botDifficulty == 1)
botCoolDown = 20;
else if(botDifficulty == 2)
botCoolDown = 10;
}
}
ball.update(player1, player2);
}
public void actionPerformed(ActionEvent e) {
if (gameStatus == 2) {
update();
}
renderer.repaint();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
rectAnimation = new RectAnimation();
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
int id = e.getKeyCode();
if (id == KeyEvent.VK_W) {
w = true;
} else if (id == KeyEvent.VK_S) {
s = true;
} else if (id == KeyEvent.VK_UP) {
up = true;
} else if (id == KeyEvent.VK_DOWN) {
down = true;
} else if (id == KeyEvent.VK_ESCAPE && gameStatus == 2) {
gameStatus = 0;
} else if (id == KeyEvent.VK_SHIFT && gameStatus == 0) {
bot = true;
start();
}
if (id == KeyEvent.VK_SPACE) {
if (gameStatus == 0) {
start();
bot = false;
} else if (gameStatus == 1) {
gameStatus = 2;
} else if (gameStatus == 2) {
gameStatus = 1;
}
}
}
public void keyReleased(KeyEvent e) {
int id = e.getKeyCode();
if (id == KeyEvent.VK_W) {
w = false;
}
if (id == KeyEvent.VK_S) {
s = false;
}
if (id == KeyEvent.VK_UP) {
up = false;
}
if (id == KeyEvent.VK_DOWN) {
down = false;
}
}
}