Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame.locals import *
- import os, sys, random
- class Chara:
- def __init__(self,im=None,x=320,y=240):
- if im==None:
- self.base = pygame.surface.Surface((32,64))
- self.base.fill((255,0,255))
- else:
- self.base = im
- self.rotation = 0
- self.image = self.base
- self.scaption = None
- self.caption = ""
- self.x = x
- self.y = y
- def display(self,context):
- context.blit(self.image, (self.x, self.y))
- if self.scaption != None:
- context.blit(self.scaption, (self.x-(self.scaption.get_width()-self.image.get_width())/2, self.y-self.scaption.get_height()))
- def setCaption(self,cap):
- self.caption = cap
- if self.caption == "":
- self.scaption = None
- else:
- self.scaption = tfont.render(self.caption, 1, (255, 255, 255))
- class ScriptCommand:
- def __init__(self):
- pass
- def execute(self, context):
- return True
- class PrintCommand(ScriptCommand):
- def __init__(self,text):
- ScriptCommand.__init__(self)
- self.text = text
- def execute(self, context):
- context.setCaption(self.text)
- return True
- class WaitCommand(ScriptCommand):
- def __init__(self,length):
- ScriptCommand.__init__(self)
- self.waitlength = length
- self.counter = -1
- def execute(self, context):
- if self.counter < 0:
- self.counter = 0
- self.counter += clock.get_time()
- return self.counter > self.waitlength
- class TurnCommand(ScriptCommand):
- def __init__(self,angle):
- ScriptCommand.__init__(self)
- self.counter = 0
- self.angle = angle
- def execute(self, context):
- if self.counter >= self.angle:
- context.rotation += self.angle
- return True
- self.counter += 1
- context.image = pygame.transform.rotate(context.base, context.rotation+self.counter)
- return False
- class MoveCommand(ScriptCommand):
- def __init__(self,targetx,targety):
- self.targetx = targetx
- self.targety = targety
- def moveX(self,context):
- if context.x < self.targetx:
- context.x += 1
- elif context.x > self.targetx:
- context.x -= 1
- def moveY(self,context):
- if context.y < self.targety:
- context.y += 1
- elif context.y > self.targety:
- context.y -= 1
- def execute(self, context):
- #self.moveX(context)
- #self.moveY(context)
- dx = abs(context.x-self.targetx)
- dy = abs(context.y-self.targety)
- if (dx < dy):
- if (dx>0):
- self.moveX(context)
- else:
- self.moveY(context)
- else:
- if (dy>0):
- self.moveY(context)
- else:
- self.moveX(context)
- return (self.targetx==context.x) and (self.targety==context.y)
- class Script:
- def __init__(self,filename):
- self.context = None
- self.task = []
- self.current = -1
- self.load(filename)
- def attach(self,context):
- self.context = context
- def load(self,filename):
- scriptfile = open(filename, "r")
- commands = scriptfile.readlines()
- for l in commands:
- token = l.split(" ")
- if token[0] == "print":
- msg = l.split("\"")
- self.task.append(PrintCommand(msg[1]))
- elif token[0] == "move":
- self.task.append(MoveCommand(int(token[1]), int(token[2])))
- elif token[0] == "wait":
- self.task.append(WaitCommand(int(token[1])))
- elif token[0] == "turn":
- self.task.append(TurnCommand(int(token[1])))
- def run(self):
- if self.current != -1:
- if self.task[self.current].execute(self.context):
- self.current += 1
- if self.current >= len(self.task):
- self.stop()
- def start(self):
- if len(self.task)>0:
- self.current = 0
- def stop(self):
- self.current = -1
- pygame.init()
- pygame.font.init()
- pygame.display.init()
- screen = pygame.display.set_mode((640,480), pygame.DOUBLEBUF|pygame.HWSURFACE)
- clock = pygame.time.Clock()
- tfont = pygame.font.Font(None, 14)
- actors = [Chara(pygame.image.load("kapal.png").convert_alpha()), Chara(pygame.image.load("kapal_musuh.png").convert_alpha())]
- scripts = []
- script = Script("script.txt")
- script.attach(actors[0])
- script.start()
- scripts.append(script)
- script = Script("script2.txt")
- script.attach(actors[1])
- script.start()
- scripts.append(script)
- running = True
- while running:
- screen.fill((127, 127, 0))
- for actor in actors:
- actor.display(screen)
- for script in scripts:
- script.run()
- pygame.display.update()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement