Showing posts with label Code. Show all posts
Showing posts with label Code. Show all posts

Friday, June 14, 2013

Map Generation - Finish Him!

I've got some working code for the Map Gen class I've been working on. There are some issues with placing rooms on top of existing stuff, and I'd like to code something to place walls on the outside of the rectangles that make up my rooms and hallways. But at least it's working now, and it's been pretty fun to work on up to this point.

Loaded the mapgen class into a pygame testbed

I know this is a crappy screenshot, but you can see that there are several rooms (the black rectangles) and hall-objects that connect them. Ignore the crappy images I used for the rooms and player objects. I know they suck, but I've been using them as my standard test images for a while now.

Currently the room generator chooses from a number of pre-loaded images and scales the image to fit the random size. I'm thinking I may change it to tile a floor pattern on the room after a size is chosen. This way I don't end up with floors that are clearly the same image, just stretched to fit. Either that, or just use the same floors and have the room generators just draw random carpets or mats or something to draw attention from the stretched image underneath...

I also want to place some generic walls on each room/hall then add random pictures, posters and windows.

For now though, you just import the class, and run genMaps() and it returns a list (or array if you prefer) of these room/hall objects. You can then use the pygame rectangle objects for collisions  and the pygame image (surface) objects to draw them to another surface.

Here's the source for this class file:
Click here to expand Source
import random, pygame

class RoomObj:
  def update(self,anchorpoint):
    self.rect.left = self.xoffset + anchorpoint[0]
    self.rect.top = self.yoffset + anchorpoint[1]
  
class Room(RoomObj):
  def __init__(self, imageset, direction=0, room_rect=None):
    #RoomObj.__init__(self)
    self.direction = random.randint(1,4) #up, down, left, right
    self.size = (random.randint(400,600),random.randint(500,700))
    self.image = pygame.Surface(self.size)
    pygame.transform.scale(random.choice(imageset), self.size, self.image)
    self.rect = self.image.get_rect()
    if direction==0:
      self.rect.top = 0
      self.rect.left = 0
    if direction==1:
      if self.direction == 2:
        self.direction = 1
      self.rect.bottom = room_rect.top
      self.rect.left = room_rect.left-(self.size[0]/2)
    if direction==2:
      if self.direction == 1:
        self.direction = 2
      self.rect.top = room_rect.bottom
      self.rect.left = room_rect.left-(self.size[0]/2)
    if direction==3:
      if self.direction == 4:
        self.direction = 3
      self.rect.top = room_rect.top-(self.size[1]/2)
      self.rect.right = room_rect.left
    if direction==4:
      if self.direction == 3:
        self.direction = 4
      self.rect.top = room_rect.top-(self.size[1]/2)
      self.rect.left = room_rect.right
    self.xoffset = self.rect.left
    self.yoffset = self.rect.top
  
  
class Hall(RoomObj):
  def __init__(self, direction, room_rect, imageset):
    #RoomObj.__init__(self)
    self.size = (80,random.randint(100,300))
    self.image = pygame.Surface(self.size)
    tmpimage = random.choice(imageset)
    if direction==1:
      self.image = pygame.transform.scale(tmpimage, self.size, self.image)
      self.rect = self.image.get_rect()
      self.rect.bottom = room_rect.top
      self.rect.left = random.randint(room_rect.left, room_rect.right-80)
      self.direction=1
    
    if direction==2:
      self.image = pygame.transform.scale(tmpimage, self.size, self.image)
      self.rect = self.image.get_rect()
      self.rect.top = room_rect.bottom
      self.rect.left = random.randint(room_rect.left, room_rect.right-80)
      self.direction=2
    
    if direction==3:
      self.size = (self.size[1],self.size[0])
      self.image = pygame.Surface(self.size)
      self.image = pygame.transform.rotate(tmpimage, 90)
      #print self.size
      #print self.image.get_size()
      self.image = pygame.transform.scale(tmpimage, self.image.get_size(), self.image)
      self.rect = self.image.get_rect()
      self.rect.top = random.randint(room_rect.top, room_rect.bottom-80)
      self.rect.right = room_rect.left
      self.direction=3
    
    if direction==4:
      self.size = (self.size[1],self.size[0])
      self.image = pygame.Surface(self.size)
      self.image = pygame.transform.rotate(tmpimage, 90)
      self.image = pygame.transform.scale(tmpimage, self.image.get_size(), self.image)
      self.rect = self.image.get_rect()
      self.rect.top = random.randint(room_rect.top, room_rect.bottom-80)
      self.rect.left = room_rect.right
      self.direction=4
    
    self.xoffset = self.rect.left
    self.yoffset = self.rect.top
  
def genMap(size,roomimages,hallimages):
  """
  takes 3 perameters: size (string), roomimages (array of pygame images/surfaces),
  and hallimages (also an array of pygame images)
 
  size should be small, medium or large
  the two arrays should use pygame.image.load() objects or pygame.Surface objects
  e.g. [pygame.image.load("file1.png").convert_alpha(), pygame.image.load("file2.jpg)]
 
  returns an array containing room and hall objects.
 
  each room or hall can be moved through the use of an anchor point variable.
  e.g. mapobjects[0].update(anchorpoint) where anchorpoint is the (x, y) offset
  """
  numofrooms=0
  if size=='small':
    numofrooms = random.randint(3,5)
  if size=='medium':
    numofrooms = random.randint(5,8)
  rooms = []
  map = []
  for i in range(numofrooms):
    if i == 0:
      tmproom = Room(roomimages)
      map.append(tmproom)
      tmphall = Hall(tmproom.direction, tmproom.rect, hallimages)
      map.append(tmphall)
    else:
      if i < len(range(numofrooms))-1:
        tmproom = Room(roomimages, tmphall.direction, tmphall.rect)
        map.append(tmproom)
        tmphall=Hall(tmproom.direction, tmproom.rect, hallimages)
        map.append(tmphall)
      else:
        map.append(Room(roomimages, tmphall.direction, tmphall.rect))
  return map

