Created
June 29, 2014 15:46
-
-
Save fredriksa/ddb0464ed5e5dd545708 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
require 'gosu' | |
class GameWindow < Gosu::Window | |
def initialize | |
super 640, 480, false | |
self.caption = "Client" | |
@image = Gosu::Image.new(self,"headcrab.bmp",false) | |
@x, @y = get("x"), get("y") | |
end | |
def start | |
@server = UDPSocket.new | |
@server.bind("25.50.161.112", 7501) | |
@thread_accept = Thread.new { | |
running = true | |
while running | |
puts "received socket" | |
content, sender = @server.recvfrom(16) | |
puts content | |
end | |
} | |
end | |
def update | |
@x, @y = get("x"), get("y") | |
end | |
def draw | |
@image.draw(@x.to_i, @y.to_i, 5) | |
end | |
def get(key) | |
request("GET #{key}") | |
end | |
def set(key) | |
request("SET #{key}") | |
end | |
def request(string) | |
@client = UDPSocket.new | |
@client.send(string, 0, "25.50.161.112",7500) | |
end | |
def button_down(id) | |
case id | |
when Gosu::KbLeft | |
set("left true") | |
when Gosu::KbRight | |
set("right true") | |
end | |
end | |
def button_up(id) | |
case id | |
when Gosu::KbLeft | |
set("left false") | |
when Gosu::KbRight | |
set("right false") | |
end | |
end | |
end | |
@game = GameWindow.new | |
@game.start | |
@game.show |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
class Server | |
def initialize(port) | |
@server = UDPSocket.new | |
@server.bind("25.50.161.112", port) | |
puts "Server listening on #{@server.local_address.ip_port}" | |
@storage = {"x" => 250, "y" => 250, "left" => false, "right" => false} | |
end | |
def start | |
@thread_update = Thread.new { | |
running = true | |
while running | |
sleep(0.0167) | |
update | |
end | |
} | |
@thread_accept = Thread.new { | |
running = true | |
while running | |
puts "sent socket 1" | |
content, sender = @server.recvfrom(16) | |
puts "sent socket 2" | |
@sender = UDPSocket.new | |
@sender.send(process(content), 0, sender[3], 7500) | |
puts "sent socket 3" | |
end | |
} | |
@thread_update.join | |
@thread_accept.join | |
end | |
def process(request) | |
case command.upcase | |
when 'GET' | |
@storage[key] | |
when 'SET' | |
@storage[key] = value | |
end | |
end | |
def update | |
if @storage["left"] == "true" | |
@storage["x"] -= 0.5 | |
elsif @storage["right"] == "true" | |
@storage["x"] += 0.5 | |
end | |
end | |
end | |
@server = Server.new(7500) | |
@server.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment