fixed inf momentum bs
This commit is contained in:
parent
94711d37ca
commit
092bce84a6
95
src/main.rs
95
src/main.rs
|
@ -1,5 +1,6 @@
|
||||||
use cat_box::{get_keyboard_state, physics::check_for_collision, Game, Sprite};
|
use cat_box::{get_keyboard_state, physics::check_for_collision, Game, Sprite, draw_text, TextMode};
|
||||||
use sdl2::keyboard::Scancode;
|
use sdl2::keyboard::{Scancode/*,Keycode */};
|
||||||
|
//use sdl2::event::Event;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
mod ball;
|
mod ball;
|
||||||
|
@ -26,34 +27,45 @@ fn main() {
|
||||||
let mut dir = Direction::Still;
|
let mut dir = Direction::Still;
|
||||||
let mut dir_enemy = Direction::Still;
|
let mut dir_enemy = Direction::Still;
|
||||||
|
|
||||||
|
let mode = TextMode::Transparent {
|
||||||
|
colour: (255,255,255),
|
||||||
|
};
|
||||||
|
let mut score_player = 0;
|
||||||
|
let mut score_enemy = 0;
|
||||||
|
|
||||||
let mut now = Instant::now();
|
let mut now = Instant::now();
|
||||||
|
|
||||||
game.run(|ctx| {
|
game.run(|ctx| {
|
||||||
ctx.set_background_colour(0, 0, 0);
|
ctx.set_background_colour(0, 0, 0);
|
||||||
|
|
||||||
let (x_player, y_player) = paddle.position().into();
|
let (x_player, y_player) = paddle.position().into();
|
||||||
let (x_enemy, y_enemy) = paddle_enemy.position().into();
|
let (x_enemy, y_enemy) = paddle_enemy.position().into();
|
||||||
|
|
||||||
let (x_ball, y_ball) = ball.sprite.position().into();
|
let (x_ball, y_ball) = ball.sprite.position().into();
|
||||||
|
|
||||||
|
if now.elapsed().as_millis() >= 1{
|
||||||
|
let keys = get_keyboard_state(ctx).keys;
|
||||||
|
//let (_tex, _canvas, e) = ctx.inner();
|
||||||
|
|
||||||
let keys = get_keyboard_state(ctx).keys;
|
for key in &keys {
|
||||||
for key in &keys {
|
match key {
|
||||||
match key {
|
Scancode::W | Scancode::Up => {
|
||||||
Scancode::W | Scancode::Up => {
|
dir = Direction::Up;
|
||||||
dir = Direction::Up;
|
held = true;
|
||||||
held = true;
|
}
|
||||||
}
|
Scancode::S | Scancode::Down => {
|
||||||
Scancode::S | Scancode::Down => {
|
dir = Direction::Down;
|
||||||
dir = Direction::Down;
|
held = true;
|
||||||
held = true;
|
}
|
||||||
}
|
_ => {
|
||||||
_ => {
|
dir = Direction::Still;
|
||||||
dir = Direction::Still;
|
held = false;
|
||||||
held = false;
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if y_enemy < y_ball {
|
if y_enemy < y_ball {
|
||||||
dir_enemy = Direction::Down;
|
dir_enemy = Direction::Down;
|
||||||
}
|
}
|
||||||
|
@ -63,22 +75,35 @@ fn main() {
|
||||||
|
|
||||||
if now.elapsed().as_millis() >= 10 {
|
if now.elapsed().as_millis() >= 10 {
|
||||||
//BALL
|
//BALL
|
||||||
if check_for_collision(&paddle, &ball.sprite) {
|
if check_for_collision(&paddle, &ball.sprite) && x_ball == 50{
|
||||||
ball.vel_y += (ball.vel_y * 1);
|
if ball.vel_y < i32::MAX - 1
|
||||||
ball.vel_x *= -1;
|
{
|
||||||
ball.vel_y *= -1;
|
ball.vel_y += ball.vel_y * 1;
|
||||||
|
ball.vel_x *= -1;
|
||||||
|
ball.vel_y *= -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if check_for_collision(&paddle_enemy, &ball.sprite) {
|
if check_for_collision(&paddle_enemy, &ball.sprite) && x_ball == 449 {
|
||||||
ball.vel_y += (ball.vel_y * 1);
|
if ball.vel_y < i32::MAX - 1
|
||||||
ball.vel_x *= -1;
|
{
|
||||||
ball.vel_y *= -1;
|
ball.vel_y += ball.vel_y * 1;
|
||||||
|
ball.vel_x *= -1;
|
||||||
|
ball.vel_y *= -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if y_ball < 0 || y_ball > 500 {
|
if y_ball < 0 || y_ball > 500 {
|
||||||
ball.vel_y *= -1;
|
ball.vel_y *= -1;
|
||||||
}
|
}
|
||||||
if x_ball < 0 || x_ball > 500 {
|
if x_ball < 0
|
||||||
|
{
|
||||||
|
ball.sprite.set_position((250,250));
|
||||||
|
ball::Ball::rand_angle(&mut ball);
|
||||||
|
score_enemy += 1;
|
||||||
|
}
|
||||||
|
else if x_ball > 500 {
|
||||||
ball.sprite.set_position((250, 250));
|
ball.sprite.set_position((250, 250));
|
||||||
ball::Ball::rand_angle(&mut ball);
|
ball::Ball::rand_angle(&mut ball);
|
||||||
|
score_player += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ball.sprite.translate((ball.vel_x, ball.vel_y));
|
ball.sprite.translate((ball.vel_x, ball.vel_y));
|
||||||
|
@ -113,10 +138,10 @@ fn main() {
|
||||||
if held {
|
if held {
|
||||||
match dir {
|
match dir {
|
||||||
Direction::Up => {
|
Direction::Up => {
|
||||||
paddle.translate((0, 2));
|
paddle.translate((0, 3));
|
||||||
}
|
}
|
||||||
Direction::Down => {
|
Direction::Down => {
|
||||||
paddle.translate((0, -2));
|
paddle.translate((0, -3));
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
};
|
};
|
||||||
|
@ -124,9 +149,17 @@ fn main() {
|
||||||
now = Instant::now();
|
now = Instant::now();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
draw_text(ctx, format!("{score_player}"), "fira.ttf", 50, (90, 100), mode).unwrap();
|
||||||
|
draw_text(ctx, format!("{score_enemy}"), "fira.ttf", 50, (500-90, 100), mode).unwrap();
|
||||||
|
|
||||||
|
|
||||||
ball.sprite.draw(ctx).unwrap();
|
ball.sprite.draw(ctx).unwrap();
|
||||||
paddle.draw(ctx).unwrap();
|
paddle.draw(ctx).unwrap();
|
||||||
paddle_enemy.draw(ctx).unwrap();
|
paddle_enemy.draw(ctx).unwrap();
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* fn is_pressed(e: &sdl2::EventPump, k: Scancode) -> bool {
|
||||||
|
e.keyboard_state().is_scancode_pressed(k)
|
||||||
|
} */
|
||||||
|
|
Loading…
Reference in a new issue