Errors output to screen, python text function accepts all types now
This commit is contained in:
parent
12ed3d3cb1
commit
5845ad2e8d
2
dependencies/pocketpy/include/pocketpy/vm.h
vendored
2
dependencies/pocketpy/include/pocketpy/vm.h
vendored
@ -229,7 +229,7 @@ public:
|
||||
PyObject* call_method(PyObject* self, PyObject* callable, Args&&... args){
|
||||
PUSH(callable);
|
||||
PUSH(self);
|
||||
_push_varargs(args...);
|
||||
_push_varargs(static_cast<PyObject *>(args)...);
|
||||
return vectorcall(sizeof...(args));
|
||||
}
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
class ball:
|
||||
class Ball:
|
||||
def __init__(self):
|
||||
self.x = rnd(10, 100)
|
||||
self.y = rnd(10, 100)
|
||||
@ -32,5 +32,3 @@ class ball:
|
||||
|
||||
|
||||
circle(self.x, self.y, 3, self.color)
|
||||
|
||||
|
||||
|
||||
115
python/main.py
115
python/main.py
@ -1,59 +1,82 @@
|
||||
import ball
|
||||
# import ball
|
||||
|
||||
linecol = 15
|
||||
def draw_line(x0, y0, x1, y1, c):
|
||||
dx = abs(x1 - x0)
|
||||
dy = abs(y1 - y0)
|
||||
sx = 1 if x0 < x1 else -1
|
||||
sy = 1 if y0 < y1 else -1
|
||||
err = dx - dy
|
||||
# linecol = 15
|
||||
# def draw_line(x0, y0, x1, y1, c):
|
||||
# dx = abs(x1 - x0)
|
||||
# dy = abs(y1 - y0)
|
||||
# sx = 1 if x0 < x1 else -1
|
||||
# sy = 1 if y0 < y1 else -1
|
||||
# err = dx - dy
|
||||
|
||||
while x0 != x1 or y0 != y1:
|
||||
pixel(x0, y0, c)
|
||||
e2 = 2 * err
|
||||
if e2 > -dy:
|
||||
err -= dy
|
||||
x0 += sx
|
||||
if e2 < dx:
|
||||
err += dx
|
||||
y0 += sy
|
||||
# while x0 != x1 or y0 != y1:
|
||||
# pixel(x0, y0, c)
|
||||
# e2 = 2 * err
|
||||
# if e2 > -dy:
|
||||
# err -= dy
|
||||
# x0 += sx
|
||||
# if e2 < dx:
|
||||
# err += dx
|
||||
# y0 += sy
|
||||
|
||||
|
||||
balls = []
|
||||
# balls = []
|
||||
|
||||
for i in range(2):
|
||||
balls.append(ball.ball())
|
||||
# for i in range(2):
|
||||
# balls.append(ball.Ball())
|
||||
|
||||
# counter = 0
|
||||
|
||||
# def update():
|
||||
# global linecol
|
||||
# global counter
|
||||
# counter += 1
|
||||
# #clear(0)
|
||||
# draw_line(balls[0].x, balls[0].y, balls[1].x, balls[1].y, linecol)
|
||||
|
||||
# for ball in balls:
|
||||
# ball.draw()
|
||||
|
||||
# for i in range(64):
|
||||
# for j in range(20):
|
||||
# pixel(i * 4, j, i)
|
||||
# pixel(i * 4 + 1, j, i)
|
||||
# pixel(i * 4 + 2, j, i)
|
||||
# pixel(i * 4 + 3, j, i)
|
||||
|
||||
# t = "Hello from python FPS:" + str(fps()) + " X:" + str(mouseX) + " Y:" + str(mouseY)
|
||||
# for i in range(len(t)):
|
||||
# text(t[i], 4 + (i * 7), 4, 20 + i)
|
||||
|
||||
# circle(mouseX, mouseY, 10, counter % 64)
|
||||
|
||||
|
||||
# clear(0)
|
||||
# text("test", 2,2, 32)
|
||||
import easing
|
||||
import time
|
||||
funcs = [easing.InSine, easing.OutSine, easing.InOutSine, easing.InQuad, easing.OutQuad, easing.InOutQuad, easing.InBounce, easing.OutBounce, easing.InOutBounce, easing.InElastic, easing.OutElastic, easing.InOutElastic]
|
||||
|
||||
test = {"1" : 0, "2" : 4}
|
||||
counter = 0
|
||||
|
||||
x = 0
|
||||
def update():
|
||||
global linecol
|
||||
cls()
|
||||
global x
|
||||
global counter
|
||||
counter += 1
|
||||
#clear(0)
|
||||
draw_line(balls[0].x, balls[0].y, balls[1].x, balls[1].y, linecol)
|
||||
counter += 0.008
|
||||
x += 0.5
|
||||
if(counter > 1):
|
||||
counter = 0
|
||||
|
||||
for ball in balls:
|
||||
ball.draw()
|
||||
for i in range(len(funcs)):
|
||||
pos = funcs[i](counter)
|
||||
circle(100 + (pos * 200), 10 + (14 * i), 5, i + 35)
|
||||
|
||||
for i in range(64):
|
||||
for j in range(20):
|
||||
if(i == 0 or i == 63 or j == 0 or j == 20):
|
||||
pixel(i * 4, j, i)
|
||||
pixel(i * 4 + 1, j, i)
|
||||
pixel(i * 4 + 2, j, i)
|
||||
pixel(i * 4 + 3, j, i)
|
||||
else:
|
||||
pixel(i * 4, j, 0)
|
||||
pixel(i * 4 + 1, j, 0)
|
||||
pixel(i * 4 + 2, j, 0)
|
||||
pixel(i * 4 + 3, j, 0)
|
||||
t = "Hello from python FPS:" + str(fps()) + " X:" + str(mouseX) + " Y:" + str(mouseY)
|
||||
for i in range(len(t)):
|
||||
text(t[i], 4 + (i * 7), 4, 20 + i)
|
||||
|
||||
circle(mouseX, mouseY, 10, counter % 64)
|
||||
text("The FPS: " + str(fps()), 2, 2, 9)
|
||||
|
||||
|
||||
clear(0)
|
||||
text("test", 2,2, 32)
|
||||
def cls():
|
||||
for i in range(width):
|
||||
for j in range(height):
|
||||
pixel(i , j, int(i + j + x) % 64)
|
||||
|
||||
@ -148,14 +148,17 @@ void Graphics::bindMethods(pkpy::VM *vm) {
|
||||
return vm->None;
|
||||
}});
|
||||
|
||||
vm->bind(vm->builtins, "text(t: string, x: int, y: int, color: int)", [this](pkpy::VM* vm, pkpy::ArgsView args){{
|
||||
std::string s = pkpy::py_cast<pkpy::Str&>(vm, args[0]).str();
|
||||
vm->bind(vm->builtins, "text(t: string, x: int, y: int, color: int)", [this](pkpy::VM* vm, pkpy::ArgsView args){
|
||||
pkpy::PyObject* func_str = vm->builtins->attr("str");
|
||||
pkpy::PyObject* result = vm->call(func_str, args[0]);
|
||||
std::string s = pkpy::py_cast<pkpy::Str&>(vm, result).str();
|
||||
|
||||
float x = pkpy::py_cast<float>(vm, args[1]);
|
||||
float y = pkpy::py_cast<float>(vm, args[2]);
|
||||
float paletteIndex = pkpy::py_cast<float>(vm, args[3]);
|
||||
this->Text(s, x, y, paletteIndex);
|
||||
return vm->None;
|
||||
}});
|
||||
});
|
||||
}
|
||||
|
||||
void Graphics::Clear(int paletteIndex) {
|
||||
@ -183,9 +186,11 @@ void Graphics::endDraw() {
|
||||
EndTextureMode();
|
||||
}
|
||||
|
||||
void Graphics::updateVMMouse(pkpy::VM* vm) {
|
||||
void Graphics::updateVMVars(pkpy::VM* vm) {
|
||||
vm->builtins->attr().set("mouseX", pkpy::py_var(vm, mouseX()));
|
||||
vm->builtins->attr().set("mouseY", pkpy::py_var(vm, mouseY()));
|
||||
vm->builtins->attr().set("width", pkpy::py_var(vm, m_screenWidth));
|
||||
vm->builtins->attr().set("height", pkpy::py_var(vm, m_screenHeight));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -46,7 +46,7 @@ public:
|
||||
void beginDraw();
|
||||
void endDraw();
|
||||
|
||||
void updateVMMouse(pkpy::VM* vm);
|
||||
void updateVMVars(pkpy::VM* vm);
|
||||
|
||||
void loadPalette(std::string path);
|
||||
int mouseX();
|
||||
|
||||
@ -31,7 +31,7 @@ Pycron::Pycron() {
|
||||
m_graphics = new Graphics{virtualScreenWidth, virtualScreenHeight, initialScale};
|
||||
m_graphics->loadPalette("../resources/palette2.hex");
|
||||
m_graphics->bindMethods(m_vm);
|
||||
m_graphics->updateVMMouse(m_vm);
|
||||
m_graphics->updateVMVars(m_vm);
|
||||
m_stateManager = new StateManager(this);
|
||||
|
||||
|
||||
@ -54,7 +54,7 @@ void Pycron::StartGameLoop() {
|
||||
if (IsKeyPressed(KEY_F)) {
|
||||
m_graphics->toggleFullScreen();
|
||||
}
|
||||
m_graphics->updateVMMouse(m_vm);
|
||||
m_graphics->updateVMVars(m_vm);
|
||||
m_graphics->draw(this->m_stateManager);
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,9 +20,12 @@ void StateManager::RequestStateChange(StateManager::StateType state) {
|
||||
|
||||
if(m_currentState){
|
||||
// Game state needs ability to draw during code loading
|
||||
if(state == GAME){
|
||||
if(m_currentState == m_gameState){
|
||||
m_pycron->m_graphics->beginDraw();
|
||||
m_currentState->OnEnter();
|
||||
if(m_gameState->m_errorThrown){
|
||||
m_pycron->m_graphics->Text(m_gameState->m_previousError, 2, 2, 59);
|
||||
}
|
||||
m_pycron->m_graphics->endDraw();
|
||||
}else{
|
||||
m_currentState->OnEnter();
|
||||
@ -32,7 +35,17 @@ void StateManager::RequestStateChange(StateManager::StateType state) {
|
||||
|
||||
void StateManager::Draw(Graphics *graphics) {
|
||||
if(m_currentState){
|
||||
m_currentState->Draw(graphics);
|
||||
if(m_currentState == m_gameState){
|
||||
if(m_gameState->m_errorThrown){
|
||||
m_pycron->m_graphics->Text(m_gameState->m_previousError, 2, 2, 59);
|
||||
}
|
||||
else{
|
||||
m_currentState->Draw(graphics);
|
||||
}
|
||||
}
|
||||
else{
|
||||
m_currentState->Draw(graphics);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -42,8 +42,8 @@ void GameState::PreProcessScripts() {
|
||||
loadPythonModules(pythonSources);
|
||||
std::string main = pythonSources[MAIN_FILE];
|
||||
|
||||
pkpy::CodeObject_ code = m_vm->compile(main, MAIN_FILE, pkpy::EXEC_MODE, false);
|
||||
try{
|
||||
pkpy::CodeObject_ code = m_vm->compile(main, MAIN_FILE, pkpy::EXEC_MODE, false);
|
||||
m_vm->_exec(code, m_vm->_main);
|
||||
m_updateFunction = m_vm->eval("update");
|
||||
// if(m_updateFunction == nullptr){
|
||||
|
||||
Loading…
Reference in New Issue
Block a user