TankorSmash

Squares

Oct 2nd, 2011
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.32 KB | None | 0 0
  1. import pygame as pygame
  2. import random as random
  3. import os.path
  4. import glob
  5.  
  6. #constants
  7.  
  8. WIDTH = 800
  9. HEIGHT = 600
  10.  
  11. UP = (0, -5)
  12. DOWN = (0, 5)
  13. LEFT = (-5, 0)
  14. RIGHT = (5, 0)
  15. STOP = (0,0)
  16.  
  17. #colors
  18. BLACK = (0,0,0)
  19. WHITE = (255,255,255)
  20.  
  21. RED = (255,0,0)
  22. GREEN = (0,255,0)
  23. BLUE = (0,0,255)
  24.  
  25. #rotations
  26. leftTurns= {
  27. UP:LEFT,
  28. LEFT:DOWN,
  29. DOWN:RIGHT,
  30. RIGHT:UP}
  31.  
  32.  
  33. rightTurns = {
  34. UP:RIGHT,
  35. RIGHT:DOWN,
  36. DOWN:LEFT,
  37. LEFT:UP}
  38.  
  39.  
  40. acrossTurns = {UP:DOWN,
  41. DOWN:UP,
  42. LEFT:RIGHT,
  43. RIGHT:LEFT}
  44.  
  45. #lists
  46. men = []
  47.  
  48. bullets = []
  49.  
  50. #set game icon
  51. def seticon(iconname):
  52. """
  53. give an iconname, a bitmap sized 32x32 pixels, black (0,0,0) will be alpha channel
  54.  
  55. the windowicon will be set to the bitmap, but the black pixels will be full alpha channel
  56.  
  57. can only be called once after pygame.init() and before somewindow = pygame.display.set_mode()
  58.  
  59. from:http://www.pygame.org/docs/ref/display.html#pygame.display.set_icon comments
  60. """
  61. #create a surface for icon
  62. icon=pygame.Surface((256,256))
  63. icon.set_colorkey((255,255,255))#and call that color transparent
  64. rawicon= pygame.image.load(iconname)#must be 32x32, black is transparent
  65. for i in range(0,256):
  66. for j in range(0,256):
  67. icon.set_at((i,j), rawicon.get_at((i,j)))
  68. pygame.display.set_icon(icon)#set wind
  69.  
  70.  
  71.  
  72. #handle keys
  73. def key_event(event):
  74.  
  75. ##WORKING ON THIS: TRY TO MAKE THIS A LIST INSTEAD
  76. ## OF ALL THE DIRECTIONS ONE AFTER ANOTHER
  77. if event.type == pygame.KEYUP:
  78. #tuple below is directions
  79. if event.key in (pygame.K_DOWN,pygame.K_UP,pygame.K_LEFT,pygame.K_RIGHT):
  80. #for brick in bricks:
  81. #brick.setMove(moving=False, dir =brick.dir)
  82. circle.stopGo(False)
  83. elif event.type == pygame.KEYDOWN:
  84. if event.key in (pygame.K_DOWN,pygame.K_UP,pygame.K_LEFT,pygame.K_RIGHT,\
  85. pygame.K_w,pygame.K_a,pygame.K_s,pygame.K_d):
  86.  
  87. keyDirs = {
  88. pygame.K_DOWN : DOWN,
  89. pygame.K_UP : UP,
  90. pygame.K_LEFT: LEFT,
  91. pygame.K_RIGHT: RIGHT,
  92. pygame.K_s : DOWN,
  93. pygame.K_w : UP,
  94. pygame.K_a: LEFT,
  95. pygame.K_d: RIGHT,
  96. }
  97.  
  98. direction= keyDirs[event.key]
  99. circle.changeDir(direction)
  100. circle.stopGo(True)
  101.  
  102.  
  103.  
  104.  
  105. elif event.key == pygame.K_c:
  106. circle.shooter.fire(circle.direction,'bullet')
  107. pass
  108.  
  109. #clear screen, slopilly.
  110. elif event.key == pygame.K_RETURN:
  111. screen.fill((0,0,0))
  112.  
  113. #change animation
  114.  
  115. elif event.key in (pygame.K_0,pygame.K_1,pygame.K_2,
  116. pygame.K_3,pygame.K_4,pygame.K_5,
  117. pygame.K_6,pygame.K_7,pygame.K_8,
  118. pygame.K_9):
  119.  
  120. keys = {pygame.K_0 :9,
  121. pygame.K_1 :0,
  122. pygame.K_2 :1,
  123. pygame.K_3 :2,
  124. pygame.K_4 :3,
  125. pygame.K_5 :4,
  126. pygame.K_6 :5,
  127. pygame.K_7 :6,
  128. pygame.K_8 :7,
  129. pygame.K_9 :8}
  130.  
  131. circle.curFrameNum = keys[event.key]
  132. circle.curFrame = circle.animList[circle.curFrameNum]
  133. print 'changed frame'
  134.  
  135.  
  136. #scale images to
  137.  
  138.  
  139. class anything:
  140. def __init__(self, (x,y),name, animFolder,
  141. direction=RIGHT, moving=False,
  142. shooter= None):
  143. self.x, self.y = (x,y)
  144. self.name = name
  145.  
  146. self.animList = self.animLister(animFolder)
  147. self.curFrameNum = 0
  148.  
  149. self.direction = direction
  150. self.facing = self.direction
  151. self.curFrame = self.animList[self.curFrameNum]
  152. self.moving = moving
  153.  
  154. self.shooter = shooter
  155.  
  156. if self.shooter: self.shooter.owner = self
  157.  
  158. def scaleImage(self,image):
  159.  
  160. #loads image to raw
  161. raw = pygame.image.load(image).convert_alpha()
  162. #raw.set_colorkey(WHITE)
  163.  
  164.  
  165. #h and w of the raw image
  166. H = raw.get_height()
  167. W = raw.get_width()
  168. #filled white surface with black color key
  169. ref = pygame.Surface((H,W)) #ref = Refined
  170. #ref.set_colorkey(WHITE)
  171. #ref.fill((255,255,255))
  172.  
  173.  
  174. #for every pixel in raw, put in ref
  175. #for i in range(0,W):
  176. #for j in range(0,H):
  177. #ref.set_at((i,j), raw.get_at((i,j)))
  178.  
  179. ref.blit(raw,(0,0))
  180. print 'colorkey', ref.get_colorkey()
  181. #SET SCALE HERE
  182. scaled = pygame.transform.scale(ref, (25,25))
  183.  
  184. imagename = image.split('/')[-1]
  185. print '{0}\'s scaled width: {1}'.format(imagename,scaled.get_width())
  186. return scaled
  187.  
  188.  
  189.  
  190. def animLister(self,folder):
  191. '''must have jpgs named 1 thru 100
  192. and this func will go through them in that
  193. order and add them to a list to animate'''
  194.  
  195.  
  196. #func to list all jpgs in folder
  197. #then count em all for len later
  198. animList = []
  199. for file in glob.glob(folder+'/*.jpg'):
  200. print file
  201. frame = self.scaleImage(file)
  202. animList.append(frame)
  203.  
  204. return animList
  205.  
  206. def drawFrame(self):
  207.  
  208. #get height and width
  209. H=self.curFrame.get_height()
  210. W=self.curFrame.get_width()
  211.  
  212.  
  213. #create surface to blit image to
  214. #then blit that to screen
  215. toBlit = pygame.Surface((W,H))
  216. toBlit.blit(self.curFrame,(0,0))
  217. screen.blit(toBlit, (self.x,self.y))
  218. #print self.name, ' is drawing'
  219.  
  220. def changeDir(self,direction, moving = True):
  221. #- check pos
  222. #- rotate part of the way
  223. #- go
  224.  
  225. #self.direction[0] = direction[0]
  226. #self.direction[1] = direction[1]
  227. self.direction = direction
  228. self.move()
  229. #self.drawFrame()
  230.  
  231. def stopGo(self,move):
  232. self.moving = move
  233.  
  234. def move(self):
  235. #print 'MOVE ', self.facing, self.direction
  236. if self.moving:
  237. if self.facing != self.direction:
  238. self.rotate()
  239. self.x += self.direction[0]
  240. self.y += self.direction[1]
  241.  
  242. def rotate(self):
  243.  
  244. f = self.facing
  245. d = self.direction
  246. print 'before', f,d
  247. #frame = self.animList[self.curFrame]
  248. #frame2 = frame.copy()
  249. frame = self.curFrame
  250. if (f,d) in acrossTurns.items():
  251. self.curFrame= pygame.transform.rotate(frame,180)
  252. print 'rotate across!'
  253. self.facing = self.direction
  254. elif (f,d) in leftTurns.items():
  255. print 'rotate left!'
  256. self.facing = self.direction
  257. self.curFrame= pygame.transform.rotate(frame,90)
  258. elif (f,d) in rightTurns.items():
  259. print 'rotate right!'
  260. self.facing = self.direction
  261. self.curFrame= pygame.transform.rotate(frame,-90)
  262. print 'after', f,d
  263.  
  264. class shooter:
  265. def __init__(self, level, maxHp):
  266. self.level = level
  267. self.maxHp = maxHp
  268. self.curHp = maxHp
  269.  
  270. def fire(self, direction, variety):
  271. #spawn and fire projectile in direction
  272. bull= projectile(self.owner,self.owner.facing, bob, 'bullet')
  273. bullets.append(bull)
  274. pass
  275.  
  276. class projectile():
  277. def __init__(self, source, direction, target, variety):
  278. self.source = source
  279. self.direction = direction
  280. self.target = target
  281. self.variety = variety
  282. self.fire()
  283.  
  284. def fire(self):
  285. #get rect of curFrame's center x y
  286. self.x,self.y = self.source.curFrame.get_rect().center
  287.  
  288. #spawn at first position out from source
  289. # add the center of the rect to the center of
  290. # shooter then move bit a bit
  291. self.x = self.x + self.source.x + self.direction[0]
  292. self.y = self.y + self.source.y + self.direction[1]
  293. self.draw()
  294.  
  295. print '*'*8
  296. print self.source.curFrame.get_rect().center
  297. print '*'*8
  298.  
  299. def move(self):
  300. self.x += self.direction[0]
  301. self.y += self.direction[1]
  302.  
  303. def draw(self):
  304.  
  305. height= 5
  306. pygame.draw.rect(screen,(100, 100, 100),
  307. (self.x, self.y, height, height))
  308.  
  309. def hit(self):
  310.  
  311. bullets.remove(self)
  312. # ########INIT######### #
  313. pygame.init()
  314. pygame.display.set_caption('Line Moving App')
  315. seticon('bricks.ico')
  316.  
  317. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  318. screen.fill(WHITE)
  319.  
  320. #background = pygame.Surface(screen.get_size())
  321. #background = background.convert()
  322. #background.fill(WHITE)
  323.  
  324. clock = pygame.time.Clock()
  325.  
  326. pygame.display.flip()
  327.  
  328. basicFont = pygame.font.SysFont(None, 48)
  329.  
  330. # ############### #
  331.  
  332.  
  333.  
  334. pathname = os.path.abspath(os.path.curdir)
  335. pathname += '/art/man/'
  336. print pathname
  337. shot = shooter(1,100)
  338. circle = anything((400,200),'Josh', pathname,shooter=shot)
  339. men.append(circle)
  340.  
  341. bob = anything((200,200),'Bob',pathname)
  342. men.append(bob)
  343.  
  344. running = 1
  345. while running:
  346. screen.fill(WHITE)
  347. #check for movements, and draw after
  348. for object in men:
  349. object.move()
  350. #print object.facing, object.direction
  351. object.drawFrame()
  352.  
  353. for object in bullets:
  354. object.move()
  355. object.draw()
  356. for event in pygame.event.get():
  357.  
  358. if event.type == pygame.QUIT:
  359. running = 0
  360.  
  361. elif event.type == pygame.KEYDOWN:
  362. key_event(event)
  363.  
  364. elif event.type == pygame.KEYUP:
  365. key_event(event)
  366.  
  367.  
  368. pygame.display.flip()
  369. clock.tick(30)
  370.  
Advertisement
Add Comment
Please, Sign In to add comment