Thursday, March 3, 2011

My first Chrome Ext: Scroll top/bottom

So today i actually got down into some code and wrote an extension for chrome that scrolls you to the top and/or bottom of the page instantly none of that slow scroll crap
using pure javascript without the magical jquery library :)
you can download it here
lets take a look the code i used

Simply created 2 Elements and populated their properties so they would stay on top and scroll with the page thanks to CSS

and i simply set their href to a javascript injection of scroll(x-axis,y-axis)

Its nothing too fancy but I find it useful for many things :)

Wednesday, March 2, 2011

Java Post Requests Class

import java.io.*;
import java.net.*;
import javax.swing.*;
public class Requests {
static String domain;
static URL myurl;
static URLConnection myurlc;
static OutputStreamWriter osr;
static BufferedReader br;
static String line;
static String resp;
static StringBuilder sb;
public Requests(String d) {
domain = d;
}
public String sendPost(String data) {
try {
sb = new StringBuilder();
myurl = new URL(domain);
myurlc = myurl.openConnection();
myurlc.setDoOutput(true);
osr = new OutputStreamWriter(myurlc.getOutputStream());
osr.write(data);
osr.flush();
br = new BufferedReader(new InputStreamReader(myurlc.getInputStream()));
while((line = br.readLine()) != null)
{
sb.append(line);
}
br.close();
osr.close();
}
catch(IOException e)
{
JOptionPane.showMessageDialog(null,"IO Error");
}
finally {
return sb.toString();
}
}
}
view raw requests.java hosted with ❤ by GitHub

Tuesday, March 1, 2011

Pygame Game and Player Class

This is a simple python class I wrote back when I was playing around with pygame its very basic and I used it as a foundation for my game projects
import pygame
from pygame.locals import *
class Game:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((640,480))
self.running = 1
self.clock = pygame.time.Clock()
self.rect = pygame.Rect(0,0,640,480)
def reDraw(self):
pygame.display.flip()
def tick(self,fps):
self.clock.tick(fps)
class Player:
def __init__(self,n):
self.name = n
self.x = 0
self.y = 0
self.vx = 0
self.vy = 0
self.speed = 1
self.image = None
self.rect = None
def getInput(self,inp):
if inp.type == KEYDOWN and inp.key == K_DOWN:
self.mvDown()
elif inp.type == KEYUP and inp.key == K_DOWN:
self.stopVertical()
elif inp.type == KEYDOWN and inp.key == K_UP:
self.mvUp()
elif inp.type == KEYUP and inp.key == K_UP:
self.stopVertical()
elif inp.type == KEYDOWN and inp.key == K_RIGHT:
self.mvRight()
elif inp.type == KEYUP and inp.key == K_RIGHT:
self.stopHoriz()
elif inp.type == KEYDOWN and inp.key == K_LEFT:
self.mvLeft()
elif inp.type == KEYUP and inp.key == K_LEFT:
self.stopHoriz()
def setImage(self,i):
self.image = pygame.image.load(i)
self.rect = self.image.get_rect()
def mvUp(self):
self.vy -= self.speed
def mvDown(self):
self.vy += self.speed
def mvLeft(self):
self.vx -= self.speed
def mvRight(self):
self.vx += self.speed
def stopVertical(self):
self.vy = 0
def stopHoriz(self):
self.vx = 0
def Move(self):
self.x += self.vx
self.y += self.vy
self.rect = pygame.Rect(self.x,self.y,self.image.get_width(),self.image.get_height())
def Draw(self,scr):
scr.blit(self.image,(self.x,self.y))
game = Game()
player = Player("Player")
player.setImage("player_image.bmp")
while game.running == 1:
for x in pygame.event.get():
player.getInput(x)
if x.type == QUIT:
game.running = 0
pygame.quit()
game.tick(90)
game.screen.fill((0,0,0))
player.Move()
player.Draw(g.screen)
game.reDraw()
view raw game.py hosted with ❤ by GitHub