*Edited - 6-21-2013* I decided to go through the class file and make a few minor updates. I added some checking to see if there was an existing object where the new room was going to be placed. I also changed the logic a bit in the loop that created the Rooms/Halls. I also went through and commented it all. (Except the Room subclass; I figured it was close enough to the Hall, I didn't need to comment both)
In any case, here's the cleaned up class file:

Click here to expand the cleaned up Source
import random, pygame

class RoomObj:
  def __init__(self, wallimgset):
    if self.type == 'room':
      #create walls for a room
      pass
    if self.type == 'hall':
      #create walls for a room
      pass
  def update(self,anchorpoint):
    self.rect.left = self.xoffset + anchorpoint[0]
    self.rect.top = self.yoffset + anchorpoint[1]
  
class Room(RoomObj):
  def __init__(self, imageset, direction=0, room_rect=None, wallimgset=[]):
    #main room rectangle
    self.type = 'room'
    self.direction = random.randint(1,4) #up, down, left, right
    self.size = (random.randint(400,600),random.randint(500,700))
    self.image = pygame.Surface(self.size)
    pygame.transform.scale(random.choice(imageset), self.size, self.image)
    self.rect = self.image.get_rect()
    if direction==0:
      self.rect.top = 0
      self.rect.left = 0
    if direction==1:
      if self.direction == 2:
        self.direction = 1
      self.rect.bottom = room_rect.top
      self.rect.left = room_rect.left-(self.size[0]/2)
    if direction==2:
      if self.direction == 1:
        self.direction = 2
      self.rect.top = room_rect.bottom
      self.rect.left = room_rect.left-(self.size[0]/2)
    if direction==3:
      if self.direction == 4:
        self.direction = 3
      self.rect.top = room_rect.top-(self.size[1]/2)
      self.rect.right = room_rect.left
    if direction==4:
      if self.direction == 3:
        self.direction = 4
      self.rect.top = room_rect.top-(self.size[1]/2)
      self.rect.left = room_rect.right
    self.xoffset = self.rect.left
    self.yoffset = self.rect.top
    RoomObj.__init__(self,wallimgset)
   
   
class Hall(RoomObj):
  #initialize our hall object
  def __init__(self, direction, room_rect, imageset, wallimgset=[]):
    self.type = 'hall'
    #size may want to be changed to be relative to screen size
    self.size = (80,random.randint(100,300))
    #will be surface that is drawn to screen
    self.image = pygame.Surface(self.size)
    #will be drawn to self.image surface
    tmpimage = random.choice(imageset)
    #hall will be facing up and down
    if direction==1:
      self.image = pygame.transform.scale(tmpimage, self.size, self.image)
      self.rect = self.image.get_rect()
      self.rect.bottom = room_rect.top
      self.rect.left = random.randint(room_rect.left, room_rect.right-80)
      self.direction=1
    #hall will be up and down 
    if direction==2:
      self.image = pygame.transform.scale(tmpimage, self.size, self.image)
      self.rect = self.image.get_rect()
      self.rect.top = room_rect.bottom
      self.rect.left = random.randint(room_rect.left, room_rect.right-80)
      self.direction=2
    #hall will be left to right
    if direction==3:
      self.size = (self.size[1],self.size[0])
      self.image = pygame.Surface(self.size)
      self.image = pygame.transform.rotate(tmpimage, 90)
      self.image = pygame.transform.scale(tmpimage, self.image.get_size(), self.image)
      self.rect = self.image.get_rect()
      self.rect.top = random.randint(room_rect.top, room_rect.bottom-80)
      self.rect.right = room_rect.left
      self.direction=3
    #hall will be left to right
    if direction==4:
      self.size = (self.size[1],self.size[0])
      self.image = pygame.Surface(self.size)
      self.image = pygame.transform.rotate(tmpimage, 90)
      self.image = pygame.transform.scale(tmpimage, self.image.get_size(), self.image)
      self.rect = self.image.get_rect()
      self.rect.top = random.randint(room_rect.top, room_rect.bottom-80)
      self.rect.left = room_rect.right
      self.direction=4
    #set offset to use with the update function
    self.xoffset = self.rect.left
    self.yoffset = self.rect.top
    #init the parent class to create the walls and draw the floors
    RoomObj.__init__(self, wallimgset)

def checkForExisting(room, maplist):
  for i in maplist:
    if room.rect.contains(i.rect):
      return True
  return False
   
def genMap(size,roomimages,hallimages):
  """
  takes 3 perameters: size (string), roomimages (array of pygame images/surfaces),
  and hallimages (also an array of pygame images)
 
  size should be small, medium or large
  the two arrays should use pygame.image.load() objects or pygame.Surface objects
  e.g. [pygame.image.load("file1.png").convert_alpha(), pygame.image.load("file2.jpg)]
 
  returns an array containing room and hall objects.
 
  each room or hall can be moved through the use of an anchor point variable.
  e.g. mapobjects[0].update(anchorpoint) where anchorpoint is the (x, y) offset
  """
  #num of rooms will be number of nodes to create
  numofrooms=0
  if size=='small':
    numofrooms = random.randint(3,5)
  if size=='medium':
    numofrooms = random.randint(5,8)
  #add a few to account for joining hall nodes
  numofrooms = numofrooms+(numofrooms-1)
  rooms = []
  map = []
  #while map array length is less than total number of desired nodes...
  while len(map) < numofrooms:
    #first create a room and a hall
    if len(map)==0:
      tmproom = Room(roomimages)
      map.append(tmproom)
      tmphall = Hall(tmproom.direction, tmproom.rect, hallimages)
      map.append(tmphall)
    #create all other nodes
    else:
      # if there are at least 2 spots left on the list, create a room, then a hall
      if len(map) < numofrooms-2:
        tmproom = Room(roomimages, tmphall.direction, tmphall.rect)
        #loop several times to ensure the new room isn't put on top of an old one
        #counter is to ensure we don't end up with an endless loop
        counter = 0
        while checkForExisting(tmproom, map):
          tmproom = Room(roomimages, tmphall.direction, tmphall.rect)
          counter+=1
          #we tried 10 times and couldn't place the room, knock off the last
          #hall and room, and try again
          if counter >10:
            map = map[:-2]
        map.append(tmproom)
        tmphall=Hall(tmproom.direction, tmproom.rect, hallimages)
        map.append(tmphall)
      #only one spot left on our list? create a room
      if len(map) == numofrooms-1:
        tmproom = Room(roomimages, tmphall.direction, tmphall.rect)
        counter=0
        while checkForExisting(tmproom, map):
          tmproom = Room(roomimages, tmphall.direction, tmphall.rect)
          counter+=1
          if counter >10:
            map = map[:-2]
        #map.append(Room(roomimages, tmphall.direction, tmphall.rect))
        map.append(tmproom)
  return map

Monday, June 10, 2013

Map Generation - Ready, Fight!

Okay, I've started work on the map generation code. I had some time to think it over and talk through the logic a bit more, and I made some changes to the way it's going to work. Here's the code I have so far:

import random, pygame

class RoomObj:
    def update(self,anchorpoint):
      self.rect.left = self.xoffset + anchorpoint[0]
      self.rect.top = self.yoffset + anchorpoint[1]

class Room(RoomObj):
    def __init__(self, x,y, imageset):
      #RoomObj.__init__(self)
      self.direction = random.randint(1,4) #up, down, left, right
      self.size = (random.randint(400,600),random.randint(500,700))
      self.image = pygame.Surface(self.size)
      pygame.transform.scale(random.choice(imageset), self.size, self.image)
      self.rect = self.image.get_rect()
      self.rect.topleft = [x,y]
      self.xoffset = x
      self.yoffset = y


class Hall(RoomObj):
    def __init__(self, direction, room_rect):
      #RoomObj.__init__(self)
      if direction==1:
      self.size = (80,random.randint(100,300))
      self.rect.bottom = room_rect.top
      self.rect.left = random.randint(room_rect.left, room_rect.right-80)

def genMap(size,roomimages,hallimages):
    if size=='small':
      numofrooms = random.randint(3,5)
    if size=='medium':
      numofrooms = random.randint(5,8)
      rooms = []
      map = []
      startpoint = [0,0]
      for i in range(numofrooms):
        if i < len(numberofrooms)-1:
          tmproom = Room(startpoint, roomimages)
          #up
          if tmproom.direction == 1:
            map.append(tmproom)
            map.append(Hall(tmproom.direction, tmproom.rect))
    return map

Basically I have a parent class called RoomObj that has the update functions. You may be wondering why a stationary object needs an update function, but for this large of a map, i'm going to have it scroll as the player moves. If you're interested in how I've done the scrolling, check out this article. I'm applying forces to the player object, which move the player sprite around. When the player moves to the edges of an invisible radius around the center of the screen, these forces are transferred to other objects making them scroll. I do this by giving each object an anchor point as an argument. This anchor point is simply a coordinate that I transfer the forces to when the player is moving outside of their center radius. I'm sure I could explain that better, but like I said, if you really care, just check out the blog post. I have the source code up that should explain it better than I could.

Then I have two other classes that inherit from the RoomObj. I created a Room object and a Hall object. I still need to flesh them out a bit more in order to accurately place everything and check for collisions and some other general stuff. I guess I could've created a single class with overloaded init functions, but I've never really been a fan of overloaded classes. They just get kind of confusing after a while. Using inheritance, I can expand the base class or add other children classes fairly easily.

The genMap function is where I made the real changes to how everything is created. If you check out my last blog post, I was going to have it create all the rooms, then loop through the objects to place connecting hall objects. After a great deal of thought, I changed it to create a room, then create a hall, then create a room, and so on. I believe this will allow for much more complex maps. In the future I'd like to add a bit to allow for multiple Hallways to branch off, but for now, I just want to get this working.

Like I said, it's still a work in progress. I know I need to rework the room and hall objects a bit, and I still need to figure out how I want to handle the placement for them. As of right now, I'm thinking I'll use the room object to choose a random direction and then have the hallway get it's x/y coordinates from that. I'm thinking that I'll also need to add wall rectangles to each room object for player collisions later on.

I'm also thinking that the images for the floors will have to be different from those used for the walls. If I'm creating wall objects for each room, those will be scaled to fit the pseudo-random size. I'd like to get some sort of tile for the floors though. I think it'll just look nicer if the floors are all uniform for each map. In any case, I think this should do the trick if I ever finish up the Room and Hall classes. I just don't have time lately to really work on anything. I can dream though...

-Newt

Wednesday, June 5, 2013

Map Gen Pseudo Code

I know there are a ton of map generators out there already, but I was considering trying my hand at it as well. More as a learning experience though, and not as an attempt to improve on them in any way. Lord knows I'm not that good.

But I was thinking about it and I think I may be able to pull it off. I threw together some pseudo code to help me out with the logistics, and I wanted to see if anyone had any improvements or maybe some comments.

Here's what I've got so far:

class RoomObj:
  init(type): #types can be hallways or rooms
    attributes, size, type, image etc.
    randomize image based on type given
  update:
    pass

class Room(RoomObj):
  create room type=Room

class Hall(RoomObj):
  create hall type=Hall

def genMap():
  numofrooms = random number of rooms from range
  listofrooms = []
  listofmapobj = []
  for i in range(numofrooms):
    listofrooms.append(Room())
  for i in range(len(listofrooms)):
    create room objects with a "hallway" type
    if i is not the last iterate:
      listofmapobj.append(Hall(listofrooms[i], listofrooms[i+1]))
    listofmapobj.append(listofrooms[i])
  return listofmapobj

I still need to figure out exactly what parameters the room and hallway classes need to be given, but the rooms will initially just create random sized rectangular rooms, then choose a random image from a pre-loaded set. The room placement would have to check to ensure there are no collisions with other room rectangles. And finally the hallway objects would base their size and shape on the locations of the two rooms given as a parameter.

There are a lot of other particulars I still need to figure out, but I think it is doable. and it should be an interesting project to work on.

I'll have to post whatever I end up with at the end of this little adventure.

-newt

Thursday, May 16, 2013

Pygame Subset for Android - Soft Keyboard

The guys over at pygame.renpy.org have out-done themsleves with their work on the pygame subset for android development. It can be a bit tricky at first if you're not familiar with ant builds or android development, but for the most part they've made it pretty simple to pick up. If I can figure it out, anyone can do it. So first let me just say thanks to all who have worked on the project.

That being said, there are a number of things I wish worked better. Since the pygame mixer wasn't included, the subset allows you to take advantage of the android mixer. Unfortunately the android mixer isn't quite as full-featured in it's implementation here. The soft-touch keyboard is in the same boat. You can import the android subset, and even have the app call the keyboard, but to my knowledge (and this may be completely wrong) you can't actually watch for these key events. I've tried a multitude of methods and searched for hours on how to correctly do this with the pygame subset for android. I've found absolutely nothing that can show me how to correctly implement this.

So what I ended up doing was writing my own soft-keyboard function. I've tested it and it works great with android apps & games.

Here's basically how it works:

def makeKey(x, y, letter):
   global keyArray
   temp = pygame.image.load('key.png')
   if letter == 'enter':
     temp = pygame.image.load('longkey.png')
   limage = pygame.transform.scale2x(temp)
   lrect = limage.get_rect()
   lrect.topleft = [x,y]
   keyArray.append((limage,lrect,letter))
#map out whole keyboard using makeKey()
def makeKeyboard(x,y):
   letters = ['q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m','enter']
   x_move = 0
   y_move = 0
   for i in letters:
     if i == 'a':
       y_move = 40
       x_move = 0
     if i == 'z':
       y_move = 80
       x_move = 0
     makeKey(x+x_move, y+y_move, i)
     x_move+=40
#draw keyboard and write in letters
def drawKeyboard(color,font,surface):
   global keyArray
   for i in keyArray:
     surface.blit(i[0],i[1])
     drawText(i[2],color,font,surface,i[1].center[0],i[1].center[1],True)
#used for drawing text onto the key surface
def drawText(text, color, font, surface, x, y, center):
   textobj = font.render(text, 1, color)
   textrect = textobj.get_rect()
   textrect.topleft = (x, y)
   if center:
     textrect.center = (x, y)
   surface.blit(textobj, textrect)


Then you can do something like this to implement it:

def main():
   #keyboard start coord
   startKey_x = screen.get_size()[0]/2
   startKey_y = screen.get_size()[1]/2

   #create images and rects for keyboard
   makeKeyboard(startKey_x,startKey_y)

   #temp string for testing
   clickedLetters = ""

   clock = pygame.time.Clock()

   while 1:
     clock.tick(60)
     global MOUSEDOWN
     global keyArray

     for event in pygame.event.get():
       if android:
         if android.check_pause():
           android.wait_for_resume()
       if event.type == pygame.QUIT:
         terminate()
       if event.type==pygame.MOUSEBUTTONDOWN:
         MOUSEDOWN=True
         for i in keyArray:
           if i[1].collidepoint(event.pos):
             if i[2] == 'enter':
               pass
             else:
               clickedLetters+=i[2]
       if event.type==pygame.MOUSEBUTTONUP:
         MOUSEDOWN=False
     drawKeyboard(black,font,screen)
     drawText(clickedLetters, black, font, screen, (800/2), (480/4), True)
     pygame.display.flip()


here's an example implementation

So right before the game loop, you create the keyboard. The makeKeyBoard function just needs a starting position and it will create each key and give it it's own rectangle, image and coordinate.

Inside the main loop, under the event listener, you have it watch for mousebuttondown events. When the app is run on android, this equates to a screen press. It uses the event position to determine if there was a collision with a keyboard rectangle. You can map these collisions to any functions you want. You'll notice my keyboard only has letters and the enter key, but you could set up a full keyboard in much the same way.

This likely isn't the best way to implement a soft-keyboard, but it works which is more than I can say for the android soft-touch keyboard using the pygame subset. If anyone else has a better way to do this, or they managed to figure out how to implement the android soft-touch keyboard, I'd love to hear from you. Especially if you figured out the android soft-touch. I'd love to see how you get the events into python.

-Newt

Tuesday, May 14, 2013

Moar Pygame Sprite Movement

There are a number of ways to have python move game objects around the screen. Up until recently I hadn't really tried much other than tying keys to a function that would move a rectangle object by an offset.

Something like this:

*** in the sprite class***
def change_pos(self, x, y):
  self.change_x += x
  self.change_y += y

*** in the main loop ***
for event in pygame.event.get():
  if event.type == KEYDOWN:
    if event.key == K_UP:
      player.change_pos(0, -PS)

I'd set up a global for player speed (PS) that I could hand to the change_pos function as a positive or negative value based on the direction the player wanted to go.

This way works fine, but it's not really all that flexible. What if you wanted to have the player sprite keep moving a bit after the player releases the movement keys? For that you'd need to use a bit of physics. There are a ton of different ways to do something like this, and I've read through some tutorials on it. Since I'm not very good with math though, most of it was way over my head. I came up with a very simplistic approach to the whole thing.

I created a sprite object with an attribute to store a list of forces that will act as inertia to be applied to the object. Then I took out the change_pos() function and replaced it with this:

def apply_force():
  for i in self.forces:
    i -= friction
  if self.up:
    self.forces[0] += speed
  if self.down:
    self.forces[1] +=speed
  if self.left:
    self.forces[2] +=speed
  if self.right:
    self.forces[3] +=speed

In the player sprite's update function, I can say:

def update():
  self.apply_force()
  self.rect.top += (self.forces[1] - self.forces[0])
  self.rect.left += (self.forces[3] - self.forces[2])

You can play around with the values that you use for speed, and friction. Speed is how fast your player object gains speed and friction is how fast the object is slowed. You may want to add something to the end of your apply_force() function to cap the forces. I used something like this:

for i in self.forces:
  if i > 200:
    i=200
  if i < 0:
    i=0

Also, if you want, you can have your main loop use actual time to update instead of FPS which can be affected by the power of the system you're running on. Up until recently I had been using the following in my main loop:

clock = pygame.time.Clock()
while 1:
  clock.tick(60)

It actually makes more sense to use the following:
FPS = 60
clock = pygame.time.Clock()
while 1:
  milliseconds = clock.tick(FPS)
  seconds = milliseconds / 1000.0

Then change the player class update function to take seconds as an argument. This will give you a much more consistent movement:


  self.rect.top += (self.forces[1] - self.forces[0]) * seconds
  self.rect.left += (self.forces[3] - self.forces[2]) * seconds

There are of course, more accurate ways to produce the same effect, but I found this pretty simple to understand and easy to implement.

-Newt


Tuesday, May 7, 2013

ArcSight Export Parser Script

For those of you not familiar with it, ArcSight is a pretty robust correlation engine. I've been working with it for quite a while now, and I've never really been happy with the case management system they have. When an alert is triggered, it pulls in some information and saves it to a Case. Unfortunately the management of these cases are kind of messy and can easily become unwieldy for large environments. If you have HP Openview, you can have ArcSight export the cases directly to your ticketing system. But what if you use another ticketing system or a custom built ticketing system? Well, then you can export the files to the system and write your own API to parse the export files and send to the ticketing system of your choice.

That's what I've been working on the last few weeks. With an ArcSight ESM appliance, I'm limited to using bash, perl (limited libraries), or python 2.4. Since I'm more familiar with python, I went that route. Although, you could easily write a parser in perl. In fact, ArcSight has an approved perl script that parses these exports and formats them to a customized email, so you can route events that way. Still python was what I went with.

It's still a work in progress, but here's the basic functions:

# simple function to consolidate headers from base events
def getHeaders(allevents):
   x = []
   for i in range(len(allevents)):
     for thing in allevents[i]:
       if x.count(thing[0]) == 0:
         x.insert(i,thing[0])
   x.reverse()
   return x


# creates an HTML table for use in our web-based ticketing system
def createHtml(baseEventList):
   if os.path.exists('tmp_alert/'):
     removeDir('tmp_alert/')
   fname="tmp_alert/tmp.html"
   d = os.path.dirname(fname)
   if not os.path.exists(d):
     os.makedirs(d)
   f = open(fname, 'w')
   f.write('<!DOCTYPE html>\n')
   f.write('<html>\n')
   f.write('<body>\n')
   f.write('<p>Base Security Events</p>\n')
   #grab the list of headers
   x = getHeaders(baseEventList)
   f.write('<br>\n')

   #the baseTable class is a css file on the server
   #change if you have your own custom style sheet
   f.write('<table class=baseTable>\n')
   f.write('<tr>\n')
   #grab and write headers
   for i in x:
     f.write('<th> %s </th>\n' % i)
   f.write('</tr>\n')
   #loop over all events
   for event in baseEventList:
     f.write('<tr>\n')
     offset = 0
     #loop again to create matching value fields in our table
     for item in range(len(event)):
       itemwritten = False
       for header in x:
         if itemwritten:
           pass
         else:
           if header == event[item][0]:
             f.write('<td> %s</td>\n' % event[item][1])
             itemwritten = True
       f.write('</tr>\n')
   f.write('</table>')
   f.close()


# function to grab all base events using a list of ID numbers
def grabBaseEvents(xmlcontents, baseEventIDList, arcObj):
   #takes the dom document object for a single xml and baseEvent ID list
   baseEvent = []
   allEvents = []
   #for each sec event
   for node in xmlcontents.getElementsByTagName('SecurityEvent'):
     attr_list = node.attributes
     #iterate over sec event attributs
     for attr in range(attr_list.length):
       if attr_list.item(attr).name == 'id':
         #look at all the event IDs
         for baseevent in baseEventIDList:
         #when we have a match, iterate over the current
         #sec event child nodes and append them to a base event list
         baseEvent = []
         if baseevent == attr_list.item(attr).value:
           for x in node.childNodes:
             if x.hasChildNodes():
               if x.nodeName == 'destinationProcessName':
                 arcObj.setAttr('TPN',x.firstChild.nodeValue)
               baseEvent.append((x.nodeName,x.firstChild.nodeValue))
         #when we're done with the base event, append that to an
         #element in the allEvents list
         if len(baseEvent) != 0:
           allEvents.append(baseEvent)
   return allEvents


# Object to store and retrieve RuleFire Event values
class arcObject:
   def __init__(self):
     self.AlertName = "deviceCustomString6 not filled in"
     self.AA = "None"
     self.AH = "None"
     self.AU = "None"
     self.TA = "None"
     self.TH = "None"
     self.TU = "None"
     self.rulename = "None"
     self.time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
     self.filename = "None"
     self.targetproc = "None"
   def getAttr(self, attr):
     if attr == 'AA':
       return self.AA
     elif attr == 'AN':
       return self.AlertName
     elif attr == 'AH':
       return self.AH
     elif attr == 'AU':
       return self.AU
     elif attr == 'TA':
       return self.TA
     elif attr == 'TH':
       return self.TH
     elif attr == 'TU':
       return self.TU
     elif attr == 'rulename':
       return self.rulename
     elif attr == 'time':
       return self.time
     elif attr == 'FN':
       return self.filename
     elif attr == 'TPN':
       return self.targetproc
     else:
       pass
   def setAttr(self, attr, value):
     if attr == 'AA':
       self.AA = value
     elif attr == 'AN':
       self.AlertName = value
     elif attr == 'AH':
       self.AH = value
     elif attr == 'AU':
       self.AU = value
     elif attr == 'TA':
       self.TA = value
     elif attr == 'TH':
       self.TH = value
     elif attr == 'TU':
       self.TU = value
     elif attr == 'rulename':
       self.rulename = value
     elif attr == 'time':
       self.time = value
     elif attr == 'FN':
       self.filename = value
     elif attr == 'TPN':
       self.targetproc = value
     else:
       pass

Essentially I will parse the xml file for a correlation event, then parse the event for specific fields that I will store in the arcsight object. I'm thinking I'll use these fields later to fill in json files that I can post to the webserver. But for now I just store them and then grab the base event ID's associated with each event.

You may have noticed the deviceCustomString6 message I apply to the default alertName variable. That is something I have ArcSight set before it exports the rule-fire. I use that field to store the rulename + time + attacker address and a number of other useful things. The reason was that our ticketing system only lets us query on that alertName field. This way we can search for any of those fields (i.e. all rules triggered by a particular host).

Then I take those ID's, create a list of base events, using my grabBaseEvents function. Then I run that through my createHtml function which will create a table with the base events. This HTML file will probably be zipped up and posted to the web server as well.

Here's what the main loop looks like so far. The addNSend function will be what ultimately adds the fields to the json file and POSTs it to the server.

dir = glob.glob('*.xml')
for xmlfile in dir:
  contents = parse(xmlfile)
  #print "parsing " +xmlfile
  #for each Security Event
  for i in contents.getElementsByTagName('SecurityEvent'):
  x = i.attributes
  #if external ID attribute exists, it's a rule fire
    if x.item(1).name != 'externalID':
    arcObj = arcObject()
    arcObj.setAttr('rulename', x.item(2).value)
    baseEventIDs = []
    #map rule fire values to alert web fields
    for node in i.childNodes:
      if node.hasChildNodes():
        if node.nodeName == 'fileName':
          if node.firstChild.nodeValue != x.item(1).value:
            arcObj.setAttr('FN', node.firstChild.nodeValue)
          elif node.nodeName == 'deviceCustomString6':
            arcObj.setAttr('AN',node.firstChild.nodeValue)
          elif node.nodeName == 'sourceAddress':
            arcObj.setAttr('AA', node.firstChild.nodeValue)
          elif node.nodeName == 'sourceHostName':
            arcObj.setAttr('AH', node.firstChild.nodeValue)
          elif node.nodeName == 'sourceUserName':
            arcObj.setAttr('AU', node.firstChild.nodeValue)
          elif node.nodeName == 'destinationAddress':
            arcObj.setAttr('TA', node.firstChild.nodeValue)
          elif node.nodeName == 'destinationHostName':
            arcObj.setAttr('TH', node.firstChild.nodeValue)
          elif node.nodeName == 'destinationUserName':
            arcObj.setAttr('TU', node.firstChild.nodeValue)
          elif node.nodeName == 'destinationProcessName':
            arcObj.setAttr('TPN', node.firstChild.nodeValue)
          elif node.nodeName == 'managerReceiptTime':
            arcObj.setAttr('time', node.firstChild.nodeValue.rstrip('.0'))
          #check for baseEventIDs
          elif node.nodeName == 'baseEventIds':
            for ref in node.getElementsByTagName('ref'):
              attr_list_obj = ref.attributes
              for attr in range(attr_list_obj.length):
                if attr_list_obj.item(attr).name == 'id':
                  baseEventIDs.append(attr_list_obj.item(attr).value)
                  #find base events and create html to store
                  #grabBaseEvents should return a list of key/value pairs for

                  #each event
            listOfBaseEvents = grabBaseEvents(contents, baseEventIDs, arcObj)
            #createHtml creates the tmp_alert directory and the html
            createHtml(listOfBaseEvents)
          else:
            pass
      #addNsend(arcObj)
  addFileToFolder(xmlfile,'/var/tmp/processed')
  os.remove(xmlfile)


All in all, it works pretty well. I still need to make some formatting changes and add some logic to parse deeper into the xml for some relevant fields, but that's not difficult. Just time consuming. For now it's fine with the handful of fields that it parses for. I guess I could also have it pretty up the html file a bit. As it is, the headers are still smashed together field names like sourceAddress, and what-not.

Maybe when I have it complete, I can post it to ArcSight 724. No telling how many other people are in need of an API for a non-supported ticketing system. With a little modification, I bet this script would work for quite a few other systems.

-Newt

Monday, April 29, 2013

Ludum Dare - Post-mortum

Well, that was quite an experience. Ludum Dare 26 has come and gone and I have very little to show for it. Due to a lack of planning on my part and some issues with conflicting responsibilities eating up my time, I didn't end up with anything to submit at the end of this game jam. Not to say I didn't get anything from the competition though.  I did get my feet wet, so to speak, and in doing so I think I came away with a number of ways I can improve upon my time management next round.

For instance, I discovered that I was spending a good amount of time coding things I could have easily gotten from existing libraries. The problem was, that I wasn't familiar with these libraries ahead of time, so I instead spent too much time coding it from scratch. Something as simple as a particle effect library or physics library could have helped me out immensely. I intend to spend the next few months getting myself acquainted with these libraries and finding out what I can do to avoid spending precious time trying to rewrite these things.

Another lesson learned was that next time I shouldn't try to do this from home. Family life, and other responsibilities can easily demand attention that will prevent a game from being finished. I ended up doing the majority of my coding in the morning (when the family was still sleeping) and in the evening (after everyone had retired). The whole 10-12 hours in between were just a cluster f*ck where I rarely accomplished anything of value.

I did manage to get a few interesting classes and functions out of this whole experience as well. The theme for the game was that it should be minimalistic in some way. I wanted to apply this to the artistic direction, so many of the objects drawn to the screen were basic shapes. I made them hallow, so that I could easily fill in the objects with whatever color I wanted. It was my intention to create several arrays with different color schemes that could be interchanged for each level. I had that working the first morning.

Next thing I wanted was some sort of map generation. I had a friend willing to work on this for me. He had already worked on something similar for one of his previous classes, and he figured he could easily rewrite it for me.

Another thing I was working on, (didnt get finished with it, but I have some promising code so far) was player movement that moved the screen around with it. I'd never done anything like that with a previous game so it was something new. I really should have attempted something like it prior to this game jam, since I spent a lot of time on this. I was also trying to incorporate a parallex effect using layered images moving at different speeds to give the game some depth. This was also somewhat new to me as I had read about it but not actually attempted it myself. Like I said, I have some promising code, but nothing completely working.

I also managed to get a pretty neat looking background for the game. I created a class that you can pass a surface (to draw to), a color scheme object, and the number of objects to be created: and the class would create a background object with small moving particles. It was a pretty neat effect, and as soon as I have some time, I'll post the code along with a little video clip or gif.

So I ended up with some pretty neat stuff and a neat idea for a game, but unfortunately it just didn't get to the "playable" phase. I might continue working on it, but I'm not sure when I'll have the time. At least I came away with a few lessons and even some neat code that I can implement elsewhere. All in all, it was a good time and I can't wait for the next Ludum Dare to come around.

Cheers,

-Newt

Friday, April 26, 2013

Ludum Dare - First Attempt

Okay, so I decided to take a stab at Ludum Dare 26. For those of you not familiar, it's a game jam. You are given a theme, and tasked with creating a game in a single weekend. Since this is my first go at the game jam, I'm going the more relaxed, less competetive route.

I'm still new at game development in general and I figured this would help me with my time management as well as just general development practice.

Anywho, it's been a couple hours since we were notified of the theme: Minimalistic. I still haven't yet nailed down a good idea for the game, but I've started working on the framework (basic functions, menu screens, etc.).

I've been throwing around some ideas for the game concept with a friend and so far we haven't come up with anything solid. Hopefully we'll have something by tomorrow morning. By then I should have most of the framework nailed down. Once I have a better idea of the type of game I can start to put together some sort of timeline and break the project down into tasks that need to be completed.

I'm thinking I might find some good music to use over at http://dig.ccmixter.org/

Some of the creative commons licensed music I've found there in the past has been really good. I'm sure I'll find something suitable.

Well, no time to waste... back to brainstorming

cheers,
-Newt

Wednesday, April 10, 2013

Game Flair

I know I'm deviating a bit from that last couple posts about designing a simple game with pygame libraries. But I stumbled upon a video that got me thinking about some of the little things you can add to a game to make them more interesting. If you're interested this is the video I'm referring to.

One really simple idea that they had was to use tweening or easing. Instead of just placing a new sprite or game object on the screen, we have it drop into place or instead of game objects just vanishing, you can use this to slowly scale them out of existence.

The way they did this was using something like:

x += (target-x) *.1

This is probably the most simple approach to tweening. Basically we give the sprite or rectangle a target and a starting point. Then we move the x by 10 percent until it gets to the destination or target. Simple right?

And it provides a really nice effect to the game for very little cost.

That's the easiest implementation of tweening/easing. If you want to get fancier you have to do a bit more math.

Here's a basic function to get you started

def linearTween(time, startPoint, change, endPoint):
  return change * time / endPoint + startPoint

time is the current time in frames, steps, whatever counter you want to use. As long as it is a percentage of the endPoint

startPoint is the value where we begin

change is the difference between the beginning and destination values

endPoint is the end time or frame or whatever for your tween.

Basically it's the same thing as x += (target-x) *.1 since you have a start, a target, diff, and percentage of change. However in this way we have a number of variables we can use in a bunch of different ways.

Check out this site: http://www.timotheegroleau.com/

He has a generator for easing functions that you can use for all sorts of fun stuff. In fact, Tim Groleau has a collection of material on the subject if you're interested.

-Newt

Wednesday, March 27, 2013

Simple Game - Python

So last week I was telling you all about Pygame. Well, it was more about a certain book which covers python game development using pygame. Anyway, I thought I might share a bit of code that I've used in designing my latest python game.

The idea for my game was just a simple hack-n-slash zombie killer. Nothing flashy, just a project to help me keep my python skills from going to utter crap. The game isn't quite finished, but I've learned a few things along the way. And if there's any interest, I can post the source for what I have so far. Just let me know.

Anywho, so where do you start with a python game? Well, if you're using pygame libraries, you'll want to import the libraries, initialize pygame and create a surface to begin drawing.

import pygame

pygame.init()

#create the display object named screen
screen = pygame.display.set_mode((800, 600))

Use whatever size screen you want. And if you feel fancy, you could use the os or pygame libraries to pull the display resolution and use that. To do that with python, just use something like:

screen_x = pygame.display.Info().current_w
screen_y = pygame.display.Info().current_h
screen = pygame.display.set_mode((screen_x,screen_y))

Of course, that's a bit wonky since it's still windowed, and the explorer bar one windows, (or the menu bar on linux distros) will be in the way. To get around this you have to set the display to fullscreen. Here's an example:

#give me the biggest 16bit display available
>>> modes = pygame.display.list_modes(16)
>>> if not modes:
...     print '16bit not supported'
... else:
...     print 'Found Resolution:', modes[0]
...     pygame.display.set_mode(modes[0], FULLSCREEN, 16)


I pulled this example from one of the pygame tutorials located here: http://www.pygame.org/docs/tut/DisplayModes.html

Anyway, once you have this much, you can start drawing to the screen. You can draw images (loaded from a file) or just draw some basic shapes using pygame. Let's start by drawing a background. I'm just going to fill it black.

background = pygame.Surface(screen.get_size())
background.fill((0,0,0))

By the way, if you're going to be using certain colors throughout your code, it makes sense to create some variables to store the rgb tuples. Here's a few to get you started:

black = (0,0,0)
white = (255,255,255)
blue = (0,0,255)
red = (255,0,0)
green = (0,255,0)
#then if you were to use them to fill the background it would look like this
background.fill(black)

So now we've got a black background, so what? Most boring game ever. Let's add a rectangle or something.

pygame.draw.rect(background, green, (100, 100, 100, 100))

You can probably figure that out, but I'm using the pygame draw class to draw a rectangle. This function takes several arguments. First it needs a surface to draw the rectangle to, I used background. Then it takes a tuple for the color. And lastly it needs to know where the rectangle is to be drawn, and the dimensions. These are passed as a tuple of four elements, where the first element is the x coordinate and the second element is the y coordinate. The other two are for width and height. 

This is drawing the rectangle (square actually) object to the background surface. Remember though, that the background was drawn to the screen surface object. So how do we display the changes we just made to the background surface? With the blit function. Each surface object has a number of useful functions that come along with it. Blit basically, redraws one surface to another, updating any changed pixels. Here's a quick example:

screen.blit(background, (0,0))

I'm updating the screen surface with the new background image at the coordinates 0, 0. Since the background is the same size, this will place it directly on top of the screen.

If you run this, it will open your game, draw the rectangle and immediately close it. In order to keep the window open, you have to have the drawing done in some sort of loop, or tell python to wait for input or something to that effect. I may go over the different ways to do that later on, but for now let's just create a little game loop to watch for "events" and quit when it sees a specific event.

while True:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit()

You can probably guess as to what this does for the most part. It's looking through pygame events, which are all sorts of keyboard, and mouse input events. pygame.QUIT is basically when the user clicks the window's X to close the window. Without this you'll have to manually kill the process.

So here's what we have so far:
import pygame

#set up color variables
black = (0,0,0)
white = (255,255,255)
blue = (0,0,255)
red = (255,0,0)
green = (0,255,0)


#start pygame 
pygame.init()

#set up a basic surface
screen = pygame.display.set_mode((800, 600))




#create a background using the screen surface size
background = pygame.Surface(screen.get_size())
background.fill(black)
 

#draw a rectangle to the background surface
pygame.draw.rect(background,green,(100,100,100,100))
 

#draw the whole thing to the screen
screen.blit(background, (0,0))
 

#update your display
pygame.display.update()



#loop through events till we get the signal to close the window

while 1:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit()


And there we go. We have a basic window, with a couple surfaces, and a rectangle object that we have drawn onto them. Now you could do this with a single surface object. I just got into the habit of having two recently. The reason for this is kind of convoluted, and it involves a solution for resizing the window to fit multiple android devices. I'll have to do a post on that down the road, but this post is getting pretty lengthy, so I'll just leave off here for now.

Next time, I'll go into a bit about moving your rectangles and animating using images rather than just using a single static green rectangle. 

-Newt

Wednesday, March 20, 2013

Games with Python

Over the last year and some change, I've been trying to teach myself to code. I started with Python for no real reason other than there were a number of tutorial sites out there that seemed to be pretty complete.

After going through the tutorials and Python documentation (several times in some cases) I found myself wondering how to get in some badly needed practice. After all, in order to code something you need to have some sort of use case in mind. I had found a number of forums offering up ideas for projects, but most were either way above my head or so simple I completed them in minutes.

Longer projects seemed to only hold my interest if there were some actual need for the functionality. Like parsing proxy logs or cmdb data at work. For everything else, I had trouble keeping focused till the completion of the code.

One day, by chance, I found this: http://inventwithpython.com/

At the time Albert Sweigart had only written the first book, but it was enough to renew my interest in learning the language. It's written so that even those with no working knowledge of programming can start creating games and learning python. It goes through a few command-line based games, into graphics based games teaching skills that can easily be translated into scripting and app development.

If you are interested in simple game development, or Python, I would definitely recommend reading his book either by download (for free) or purchasing a hardcopy . The second book is also now available and goes into even more detail with several new game types and development concepts.

I know this sounds like some sort of plug, but I am in no way being compensated for this endorsement. I am merely a grateful reader of his work.

-newt

Wednesday, March 13, 2013

Coding Challenge - Python

So I've been a fan of hackthissite.org for a while now. IT Security has been an interest of mine since I was introduced to computers. They've got tons of hacking challenges to test your skills and teach you new ones.

Just recently though, I noticed that they have coding/scripting challenges now as well. They range from beginner to really difficult, but they've got some really neat ones up there. If you're interested, go register (for free, of course) and try them out for yourself.

The first challenge asks you to code something that can take a number of scrambled words, that were chosen from a file that they provide you with, run the input through a script and produce the unscrambled words in a specific format. i.e. (word1, word2, word3,.....)

Sure you could do it manually, but the page expires a few seconds after the scrambled words are given. So you have to get the scrambled words, run them through your script and submit the output in a few seconds.

There are a ton of ways you could handle this, like sending the output to a file, or console, or even have it go straight to the clipboard so you can paste it into the web form quickly. Even write something to parse the page, pull down the words, and post the output automatically...

Below the break, you can see how I did it using python. Kind of a spoiler if you're planning to try this stuff out yourself, just fyi.

Wednesday, March 6, 2013

Assignment - Python

So, one of my java classes had this assignment a while back to write a command-line application to store user input to a text file. Easy, right? Just loop over the 3 questions and save the user responses on a line in a text file.

Having never worked with Java before, I was surprised at the amount of code necessary to do this. Even without validating user input, the code ended up being around 40 lines.

It got me thinking about how I would do the same thing in python. And I was curious to see how few lines it would take to get the exact same functionality. Here's what I came up with:

file = open("output.txt", 'w')
listOfStuff = []
while True:
    productID = raw_input("enter productID: ")
    product = raw_input("enter product: ")
    price = raw_input("enter price: ")
    listOfStuff.append(productID+" "+product+" "+price+ "\n")
    blah = raw_input("Coninue? yes or no: ")
    if blah in ("no", "nope", "negative"):
        break
for i in listOfStuff:
    file.write(i)
file.close()

13 lines does it. Sure I should do some input validation and all that best-practice jazz, but I didn't have to implement any of that for the java project either, and I was just trying to get the same output and functionality.

If anyone wants to see the crappy java version of this that I did, I guess I could post that too, just leave a comment, or shoot me a message. I only leave it out because 1) I'm lazy, and 2) I have my doubts about anyone actually reading this blog.

-newt

Wednesday, February 27, 2013

99 bottles of beer - Python

This one is pretty useless, I know. I was trying to think up something to code to test out my fledgling Python knowledge back when I was first picking it up. There was a competition online to script something to output the lyrics to "99 bottles of beer on the wall" in the fewest lines possible. There was a category for fewest characters as well. I knew I wasn't good enough to enter such a competition, but I figured I'd try it and see what I could come up with.

Here's what I ended up doing:

def outputLine(x,v):
  z = str(x) + " bottle" + v + " of beer on the wall, " + str(x) + " bottle" + v + " of beer. take one down pass it around, " + str(x-1) + " bottles of beer on the wall."
  print z
def song():
  boosh = range(1, 100)
  boosh.reverse()
  for x in boosh:     
    if x == 1:
      v = ""
      outputLine(x,v)
    else:
      v = "s"
      outputLine(x,v)
      x = x - 1   
song()

works like a champ
I know now, several shortcuts I could use to shorten it up a bit more, but it's interesting for me to see how I solved a problem a year ago and think about how I would do it now.

-newt

Monday, December 17, 2012

Konami code - Python

Okay, so I was playing around with my game trying to figure out how to implement cheats. There's probably a more elegant way to do this, but I'm still pretty new to coding, so give me a break.

