initial commit

main
Tylan Tyson 12 months ago
commit 565662e92e

@ -0,0 +1,256 @@
# Example file showing a basic pygame "game loop"
import pygame
import math
# pygame setup
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True
class Ball ( object ) :
def __init__ ( self, x, y ) :
self.x = x
self.y = y
self.width = 20
self.height = 20
self.x_vel = 200
self.y_vel = 200
def draw ( self ) :
pygame.draw.rect(screen, pygame.Color(0, 255, 0, 255), pygame.Rect(self.x, self.y, self.width, self.height))
def update ( self, dt, width, height, players ) :
if self.x > width :
self.x_vel *= -1
players[0].points += 1
if self.x < 0 :
self.x_vel *= -1
if self.y > height or self.y < 0:
self.y_vel *= -1
for player in players :
if self.y > player.y and self.y < player.y + player.height :
if self.x > player.x and self.x < player.x + player.width :
self.x_vel *= -1
self.x += self.x_vel * dt
self.y += self.y_vel * dt
def draw_points ( self, rects, length ) :
num_points = 360
size = length
vertices = []
for i in range(num_points) :
angle = 2 * math.pi * i / num_points
x = (self.x + self.width * math.cos(angle)) + self.width / 2
y = (self.y + self.height * math.sin(angle)) + self.height / 2
end_x = (self.x + size * math.cos(angle)) + self.width / 2
end_y = (self.y + size * math.sin(angle)) + self.height / 2
current_end_x = end_x
current_end_y = end_y
for i2 in range(len(rects)) :
(ex, ey) = line_rect_intersection(x, y, end_x, end_y, rects[i2], size)
if find_line_length(x, y, ex, ey) < find_line_length(x, y, current_end_x, current_end_y):
current_end_x = ex
current_end_y = ey
vertices.append([current_end_x, current_end_y])
#pygame.draw.line(screen, pygame.Color(255, 255, 255, 255), ( x, y ), ( current_end_x, current_end_y ))
surface2 = pygame.Surface((screen.get_width(),screen.get_height()))
surface2.set_colorkey((0,0,0))
surface2.set_alpha(80)
pygame.draw.polygon(surface2, pygame.Color(0, 0, 255, a = 255), vertices)
return pygame.mask.from_surface(surface2), surface2
def find_line_length(start_x, start_y, end_x, end_y):
dx = end_x - start_x
dy = end_y - start_y
distance_squared = dx * dx + dy * dy
return math.sqrt(distance_squared)
def line_rect_intersection(start_x, start_y, end_x, end_y, rect, max_size):
# end
t_min = 0.0
t_max = 1.0
dx = end_x - start_x
dy = end_y - start_y
p1 = [-dx, dx, -dy, dy]
p2 = [start_x - rect.x, rect.x + rect.width - start_x, start_y - rect.y, rect.y + rect.height - start_y]
for i in range(4):
if p1[i] == 0.0:
if p2[i] < 0.0:
return (end_x, end_y) # Line is outside the rectangle, return the original end point
else:
t = p2[i] / p1[i]
if p1[i] < 0.0:
t_min = max(t_min, t)
else:
t_max = min(t_max, t)
if t_min > t_max:
return (end_x, end_y) # Line is outside the rectangle, return the original end point
collision_x = start_x + t_min * dx
collision_y = start_y + t_min * dy
return (collision_x, collision_y)
class Player ( object ) :
def __init__ ( self, x ) :
self.x = x
self.y = 10
self.height = 100
self.width = 20
self.up = False
self.down = False
self.movement_speed = 400
self.points = 0
def draw ( self ) :
pygame.draw.rect(screen, pygame.Color(255, 0, 0, 255), pygame.Rect(self.x, self.y, self.width, self.height))
def update ( self, dt ) :
if self.up :
self.y -= self.movement_speed * dt
if self.down :
self.y += self.movement_speed * dt
getTicksLastFrame = 0
player = Player(10)
player2 = Player(screen.get_width() - 30)
ball = Ball(screen.get_width() / 2, screen.get_height() / 2)
while running:
t = pygame.time.get_ticks()
# deltaTime in seconds.
deltaTime = (t - getTicksLastFrame) / 1000.0
getTicksLastFrame = t
# poll for events
# pygame.QUIT event means the user clicked X to close your window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.dict.get("unicode") == "w" :
player.up = True
if event.dict.get("unicode") == "s" :
player.down = True
# Up
if event.dict.get("key") == 1073741906:
player2.up = True
if event.dict.get("key") == 1073741905:
player2.down = True
if event.type == pygame.KEYUP :
if event.dict.get("unicode") == "w" :
player.up = False
if event.dict.get("unicode") == "s" :
player.down = False
# Up
if event.dict.get("key") == 1073741906:
player2.up = False
if event.dict.get("key") == 1073741905:
player2.down = False
# fill the screen with a color to wipe away anything from last frame
screen.fill("black")
# RENDER YOUR GAME HERE
player.update(dt=deltaTime)
player2.update(dt=deltaTime)
ball.update(dt=deltaTime, width=screen.get_width(), height=screen.get_height(), players=[player, player2])
ball.draw()
####
player.draw()
player2.draw()
mask, surface5 = ball.draw_points(rects=[pygame.Rect(200, 200, 20, 20)], length=50)
mask, surface6 = ball.draw_points(rects=[pygame.Rect(200, 200, 20, 20)], length=100)
mask, surface7 = ball.draw_points(rects=[pygame.Rect(200, 200, 20, 20)], length=150)
mask, surface8 = ball.draw_points(rects=[pygame.Rect(200, 200, 20, 20)], length=200)
filter = pygame.surface.Surface((screen.get_width(), screen.get_height()))
filter.fill(pygame.color.Color(200, 200,200))
filter.blit(surface5, (0, 0))
filter.blit(surface6, (0, 0))
filter.blit(surface7, (0, 0))
filter.blit(surface8, (0, 0))
screen.blit(filter, (0, 0), special_flags=pygame.BLEND_RGBA_SUB)
filter = pygame.surface.Surface((screen.get_width(), screen.get_height()))
filter.fill(pygame.color.Color(0, 0,0))
filter.blit(surface5, (0, 0))
filter.blit(surface6, (0, 0))
filter.blit(surface7, (0, 0))
filter.blit(surface8, (0, 0))
screen.blit(filter, (0, 0), special_flags=pygame.BLEND_RGBA_ADD)
screen.blit(mask.to_surface(), (0, 0), special_flags=pygame.BLEND_RGBA_MIN)
####
pygame.draw.rect(screen, pygame.Color(100, 100, 100), pygame.Rect(200, 200, 20, 20))
text_surface = pygame.font.Font(pygame.font.get_default_font(), 48).render(str(player.points), False, pygame.Color(200, 200, 200))
screen.blit(text_surface, ( 100, 100 ))
text_surface = pygame.font.Font(pygame.font.get_default_font(), 48).render(str(int( 1 / deltaTime)), False, pygame.Color(0, 255, 0))
screen.blit(text_surface, ( screen.get_width() - 200, screen.get_height() - 100 ))
# flip() the display to put your work on screen
pygame.display.flip()
clock.tick(60) # limits FPS to 60
pygame.quit()

