Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame as pygame
- import random as random
- import os.path
- import glob
- #constants
- WIDTH = 800
- HEIGHT = 600
- UP = (0, -5)
- DOWN = (0, 5)
- LEFT = (-5, 0)
- RIGHT = (5, 0)
- STOP = (0,0)
- #colors
- BLACK = (0,0,0)
- WHITE = (255,255,255)
- RED = (255,0,0)
- GREEN = (0,255,0)
- BLUE = (0,0,255)
- #rotations
- leftTurns= {
- UP:LEFT,
- LEFT:DOWN,
- DOWN:RIGHT,
- RIGHT:UP}
- rightTurns = {
- UP:RIGHT,
- RIGHT:DOWN,
- DOWN:LEFT,
- LEFT:UP}
- acrossTurns = {UP:DOWN,
- DOWN:UP,
- LEFT:RIGHT,
- RIGHT:LEFT}
- #lists
- men = []
- bullets = []
- #set game icon
- def seticon(iconname):
- """
- give an iconname, a bitmap sized 32x32 pixels, black (0,0,0) will be alpha channel
- the windowicon will be set to the bitmap, but the black pixels will be full alpha channel
- can only be called once after pygame.init() and before somewindow = pygame.display.set_mode()
- from:http://www.pygame.org/docs/ref/display.html#pygame.display.set_icon comments
- """
- #create a surface for icon
- icon=pygame.Surface((256,256))
- icon.set_colorkey((255,255,255))#and call that color transparent
- rawicon= pygame.image.load(iconname)#must be 32x32, black is transparent
- for i in range(0,256):
- for j in range(0,256):
- icon.set_at((i,j), rawicon.get_at((i,j)))
- pygame.display.set_icon(icon)#set wind
- #handle keys
- def key_event(event):
- ##WORKING ON THIS: TRY TO MAKE THIS A LIST INSTEAD
- ## OF ALL THE DIRECTIONS ONE AFTER ANOTHER
- if event.type == pygame.KEYUP:
- #tuple below is directions
- if event.key in (pygame.K_DOWN,pygame.K_UP,pygame.K_LEFT,pygame.K_RIGHT):
- #for brick in bricks:
- #brick.setMove(moving=False, dir =brick.dir)
- circle.stopGo(False)
- elif event.type == pygame.KEYDOWN:
- if event.key in (pygame.K_DOWN,pygame.K_UP,pygame.K_LEFT,pygame.K_RIGHT,\
- pygame.K_w,pygame.K_a,pygame.K_s,pygame.K_d):
- keyDirs = {
- pygame.K_DOWN : DOWN,
- pygame.K_UP : UP,
- pygame.K_LEFT: LEFT,
- pygame.K_RIGHT: RIGHT,
- pygame.K_s : DOWN,
- pygame.K_w : UP,
- pygame.K_a: LEFT,
- pygame.K_d: RIGHT,
- }
- direction= keyDirs[event.key]
- circle.changeDir(direction)
- circle.stopGo(True)
- elif event.key == pygame.K_c:
- circle.shooter.fire(circle.direction,'bullet')
- pass
- #clear screen, slopilly.
- elif event.key == pygame.K_RETURN:
- screen.fill((0,0,0))
- #change animation
- elif event.key in (pygame.K_0,pygame.K_1,pygame.K_2,
- pygame.K_3,pygame.K_4,pygame.K_5,
- pygame.K_6,pygame.K_7,pygame.K_8,
- pygame.K_9):
- keys = {pygame.K_0 :9,
- pygame.K_1 :0,
- pygame.K_2 :1,
- pygame.K_3 :2,
- pygame.K_4 :3,
- pygame.K_5 :4,
- pygame.K_6 :5,
- pygame.K_7 :6,
- pygame.K_8 :7,
- pygame.K_9 :8}
- circle.curFrameNum = keys[event.key]
- circle.curFrame = circle.animList[circle.curFrameNum]
- print 'changed frame'
- #scale images to
- class anything:
- def __init__(self, (x,y),name, animFolder,
- direction=RIGHT, moving=False,
- shooter= None):
- self.x, self.y = (x,y)
- self.name = name
- self.animList = self.animLister(animFolder)
- self.curFrameNum = 0
- self.direction = direction
- self.facing = self.direction
- self.curFrame = self.animList[self.curFrameNum]
- self.moving = moving
- self.shooter = shooter
- if self.shooter: self.shooter.owner = self
- def scaleImage(self,image):
- #loads image to raw
- raw = pygame.image.load(image).convert_alpha()
- #raw.set_colorkey(WHITE)
- #h and w of the raw image
- H = raw.get_height()
- W = raw.get_width()
- #filled white surface with black color key
- ref = pygame.Surface((H,W)) #ref = Refined
- #ref.set_colorkey(WHITE)
- #ref.fill((255,255,255))
- #for every pixel in raw, put in ref
- #for i in range(0,W):
- #for j in range(0,H):
- #ref.set_at((i,j), raw.get_at((i,j)))
- ref.blit(raw,(0,0))
- print 'colorkey', ref.get_colorkey()
- #SET SCALE HERE
- scaled = pygame.transform.scale(ref, (25,25))
- imagename = image.split('/')[-1]
- print '{0}\'s scaled width: {1}'.format(imagename,scaled.get_width())
- return scaled
- def animLister(self,folder):
- '''must have jpgs named 1 thru 100
- and this func will go through them in that
- order and add them to a list to animate'''
- #func to list all jpgs in folder
- #then count em all for len later
- animList = []
- for file in glob.glob(folder+'/*.jpg'):
- print file
- frame = self.scaleImage(file)
- animList.append(frame)
- return animList
- def drawFrame(self):
- #get height and width
- H=self.curFrame.get_height()
- W=self.curFrame.get_width()
- #create surface to blit image to
- #then blit that to screen
- toBlit = pygame.Surface((W,H))
- toBlit.blit(self.curFrame,(0,0))
- screen.blit(toBlit, (self.x,self.y))
- #print self.name, ' is drawing'
- def changeDir(self,direction, moving = True):
- #- check pos
- #- rotate part of the way
- #- go
- #self.direction[0] = direction[0]
- #self.direction[1] = direction[1]
- self.direction = direction
- self.move()
- #self.drawFrame()
- def stopGo(self,move):
- self.moving = move
- def move(self):
- #print 'MOVE ', self.facing, self.direction
- if self.moving:
- if self.facing != self.direction:
- self.rotate()
- self.x += self.direction[0]
- self.y += self.direction[1]
- def rotate(self):
- f = self.facing
- d = self.direction
- print 'before', f,d
- #frame = self.animList[self.curFrame]
- #frame2 = frame.copy()
- frame = self.curFrame
- if (f,d) in acrossTurns.items():
- self.curFrame= pygame.transform.rotate(frame,180)
- print 'rotate across!'
- self.facing = self.direction
- elif (f,d) in leftTurns.items():
- print 'rotate left!'
- self.facing = self.direction
- self.curFrame= pygame.transform.rotate(frame,90)
- elif (f,d) in rightTurns.items():
- print 'rotate right!'
- self.facing = self.direction
- self.curFrame= pygame.transform.rotate(frame,-90)
- print 'after', f,d
- class shooter:
- def __init__(self, level, maxHp):
- self.level = level
- self.maxHp = maxHp
- self.curHp = maxHp
- def fire(self, direction, variety):
- #spawn and fire projectile in direction
- bull= projectile(self.owner,self.owner.facing, bob, 'bullet')
- bullets.append(bull)
- pass
- class projectile():
- def __init__(self, source, direction, target, variety):
- self.source = source
- self.direction = direction
- self.target = target
- self.variety = variety
- self.fire()
- def fire(self):
- #get rect of curFrame's center x y
- self.x,self.y = self.source.curFrame.get_rect().center
- #spawn at first position out from source
- # add the center of the rect to the center of
- # shooter then move bit a bit
- self.x = self.x + self.source.x + self.direction[0]
- self.y = self.y + self.source.y + self.direction[1]
- self.draw()
- print '*'*8
- print self.source.curFrame.get_rect().center
- print '*'*8
- def move(self):
- self.x += self.direction[0]
- self.y += self.direction[1]
- def draw(self):
- height= 5
- pygame.draw.rect(screen,(100, 100, 100),
- (self.x, self.y, height, height))
- def hit(self):
- bullets.remove(self)
- # ########INIT######### #
- pygame.init()
- pygame.display.set_caption('Line Moving App')
- seticon('bricks.ico')
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- screen.fill(WHITE)
- #background = pygame.Surface(screen.get_size())
- #background = background.convert()
- #background.fill(WHITE)
- clock = pygame.time.Clock()
- pygame.display.flip()
- basicFont = pygame.font.SysFont(None, 48)
- # ############### #
- pathname = os.path.abspath(os.path.curdir)
- pathname += '/art/man/'
- print pathname
- shot = shooter(1,100)
- circle = anything((400,200),'Josh', pathname,shooter=shot)
- men.append(circle)
- bob = anything((200,200),'Bob',pathname)
- men.append(bob)
- running = 1
- while running:
- screen.fill(WHITE)
- #check for movements, and draw after
- for object in men:
- object.move()
- #print object.facing, object.direction
- object.drawFrame()
- for object in bullets:
- object.move()
- object.draw()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = 0
- elif event.type == pygame.KEYDOWN:
- key_event(event)
- elif event.type == pygame.KEYUP:
- key_event(event)
- pygame.display.flip()
- clock.tick(30)
Advertisement
Add Comment
Please, Sign In to add comment