Basically I created a pause menu function. All my menu's work by implementing a menu with basic functions and attributes. You just feed the menu function some images to use for the screen, and set up a loop to watch for keystrokes or other input events.

Anyway, for the pause menu, I just have a simple background with two options available to choose from. It watches for input from the up, down, left, right, b, a,  keys. (went with the konami code for testing)  I set up two variables. code and match. code is an array that holds key strokes while match is used to determine if the keys were pressed in the correct order.

def pause(screen, font):
  images = ['menu.png', 'menu1.png']
  menu = Menu(images)
  menu.imgset = [pygame.image.load(images[0]), pygame.image.load(images[1])]
  pygame.sprite.RenderPlain(menu)
  clock = pygame.time.Clock()
  code = []
  match = ['u','u','d','d','l','r','l','r','b','a']
  while True:
    clock.tick(30)
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        terminate()

      if event.type == KEYDOWN:
        if event.type == pygame.QUIT:
          terminate()
        if event.key == K_UP:
          menu.changeup()
          code.append('u')
        if event.key == K_DOWN:
          menu.changedown()
          code.append('d')
        if event.key == K_LEFT:
          code.append('l')
        if event.key == K_RIGHT:
          code.append('r')
        if event.key == K_b:
          code.append('b')
        if event.key == K_a:
          code.append('a')
        if event.key == K_RETURN:
          if menu.pos == 0:
            #shared.PAUSE = False
            return False
          if menu.pos == 1:
            pygame.quit()
            sys.exit()
   
    code = code[-10:]
    if code == match:
      shared.CHEAT = True
      code = []
    menu.update()      
    pygame.draw.rect(screen,(0, 0 ,0),(0,0,800,600))
    screen.blit(menu.image,menu.rect)
    #drawText('Paused', font, screen, 350, 30)
    textobj = font.render("Paused", 1, (255,255,255))
    textrect = textobj.get_rect()
    textrect.topleft = (350, 30)
    screen.blit(textobj, textrect)
    pygame.display.update()