@ -0,0 +1 @@
pygame==2.5.2

75
rust/Cargo.lock generated

@ -0,0 +1,75 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "Engine"
version = "0.1.0"
dependencies = [
"raylib",
]
[[package]]
name = "cc"
version = "1.0.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
dependencies = [
"libc",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cmake"
version = "0.1.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a31c789563b815f77f4250caee12365734369f942439b7defd71e18a48197130"
dependencies = [
"cc",
]
[[package]]
name = "fs_extra"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.148"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b"
[[package]]
name = "raylib"
version = "3.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb567269b7ea9ae3c4a5aab4dc95e0b4d8df2a49a25e1670a3bb17bc17504606"
dependencies = [
"cfg-if",
"lazy_static",
"libc",
"raylib-sys",
]
[[package]]
name = "raylib-sys"
version = "3.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20c97b5e251b73c52183914d4756104cab401050f244c19abe83fa05a4e86840"
dependencies = [
"cc",
"cmake",
"fs_extra",
]

@ -0,0 +1,9 @@
[package]
name = "Engine"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
raylib = "*"

@ -0,0 +1,257 @@
use raylib::{prelude::*, ffi::{GetMouseX, GetMouseY}};
fn find_line_length ( start_x : f32, start_y : f32, end_x : f32, end_y : f32 ) -> f32 {
let dx = end_x - start_x;
let dy = end_y - start_y;
let distance_squared = dx * dx + dy * dy;
return distance_squared.sqrt();
}
struct Player {
pos : Vector2,
size : f32
}
impl Player {
fn new (x: f32) -> Self {
Self {
pos: Vector2 { x: x, y: 500f32 },
size: 30f32
}
}
fn draw(&self, drawHandle: &mut RaylibDrawHandle) {
drawHandle.draw_circle(self.pos.x as i32, self.pos.y as i32, self.size, Color::BLUE);
}
fn draw_points(&self, draw_handle: &mut RaylibDrawHandle, rects: &Vec<Box>) {
let num_points = 10000;
let size = 400f32;
let mut vertices: Vec<Vector2> = vec![Vector2::default(); num_points];
for i in 0..num_points {
let angle = 2.0 * std::f64::consts::PI * (i as f64) / (num_points as f64);
let x = self.pos.x + self.size * angle.cos() as f32;
let y = self.pos.y + self.size * angle.sin() as f32;
let end_x = self.pos.x + size * angle.cos() as f32; // Adjust the factor for desired line length
let end_y = self.pos.y + size * angle.sin() as f32; // Adjust the factor for desired line length
// End
let mut currentEndX : f32 = end_x;
let mut currentEndY : f32 = end_y;
for i2 in 0..rects.len() {
// Box End
let (ex, ey) = line_rect_intersection(x, y, end_x, end_y, &rects[i2].rect, size);
if find_line_length(x, y, ex, ey) < find_line_length(x, y, currentEndX, currentEndY) {
currentEndX = ex;
currentEndY = ey;
}
}
vertices[i] = Vector2 { x: currentEndX, y: currentEndY };
//draw_handle.draw_line(x as i32, y as i32, currentEndX as i32, currentEndY as i32, Color::GREEN);
}
// Connect
for i in 1..vertices.len() {
draw_handle.draw_line(vertices[i - 1].x as i32, vertices[i - 1].y as i32, vertices[i].x as i32, vertices[i].y as i32, Color::RED);
}
draw_handle.draw_line(vertices[vertices.len()-1].x as i32, vertices[vertices.len()-1].y as i32, vertices[0].x as i32, vertices[0].y as i32, Color::RED);
//let c_string = CString::new("./src/red.png").expect("CString conversion failed");
//let texture: Texture = unsafe { LoadTexture(c_string.as_ptr()) };
//let custom_texture = CustomTexture { texture };
//draw_handle.draw_texture_poly(custom_texture, self.pos, &vertices, &vertices, Color::RED);
}
fn update(&mut self) {
self.pos.x = unsafe { GetMouseX() } as f32;
self.pos.y = unsafe {
GetMouseY()
} as f32;
}
}
fn line_rect_intersection(start_x: f32, start_y: f32, end_x: f32, end_y: f32, rect: &Rectangle, max_size : f32) -> (f32, f32) {
// Only Check Close
const max: f32 = ((60 * 60) + (60 * 60)) as f32;
let difx = rect.x - start_x;
let dify = rect.y - start_y;
let mag = difx * difx + dify * dify;
if mag.sqrt() > max_size - 1f32 + max.sqrt() {
return (end_x,end_y)
}
// end
let mut t_min: f32 = 0.0;
let mut t_max: f32 = 1.0;
let dx = end_x - start_x;
let dy = end_y - start_y;
let p1 = [-dx, dx, -dy, dy];
let p2 = [start_x - rect.x, rect.x + rect.width - start_x, start_y - rect.y, rect.y + rect.height - start_y];
for i in 0..4 {
if p1[i] == 0.0 {
if p2[i] < 0.0 {
return (end_x, end_y); // Line is outside the rectangle, return the original end point
}
} else {
let t = p2[i] / p1[i];
if p1[i] < 0.0 {
t_min = t_min.max(t);
} else {
t_max = t_max.min(t);
}
}
}
if t_min > t_max {
return (end_x, end_y ); // Line is outside the rectangle, return the original end point
}
let collision_x = start_x + t_min * dx;
let collision_y = start_y + t_min * dy;
return (collision_x, collision_y);
}
struct Box {
rect : Rectangle
}
impl Box {
fn new (xPosition: f32, yPosition: f32) -> Self {
Self {
rect: Rectangle { x: xPosition, y: yPosition, width: 60f32, height: 60f32 }
}
}
fn draw(&self, drawHandle: &mut RaylibDrawHandle) {
drawHandle.draw_rectangle(self.rect.x as i32, self.rect.y as i32, self.rect.width as i32, self.rect.height as i32, Color::PURPLE);
}
}
struct Tilemap {
data: Vec<Box>
}
impl Tilemap {
fn new ( map: [[&str; 32]; 18]) -> Self {
let mut data_final: Vec<Box> = Vec::new();
for row in 0..map.len() {
for column in 0..map[row].len() {
if map[row][column] == "1" {
let boxx: Box = Box::new(60f32 * column as f32, 60f32 * row as f32);
data_final.push(boxx);
}
}
}
return Tilemap { data: data_final }
}
fn draw (&self, drawHandle: &mut RaylibDrawHandle) {
for item_number in 0..self.data.len() {
self.data[item_number].draw(drawHandle)
}
}
}
fn main() {
let world = [
["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"],
["1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1"],
["1", "0", "1", "1", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1"],
["1", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1"],
["1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1"],
["1", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1"],
["1", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1"],
["1", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1"],
["1", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "1", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1"],
["1", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1"],
["1", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1"],
["1", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1"],
["1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1"],
["1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1"],
["1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1"],
["1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1"],
["1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1"],
["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"]
];
let tilemap = Tilemap::new(world);
let mut player = Player::new(500f32);
let ( mut rl, thread ) = raylib::init().size(1920, 1080).title("Title").build();
while !rl.window_should_close() {
let mut d = rl.begin_drawing(&thread);
d.clear_background(Color::DARKGRAY);
player.update();
player.draw(&mut d);
tilemap.draw(&mut d);
player.draw_points(&mut d, &tilemap.data);
d.draw_fps(12, 12);
}
}
Loading…
Cancel
Save