EQ
AND
TRUE
10
WHILE
i
1
10
1
j
BREAK
0
ADD
1
1
ROOT
9
SIN
45
PI
EVEN
0
ROUND
3.1
SUM
64
10
50
1
100
1
100
item
abc
FIRST
text
abc
FROM_START
text
FROM_START
FROM_START
text
UPPERCASE
abc
BOTH
abc
abc
TEXT
abc
0
0
1
1
0
1
90
1
0.5
1
"Hello world"
0
0
5
FIRST
GET
FROM_START
SET
FROM_START
FROM_START
FROM_START
SPLIT
,
#Main Script
from processing import *
import random
import math
import time
### START ENGINE CODE
class Data:
url = "https://s3-us-west-1.amazonaws.com/media.pixelpad.io/Blockly_Assets/"
#Ivo-Engine stuff
objs = []
#object are only destroyed at the end of the frame
toBeDestroyed = []
inputkeys = {} #dict
gameWidth = 800
gameHeight = 500
refWidth = 800
refHeight = 500
scaleRatio = 1
reloadEndOfFrame = False
main = None
images = {}
#Base-class of every object to enable start & update
class MonoBehaviour:
#REGION variables
x = 0
y = 0
#used for sorting
z = 0
rot = 0
#used for z-sorting internally
prevZ = -1000
#should be changed through setScale only!
scale = 1
currAnim = None
inGameScreen = True
min_x = 0
min_y = 0
max_x = 0
max_y = 0
markForUpdateSize = False
#ENDREGION
def setAnim(self, anim):
self.currAnim = anim
def setScale(self, val):
self.scale = val
#REGION Sprites
sprite = None
def set_sprite(self, url, width = None, height = None):
if (url not in Data.images):
Data.images[url] = loadImage(Data.url + url)
self.sprite = Data.images[url]
if (width != None):
self.set_width(width)
if (height != None):
self.set_height(height)
#self.updateMinMax() should be called after...
def set_width(self, width):
self.sprite.width = width
def set_height(self, height):
self.sprite.height = height
def move_forward(self, value):
self.x += math.cos(radians(self.rot-90))*value
self.y -= math.sin(radians(self.rot-90))*value
def move_right(self, value):
self.x += math.cos(radians(self.rot))*value
self.y -= math.sin(radians(self.rot))*value
#ENDREGION
#REGION Collision
def get_real_width(self):
return self.sprite.width * self.scale
def get_real_height(self):
return self.sprite.height * self.scale
def get_min_x(self):
return self.x - self.get_real_width() * 0.5
def get_max_x(self):
return self.x + self.get_real_width() * 0.5
b
def get_min_y(self):
return self.y - self.get_real_height() * 0.5
def get_max_y(self):
return self.y + self.get_real_height() * 0.5
#TODO only objects inside the gamescreen collide?
def collisionCheck(self, objType):
if (not self.inGameScreen):
return False;
for obj in Data.objs:
if (obj.inGameScreen):
if (obj is not self and type(obj).__name__ == objType):
if (self.intersects(obj)):
return obj
return False
def intersects(self, obj2):
return not (self.get_max_x() < obj2.get_min_x() or self.get_min_x() > obj2.get_max_x() or self.get_min_y() > obj2.get_max_y() or self.get_max_y() < obj2.get_min_y())
def check_in_gamescreen(self):
return not (self.get_max_x() < -Data.refWidth * 0.5 or self.get_min_x() > Data.refWidth * 0.5 or self.get_min_y() > Data.refHeight * 0.5 or self.get_max_y() < -Data.refHeight*0.5)
#ENDREGION
#REGION base functions
def start_INTERNAL(self):
#commented since empty objects can just display nothing
self.set_sprite("Empty.png")
self.start()
def update_INTERNAL(self):
self.update()
#we use translate instead of setting the objects position, to fix rotation issues
posX = (int)((self.x+Data.refWidth*0.5)*Data.scaleRatio)
posY = (int)((-self.y+Data.refHeight*0.5)*Data.scaleRatio)
translate(posX, posY)
rotate(radians(self.rot))
#if (self.currAnim != None):
# self.newSprite = self.currAnim.play()
self.inGameScreen = self.check_in_gamescreen()
if (self.inGameScreen):
image(self.sprite, 0, 0, self.get_real_width()*Data.scaleRatio, self.get_real_height()*Data.scaleRatio)
rotate(-radians(self.rot))
translate(-posX, -posY)
#Base functions to be overridden by any class inheriting from MonoBehaviour
def start(self):
pass
def update(self):
pass
def isMouseOver(self):
return (Data.mouse.x > self.get_min_x() and Data.mouse.x < self.get_max_x() and Data.mouse.y > self.get_min_y() and Data.mouse.y < self.get_max_y())
#ENDREGION
class Anim:
images = None
frames = 0
framesPerImage = 5
imageIndex = 0
timer = 0
#FRAMES OER IMAGE IS NOT THE FRAMERATE... Confusing, sorry
def __init__(self, url, frames, framesPerImage = 3):
self.images = []
self.framesPerImage = framesPerImage
self.frames = frames
for i in range(frames):
fullUrl = Data.url + url + str(i+1) + ".png"
img = requestImage(fullUrl)
self.images.append(img)
#should be called from a MonoBehaviour every frame while playing.
#looks like this: self.newSprite = currAnim.play()
def play(self):
self.timer += 1
if (self.timer >= self.framesPerImage):
self.timer = 0
self.imageIndex += 1
if (self.imageIndex >= self.frames):
self.imageIndex = 0
return self.images[self.imageIndex]
def instantiate(objtype, posX = 0, posY = 0):
n = objtype()
n.x = posX
n.y = posY
n.start_INTERNAL()
Data.objs.append(n)
return n
def destroy(obj):
#Check if objects wasn't already marked as destroyed.
if obj not in Data.toBeDestroyed:
Data.toBeDestroyed.append(obj)
def key_is_pressed(k):
if (k not in Data.inputkeys):
return False
return Data.inputkeys[k] == 1 or Data.inputkeys[k] == 2
def key_was_pressed(k):
if (k not in Data.inputkeys):
return False
return Data.inputkeys[k] == 1
def key_was_released(k):
if (k not in Data.inputkeys):
return False
return Data.inputkeys[k] == 3
def reload_game():
Data.reloadEndOfFrame = True
#SHOULD BE CALLED AT END OF DRAW
def reload_game_immediate():
Data.reloadEndOfFrame = False
Data.objs = []
Data.inputkeys = {}
Data.main = Main()
Data.main.start()
class Main(MonoBehaviour):
def start(self):
print("No Blockly Main class")
class Global:
pass
### END ENGINE CODE
################################################################
#BLOCKLY_REPLACE
################################################################
### START HELPER CODE
#z-sorting
#bubblesort with extra check for each element if it changed
def bubbleSort(alist):
pop = 0
for passnum in range(len(alist)-1,0,-1):
if (alist[passnum].z != alist[passnum].prevZ):
for i in range(passnum):
pop += 1
if alist[i].z > alist[i+1].z:
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
alist[passnum].prevZ = alist[passnum].z
#print("Sorted " + str(pop) + " elements")
### END HELPER CODE
### START PROCESSING CODE
def setup():
#SIZE_REPLACE
size(Data.gameWidth, Data.gameHeight)
Data.scaleRatio = float(Data.gameWidth) / Data.refWidth
Data.invScaleRatio = float(1)/Data.scaleRatio
imageMode(CENTER)
frameRate(30)
textSize(32*Data.scaleRatio)
reload_game_immediate()
#run first frame right away
draw()
def draw():
background(0)
Data.mouse = PVector((mouse.x* Data.invScaleRatio-Data.refWidth*0.5), -(mouse.y * Data.invScaleRatio-Data.refHeight*0.5))
#bubbleSort(Data.objs)
Data.main.update()
for obj in Data.objs:
obj.update_INTERNAL()
#printObjs()
#if was pressed, go to is pressed now
for key in Data.inputkeys:
if (Data.inputkeys[key] == 1):
Data.inputkeys[key] = 2
#if was released, go to off now
if (Data.inputkeys[key] == 3):
Data.inputkeys[key] = 0
for obj in Data.toBeDestroyed:
Data.objs.remove(obj)
Data.toBeDestroyed = []
if (Data.reloadEndOfFrame):
reload_game_immediate()
#DevTools.showFPS()
#DevTools.printObjs()
class DevTools:
lastFrame = 0
avg = 0
nFramesToUpdate = 5
nFrame = 0
def showFPS():
DevTools.nFrame += 1
if (DevTools.nFrame >= DevTools.nFramesToUpdate):
m = millis()
deltaTime = m-DevTools.lastFrame
DevTools.lastFrame = m
DevTools.nFrame = 0
DevTools.avg = DevTools.nFramesToUpdate*1000/deltaTime
ui_text("FPS: " + str(DevTools.avg), 650, 50)
def printObjs():
#objs = ""
i = 0
j = 0
for obj in Data.objs:
#objs += str(obj.z) + ", "
i+=1
if (obj.inGameScreen):
j+=1
ui_text("Objs: " + str(i) + ", in screen: " + str(j), 450, 100)
def processKeyOrMousePress(val):
if (val not in Data.inputkeys or Data.inputkeys[val] == 0):
Data.inputkeys[val] = 1
def processKeyOrMouseRelease(val):
Data.inputkeys[val] = 3
def ui_text(txt, posX, posY):
pX = (int)((posX+Data.refWidth*0.5)*Data.scaleRatio)
pY = (int)((-posY+Data.refHeight*0.5)*Data.scaleRatio)
text(txt, pX, pY )
#Processing funcs
def keyPressed():
processKeyOrMousePress(str(keyboard.key))
def keyReleased():
processKeyOrMouseRelease(str(keyboard.key))
def mousePressed():
if (mouse.button == 37):
processKeyOrMousePress("m_left")
if (mouse.button == 39):
processKeyOrMousePress("m_right")
def mouseReleased():
if (mouse.button == 37):
processKeyOrMouseRelease("m_left")
if (mouse.button == 39):
processKeyOrMouseRelease("m_right")
run()
### END PROCESSING CODE
{"main":"<xml xmlns=\"http://www.w3.org/1999/xhtml\"><block type=\"main_class_object\" id=\"M|TUBcRXfP-zXMfslc+k\" x=\"45\" y=\"45\"><statement name=\"start\"><block type=\"instantiate\" id=\"we)[@O2%3j2Y0W+B9N@f\"><field name=\"NAME\">skyy</field><next><block type=\"instantiate\" id=\"yybxn;m9z-)yOx;Z_T~:\"><field name=\"NAME\">bloc</field></block></next></block></statement></block><block type=\"class_object\" id=\"AUuWqj*x%Rwy?3j*im`s\" x=\"255\" y=\"75\"><field name=\"NAME\">skyy</field><statement name=\"start\"><block type=\"set_sprite\" id=\")nRMS(mdurVWA}F$+:KF\"><value name=\"SPRITE\"><block type=\"sprite\" id=\"hP+mr?1oPMPf3VRilDz;\"><field name=\"SPRITE\">https://s3-us-west-1.amazonaws.com/media.pixelpad.io/Blockly_Assets/Flappy/SkyTileSprite.png</field></block></value></block></statement></block><block type=\"class_object\" id=\"Hj7p,T]uG-ndE9bXwu=!\" x=\"165\" y=\"226\"><field name=\"NAME\">bloc</field><statement name=\"start\"><block type=\"set_sprite\" id=\"!`ExKN[mbTAL6;pR++=6\"><value name=\"SPRITE\"><block type=\"sprite\" id=\"BIp,Q,0!U@D=u,.#U1.y\"><field name=\"SPRITE\">https://s3-us-west-1.amazonaws.com/media.pixelpad.io/Blockly_Assets/Dungeon/spider_run.png</field></block></value></block></statement><statement name=\"update\"><block type=\"controls_if\" id=\"gl=]:co_Nng)`~b+*k?`\"><value name=\"IF0\"><block type=\"key_input\" id=\"`1o$rjb6vZX@]{:=f073\"><field name=\"NAME\">w</field><field name=\"PRESSED\">key_was_pressed</field></block></value><statement name=\"DO0\"><block type=\"move_forward\" id=\"9ISd*gzlr6o@#rCvpt@t\"><value name=\"NAME\"><shadow type=\"math_number\" id=\"uF0ee*T70/v+@OVASq-0\"><field name=\"NUM\">10</field></shadow></value></block></statement><next><block type=\"controls_if\" id=\"](lu!eedvKWoR5xiW^Oi\"><value name=\"IF0\"><block type=\"key_input\" id=\"oK@/KOklyN%1)3qb4O(y\"><field name=\"NAME\">d</field><field name=\"PRESSED\">key_was_pressed</field></block></value><statement name=\"DO0\"><block type=\"change_rot\" id=\"po}l#_o[KxFX]5MacinT\"><value name=\"NAME\"><shadow type=\"math_number\" id=\".d}(`r%m{)!Smy_8Dz9@\"><field name=\"NUM\">90</field></shadow></value><next><block type=\"move_right\" id=\"b^/JBao%qWIa7U*nM/a$\"><value name=\"NAME\"><shadow type=\"math_number\" id=\"HaYQAOQ_!U0jirBvcYi=\"><field name=\"NUM\">10</field></shadow></value></block></next></block></statement><next><block type=\"controls_if\" id=\"n#qY912)Wx{6Fx!oQksT\"><value name=\"IF0\"><block type=\"key_input\" id=\"+mFc@L|!}18YHx/[m6wx\"><field name=\"NAME\">s</field><field name=\"PRESSED\">key_was_pressed</field></block></value><statement name=\"DO0\"><block type=\"change_rot\" id=\"w65[WcS|D[PZoHLdzFN2\"><value name=\"NAME\"><shadow type=\"math_number\" id=\"HqTmrA`{0g9*U,119A2Y\"><field name=\"NUM\">180</field></shadow></value><next><block type=\"move_right\" id=\"cuDsaCT/:[o^d[KaO0B`\"><value name=\"NAME\"><shadow type=\"math_number\" id=\"%@IpB/-eC{rK93Tes=!b\"><field name=\"NUM\">10</field></shadow></value></block></next></block></statement><next><block type=\"controls_if\" id=\"U+rMAgsT@@K|BY131%y`\"><value name=\"IF0\"><block type=\"key_input\" id=\",/r?9LbKMW9U.kfY/u=@\"><field name=\"NAME\">a</field><field name=\"PRESSED\">key_was_pressed</field></block></value><statement name=\"DO0\"><block type=\"change_rot\" id=\"#uT7wx-f4ikzBg{zk3{+\"><value name=\"NAME\"><shadow type=\"math_number\" id=\"#v@XspB#O42=-ZNI!$.R\"><field name=\"NUM\">270</field></shadow></value><next><block type=\"move_right\" id=\"xMT@9Vx!7CkP2m*,lz1)\"><value name=\"NAME\"><shadow type=\"math_number\" id=\"rkxZ00NaGQJc/4R1mV}6\"><field name=\"NUM\">10</field></shadow></value></block></next></block></statement></block></next></block></next></block></next></block></statement></block><block type=\"class_object\" id=\"djfzOkYto#u{%fF^.QGu\" x=\"-15\" y=\"315\"><field name=\"NAME\">birb</field><statement name=\"start\"><block type=\"set_sprite\" id=\"?if%J_Vkz(W6NS*~4Jc8\"><value name=\"SPRITE\"><block type=\"sprite\" id=\"Zs=L!25,(+*D8P;/Um0[\"><field name=\"SPRITE\">https://s3-us-west-1.amazonaws.com/media.pixelpad.io/Blockly_Assets/Flappy/BirdHero.png</field></block></value></block></statement></block></xml>"}