Here's the bit in the main loop that uses the pause and checks for the cheat variable before changing the background image:

def main():
  while 1:
    #set up variables, initialize sprites, etc, etc...
    .......
   
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        terminate()

      if event.type == KEYDOWN:
        #throw in a pause key
        if event.key == K_p:
          pause(screen, font)

    #check for cheat and blit images to screen surface
    if shared.CHEAT:
      screen.blit(backimage2,backrect)
    if not shared.CHEAT:
      screen.blit(backimage,backrect)
    screen.blit(attack.image,attack.rect)
    screen.blit(player.image,player.rect)
      
  pygame.display.update()

basically pressing p brings up the pause screen, and while in the pause menu, if the two variables match, it sets the CHEAT variable to true.

You could instead, have it add more "cheat" options to the menu, so you could turn them on and off. The way I did it, leaves the cheat on for the duration of the game. Like I said, there are probably better ways to do it, but this works. I'll probably post the whole game source at some point, but if you'd like to see it now, just let me know and I'll shoot you the source.

-newt

Calculate Car Loan

When run on my linux system

Here's a simple script for calculating a car loan using java:
package calculateloan;
public class displayFrame extends javax.swing.JFrame {
    //set up variables
    public float interest;
    public float price;
    public float dpayment;
    public int num_months;
    public float results;
    public float results1;
    public double cubed;
    /**
     * Creates new form displayFrame
     */
    public displayFrame() {
        initComponents();
    }
   
     ..........
                                 
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
       
        // set default credit rating
        interest = 0.15f;
        //find out credit rating
        if (jRadioButton1.isSelected()){
            interest = 0.05f;
        }
        if (jRadioButton2.isSelected()){
            interest = 0.10f;
        }
        if (jRadioButton3.isSelected()){
            interest = 0.15f;
        }
        //find number of months
        if (jComboBox1.getSelectedItem() == "5 years"){
            num_months = 60;
        }
        if (jComboBox1.getSelectedItem() == "10 years"){
            num_months = 120;
        }
        if (jTextField1.getText().isEmpty()){
            //set default value for price
            price = 15000.0f;
        }
        if (jTextField1.getText().length() != 0){
            //set price from fields if not null
            price = Integer.parseInt(jTextField1.getText());
        }
        if (jTextField2.getText().isEmpty()){
            //set default down payment
            dpayment = 0.0f;
        }
        if (jTextField2.getText().length() != 0){
            //set down payment if field not null
            dpayment = Integer.parseInt(jTextField2.getText());
        }
       
        //Print lines to test variable assignment
        //System.out.println(price);
        //System.out.println(dpayment);
        //System.out.println(num_months);
        //System.out.println(interest);
       
        //at this point fields are set. we can calculate payment
        //the calculation is (payment(rate/12))/(1-(1+rate/12)^-num_months)
        //i broke it down into several steps
        results = interest/12;
        results = price * results;
        results1 = interest/12;
        results1 += 1;
        num_months = -num_months;
        cubed = java.lang.Math.pow(results1, num_months);
        cubed = 1-cubed;
        cubed = results/cubed;
        //cubed is now a double with way too many decimal places
        //let's do a bit of math to round it to the nearest tenth
        //thanks to Obvius for this bit
        int tempInteger;
        cubed *= 100;
        cubed += (double).5;
        tempInteger = (int)cubed;
        cubed = (double)tempInteger;
        cubed /= 100;
        jLabel7.setText("Results: " + cubed);
       
       
    }                                       
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        float[] anArray;
        anArray = new float[3];
        anArray[0] = 0.05f;
        anArray[1] = 0.10f;
        anArray[2] = 0.15f;
       
        .........
       
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new displayFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                    
    .......           
}
Where ever you see "......." I've omitted some of the garbage netbeans adds. Obviously this is a snippet from a jframe project and the important bits are in tact. I set up a number of fields, dropdowns, etc, and a button that takes all of it to calculate the loan.
You should be able to do the same thing fairly easy using the design function within netbeans or a similar IDE.
-Newt

