pixelpride/src/main.rs

186 lines
6.2 KiB
Rust

use std::io::{BufRead, BufReader, Read, Write};
use std::net::TcpStream;
use std::sync::{Arc, RwLock};
use std::{thread, time};
use std::thread::sleep;
use std::io::Cursor;
use image::GenericImageView;
use image::io::Reader as ImageReader;
use rand::seq::SliceRandom;
fn main() {
img();
}
fn img(){
thread::spawn(|| {
let img = ImageReader::open("dvd.png").unwrap().decode().unwrap();
let (x_max, y_max) = img.dimensions();
let canvas_x = 1280;
let canvas_y = 700;
let mut stream = TcpStream::connect("schenklflut.de:1234").unwrap();
let mut x_shift = 0;
let mut y_shift = 0;
let mut x_shift_change:i32 = 5;
let mut y_shift_change: i32 = 5;
let mut colour : String = "00ff00".to_string();
loop{
if calculate_new_x_value(x_shift, x_shift_change, x_max) > canvas_x{ //Touched east
x_shift_change = -5;
colour = random_colour(colour);
}
if calculate_new_x_value(x_shift, x_shift_change, x_max) < 0{ //Touched west
x_shift_change = 5;
colour = random_colour(colour);
}
if calculate_new_y_value(y_shift, y_shift_change, y_max) > canvas_y{ //Touched south
y_shift_change = -5;
colour = random_colour(colour);
}
if calculate_new_y_value(y_shift, y_shift_change, y_max) < 0{ //Touched north
y_shift_change = 5;
colour = random_colour(colour);
}
x_shift = x_shift + x_shift_change;
y_shift = y_shift + y_shift_change;
for x in 1..x_max{
for y in 1..y_max{
let pixel = img.get_pixel(x, y);
if pixel[3] > 0{
let command = format!("PX {} {} {}\n", x_shift +x as i32, y_shift +y as i32, colour);
stream.write_all(command.as_str().as_ref()).unwrap();
}/*else{
let command = format!("PX {} {} {}\n", x_shift +x as i32, y_shift +y as i32, "ffffff");
stream.write_all(command.as_str().as_ref()).unwrap();
}*/
}
}
}
fn random_colour(colour: String) -> String{
let mut new = colour.clone();
let colours = vec!["00ff00", "d1d1d1", "5BCEFA", "DD0000", "FF0000"];
while new == colour{
new = colours.choose(&mut rand::thread_rng()).unwrap().to_string();
}
return new;
}
}).join().unwrap();
}
fn calculate_new_x_value(x_pos: i32, x_shift_change:i32, x_max:u32) -> i32{
if x_shift_change > 0{
return x_pos+x_shift_change+(x_max as i32);
}else{
return x_pos+x_shift_change;
}
}
fn calculate_new_y_value(y_pos: i32, y_shift_change:i32, y_max:u32) -> i32{
if y_shift_change > 0{
return y_pos+y_shift_change+(y_max as i32);
}else{
return y_pos+y_shift_change;
}
}
fn pride(){
let mut y_offset_org = Arc::new(RwLock::new(0));
let y_offset = Arc::clone(&y_offset_org);
thread::spawn(move || {
let mut stream = TcpStream::connect("schenklflut.de:1234").unwrap();
println!("Connected to the server!");
loop {
let yo : i32 = y_offset.read().unwrap().clone();
for x in 1..1280 {
for y in yo+1..yo+25 {
let command = format!("PX {} {} 5BCEFA\n", x, y);
stream.write_all(command.as_str().as_ref()).unwrap()
}
}
}
});
let y_offset = Arc::clone(&y_offset_org);
thread::spawn(move|| {
let mut stream = TcpStream::connect("schenklflut.de:1234").unwrap();
println!("Connected to the server!");
loop {
let yo : i32 = y_offset.read().unwrap().clone();
for x in 1..1280 {
for y in yo+25..yo+50 {
let command = format!("PX {} {} F5A9B8\n", x, y);
stream.write_all(command.as_str().as_ref()).unwrap()
}
}
}
});
let y_offset = Arc::clone(&y_offset_org);
thread::spawn(move|| {
let mut stream = TcpStream::connect("schenklflut.de:1234").unwrap();
println!("Connected to the server!");
loop {
let yo : i32 = y_offset.read().unwrap().clone();
for x in 1..1280 {
for y in yo+50..yo+75 {
let command = format!("PX {} {} FFFFFF\n", x, y);
stream.write_all(command.as_str().as_ref()).unwrap()
}
}
}
});
let y_offset = Arc::clone(&y_offset_org);
thread::spawn(move|| {
let mut stream = TcpStream::connect("schenklflut.de:1234").unwrap();
println!("Connected to the server!");
loop {
let yo : i32 = y_offset.read().unwrap().clone();
for x in 1..1280 {
for y in yo+75..yo+100 {
let command = format!("PX {} {} F5A9B8\n", x, y);
stream.write_all(command.as_str().as_ref()).unwrap()
}
}
}
});
let y_offset = Arc::clone(&y_offset_org);
thread::spawn(move|| {
let mut stream = TcpStream::connect("schenklflut.de:1234").unwrap();
println!("Connected to the server!");
loop {
let yo : i32 = y_offset.read().unwrap().clone();
for x in 1..1280 {
for y in yo+100..yo+125 {
let command = format!("PX {} {} 5BCEFA\n", x, y);
stream.write_all(command.as_str().as_ref()).unwrap()
}
}
}
});
thread::spawn(move|| {
loop {
let temp = y_offset_org.read().unwrap().clone();
if temp + 10 < 720 {
*y_offset_org.write().unwrap() = temp + 10;
} else {
*y_offset_org.write().unwrap() = 0;
}
println!("Set y_offset to: {}", temp + 10);
sleep(time::Duration::from_millis(100));
println!("Woke up from sleep");
}
}).join().unwrap();
}