document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import pygame
  2. from pygame.locals import *
  3.  
  4. import os, sys, random
  5.  
  6. class Chara:
  7.     def __init__(self,im=None,x=320,y=240):
  8.         if im==None:
  9.             self.base = pygame.surface.Surface((32,64))
  10.             self.base.fill((255,0,255))
  11.         else:
  12.             self.base = im
  13.         self.rotation = 0
  14.         self.image = self.base
  15.         self.scaption = None
  16.         self.caption = ""
  17.         self.x = x
  18.         self.y = y
  19.        
  20.     def display(self,context):
  21.         context.blit(self.image, (self.x, self.y))
  22.         if self.scaption != None:
  23.             context.blit(self.scaption, (self.x-(self.scaption.get_width()-self.image.get_width())/2, self.y-self.scaption.get_height()))
  24.            
  25.     def setCaption(self,cap):
  26.         self.caption = cap
  27.         if self.caption == "":
  28.             self.scaption = None
  29.         else:
  30.             self.scaption = tfont.render(self.caption, 1, (255, 255, 255))
  31.  
  32. class ScriptCommand:
  33.     def __init__(self):
  34.         pass
  35.     def execute(self, context):
  36.         return True
  37.        
  38. class PrintCommand(ScriptCommand):
  39.     def __init__(self,text):
  40.         ScriptCommand.__init__(self)
  41.         self.text = text
  42.     def execute(self, context):
  43.         context.setCaption(self.text)
  44.         return True
  45.        
  46. class WaitCommand(ScriptCommand):
  47.     def __init__(self,length):
  48.         ScriptCommand.__init__(self)
  49.         self.waitlength = length
  50.         self.counter = -1
  51.     def execute(self, context):
  52.         if self.counter < 0:
  53.             self.counter = 0
  54.         self.counter += clock.get_time()
  55.         return self.counter > self.waitlength
  56.        
  57. class TurnCommand(ScriptCommand):
  58.     def __init__(self,angle):
  59.         ScriptCommand.__init__(self)
  60.         self.counter = 0
  61.         self.angle = angle
  62.     def execute(self, context):
  63.         if self.counter >= self.angle:
  64.             context.rotation += self.angle
  65.             return True
  66.         self.counter += 1
  67.         context.image = pygame.transform.rotate(context.base, context.rotation+self.counter)
  68.         return False
  69.        
  70. class MoveCommand(ScriptCommand):
  71.     def __init__(self,targetx,targety):
  72.         self.targetx = targetx
  73.         self.targety = targety
  74.     def moveX(self,context):
  75.         if context.x < self.targetx:
  76.             context.x += 1
  77.         elif context.x > self.targetx:
  78.             context.x -= 1
  79.     def moveY(self,context):
  80.         if context.y < self.targety:
  81.             context.y += 1
  82.         elif context.y > self.targety:
  83.             context.y -= 1
  84.     def execute(self, context):
  85.         #self.moveX(context)
  86.         #self.moveY(context)
  87.        
  88.         dx = abs(context.x-self.targetx)
  89.         dy = abs(context.y-self.targety)
  90.         if (dx < dy):
  91.             if (dx>0):
  92.                 self.moveX(context)
  93.             else:
  94.                 self.moveY(context)
  95.         else:
  96.             if (dy>0):
  97.                 self.moveY(context)
  98.             else:
  99.                 self.moveX(context)
  100.        
  101.         return (self.targetx==context.x) and (self.targety==context.y)
  102.  
  103. class Script:
  104.     def __init__(self,filename):
  105.         self.context = None
  106.         self.task = []
  107.         self.current = -1
  108.         self.load(filename)
  109.        
  110.     def attach(self,context):
  111.         self.context = context
  112.        
  113.     def load(self,filename):
  114.         scriptfile = open(filename, "r")
  115.         commands = scriptfile.readlines()
  116.         for l in commands:
  117.             token = l.split(" ")
  118.             if token[0] == "print":
  119.                 msg = l.split("\\"")
  120.                 self.task.append(PrintCommand(msg[1]))
  121.             elif token[0] == "move":
  122.                 self.task.append(MoveCommand(int(token[1]), int(token[2])))
  123.             elif token[0] == "wait":
  124.                 self.task.append(WaitCommand(int(token[1])))
  125.             elif token[0] == "turn":
  126.                 self.task.append(TurnCommand(int(token[1])))
  127.            
  128.                
  129.     def run(self):
  130.         if self.current != -1:
  131.             if self.task[self.current].execute(self.context):
  132.                 self.current += 1
  133.                 if self.current >= len(self.task):
  134.                     self.stop()
  135.    
  136.     def start(self):
  137.         if len(self.task)>0:
  138.             self.current = 0
  139.    
  140.     def stop(self):
  141.         self.current = -1
  142.  
  143.  
  144. pygame.init()
  145. pygame.font.init()
  146. pygame.display.init()
  147. screen = pygame.display.set_mode((640,480), pygame.DOUBLEBUF|pygame.HWSURFACE)
  148. clock = pygame.time.Clock()
  149. tfont = pygame.font.Font(None, 14)
  150.  
  151. actors = [Chara(pygame.image.load("kapal.png").convert_alpha()), Chara(pygame.image.load("kapal_musuh.png").convert_alpha())]
  152. scripts = []
  153. script = Script("script.txt")
  154. script.attach(actors[0])
  155. script.start()
  156. scripts.append(script)
  157.  
  158. script = Script("script2.txt")
  159. script.attach(actors[1])
  160. script.start()
  161. scripts.append(script)
  162.  
  163. running = True
  164. while running:
  165.     screen.fill((127, 127, 0))
  166.    
  167.     for actor in actors:
  168.         actor.display(screen)
  169.     for script in scripts:
  170.         script.run()
  171.    
  172.     pygame.display.update()
  173.    
  174.     for event in pygame.event.get():
  175.         if event.type == pygame.QUIT:
  176.             running = False
  177.     clock.tick(60)
');