Monday, December 10, 2012

Hopefully Helpful

So I was coding a simple python game the other day using pygame, and I thought this might be helpful for someone:

class Test(pygame.sprite.Sprite):
  # Constructor function
  def __init__(self,x,y):
    # Call the parent's constructor
    pygame.sprite.Sprite.__init__(self)

    # Set init speed
    self.change_x=0
    self.change_y=0
    self.facing = 0 
    # Set height, width
    self.image = pygame.image.load('baddie.png')
    self.down = [pygame.image.load('baddie.png').convert_alpha(),
                 pygame.image.load('baddie1.png').convert_alpha(),
                 pygame.image.load('baddie2.png').convert_alpha(),
                 pygame.image.load('baddie3.png').convert_alpha(),
                 pygame.image.load('baddie2.png').convert_alpha(),
                 pygame.image.load('baddie1.png').convert_alpha()]
    self.left = [pygame.image.load('baddiel.png').convert_alpha(),
                 pygame.image.load('baddiel1.png').convert_alpha(),
                 pygame.image.load('baddiel2.png').convert_alpha(),
                 pygame.image.load('baddiel3.png').convert_alpha(),
                 pygame.image.load('baddiel2.png').convert_alpha(),
                 pygame.image.load('baddiel1.png').convert_alpha()]
    self.right = [pygame.image.load('baddier.png').convert_alpha(),
                  pygame.image.load('baddier1.png').convert_alpha(),
                  pygame.image.load('baddier2.png').convert_alpha(),
                  pygame.image.load('baddier3.png').convert_alpha(),
                  pygame.image.load('baddier2.png').convert_alpha(),
                  pygame.image.load('baddier1.png').convert_alpha()]
    self.up = [pygame.image.load('baddieu.png').convert_alpha(),
               pygame.image.load('baddieu1.png').convert_alpha(),
               pygame.image.load('baddieu2.png').convert_alpha(),
               pygame.image.load('baddieu3.png').convert_alpha(),
               pygame.image.load('baddieu2.png').convert_alpha(),
               pygame.image.load('baddieu1.png').convert_alpha()]

    self.imgcounter = 0
    self.imgvar = 0
    # Make our top-left corner a required input for the class
    self.rect = self.image.get_rect()
    self.rect.topleft = [x,y]
  
# Change the speed of the player
  def changespeed(self,x,y):

    self.change_x+=x
    self.change_y+=y
     
  # Find a new position for the player
  def update(self):

    # Get the old position, in case we need to go back to it
    old_x=self.rect.topleft[0]
    old_y=self.rect.topleft[1]
     
    # Update position according to our speed (vector)
    new_x=old_x+self.change_x
    new_y=old_y+self.change_y
     
    # Put the player in the new spot
    self.rect.topleft = (new_x,new_y)
    if self.change_x == 0 and self.change_y == 0:
      #self.imgvar = 0
      if self.facing == 3:
        self.image = self.left[0]
      if self.facing == 2:
        self.image = self.down[0]
      if self.facing == 4:
        self.image = self.right[0]
      if self.facing == 1:
        self.image = self.up[0]
       
    if self.change_x < 0:
      self.facing = 3
      if self.imgcounter == 0:
        self.image = self.left[0]
        #self.imgcounter =+ 1
      self.imgcounter += 1
      self.image = self.left[self.imgvar]
      if self.imgcounter > 1:
        self.imgcounter = 0
        self.imgvar += 1
        if self.imgvar > len(self.left)-1:
          self.imgvar = 0
         
        #self.image = self.left[self.imgvar]
     
    if self.change_x > 0:
      self.facing = 4
      if self.imgcounter == 0:
        self.image = self.right[0]
        #self.imgcounter =+ 1
      self.imgcounter += 1
      self.image = self.right[self.imgvar]
      if self.imgcounter > 1:
        self.imgcounter = 0
        self.imgvar += 1
        if self.imgvar > len(self.right)-1:
          self.imgvar = 0
    if self.change_y < 0:
     
      self.facing = 1
      if self.imgcounter == 0:
        self.image = self.up[0]
        #self.imgcounter =+ 1
      self.imgcounter += 1
      self.image = self.up[self.imgvar]
      if self.imgcounter > 1:
        self.imgcounter = 0
        self.imgvar += 1
        if self.imgvar > len(self.up)-1:
          self.imgvar = 0
    if self.change_y > 0:
      self.facing = 2
      if self.imgcounter == 0:
        self.image = self.down[0]
        #self.imgcounter =+ 1
      self.imgcounter += 1
      self.image = self.down[self.imgvar]
      if self.imgcounter > 1:
        self.imgcounter = 0
        self.imgvar += 1
        if self.imgvar > len(self.down)-1:
          self.imgvar = 0

It's a simple sprite created to help test some animations I created. Keeps track of directions and the update function flips through an array of images. You can easily adjust this to pull in images from a sprite sheet instead of using several image files.

It doesn't really do much else, but it's useful for testing out sprite animations.

-Newt