Key pressed, key down, rectangle, python demo scene
This commit is contained in:
parent
603d764c15
commit
df9e305828
205
python/main.py
205
python/main.py
@ -1,82 +1,153 @@
|
|||||||
# import ball
|
from scene import Scene
|
||||||
|
|
||||||
# linecol = 15
|
fireGradient = [9, 9, 18, 18, 17, 16, 15, 14, 15, 14, 16, 14, 14,14,16, 2, 1]
|
||||||
# def draw_line(x0, y0, x1, y1, c):
|
waterGradient = [9, 9, 43, 43, 42, 41, 40, 39, 40, 39, 41, 39, 39,39,41, 2, 1]
|
||||||
# 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:
|
class Particle:
|
||||||
# pixel(x0, y0, c)
|
def __init__(self, x, y, velX, velY):
|
||||||
# e2 = 2 * err
|
self.x = x
|
||||||
# if e2 > -dy:
|
self.y = y
|
||||||
# err -= dy
|
self.velX = velX
|
||||||
# x0 += sx
|
self.velY = velY
|
||||||
# if e2 < dx:
|
self.c = fireGradient[0]
|
||||||
# err += dx
|
self.size = 5
|
||||||
# y0 += sy
|
|
||||||
|
class Palette(Scene):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
clear(0)
|
||||||
|
|
||||||
|
for i in range(8):
|
||||||
|
for j in range(8):
|
||||||
|
rectangle(10 + (i * 20), 10 + (j * 20), 18, 18, (j * 8 + i))
|
||||||
|
text(str(j * 8 + i), 10 + (i * 20), 10 + (j * 20), 9)
|
||||||
|
|
||||||
|
|
||||||
# balls = []
|
class Main(Scene):
|
||||||
|
|
||||||
# for i in range(2):
|
def __init__(self):
|
||||||
# balls.append(ball.Ball())
|
super().__init__()
|
||||||
|
self.fireParts = []
|
||||||
|
self.waterParts = []
|
||||||
|
for i in range(10):
|
||||||
|
self.fireParts.append(Particle(width/2, height/2, rnd(-1, 1), rnd(-1, 1)))
|
||||||
|
self.waterParts.append(Particle(width/2, height/2, rnd(-1, 1), rnd(-1, 1)))
|
||||||
|
|
||||||
# counter = 0
|
self.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)
|
def update(self):
|
||||||
# text("test", 2,2, 32)
|
global mouseVelX
|
||||||
import easing
|
global mouseVelY
|
||||||
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]
|
self.counter += 1
|
||||||
|
|
||||||
|
for p in self.fireParts[:]:
|
||||||
|
p.x += p.velX
|
||||||
|
p.y += p.velY
|
||||||
|
p.velX += rnd(-0.3,0.3)
|
||||||
|
p.velY += rnd(-0.3,0.3)
|
||||||
|
|
||||||
|
idx = int(p.size / 5.0 * len(fireGradient) - 1)
|
||||||
|
|
||||||
|
p.c = fireGradient[(len(fireGradient) - 1) - idx]
|
||||||
|
|
||||||
|
if(p.size < 1):
|
||||||
|
p.size -= 0.01
|
||||||
|
else:
|
||||||
|
p.size -= 0.05
|
||||||
|
if(p.size < 0):
|
||||||
|
self.fireParts.remove(p)
|
||||||
|
|
||||||
|
for p in self.waterParts[:]:
|
||||||
|
p.x += p.velX
|
||||||
|
p.y += p.velY
|
||||||
|
p.velX += rnd(-0.3,0.3)
|
||||||
|
p.velY += rnd(-0.3,0.3)
|
||||||
|
|
||||||
|
idx = int(p.size / 5.0 * len(fireGradient) - 1)
|
||||||
|
|
||||||
|
p.c = waterGradient[(len(waterGradient) - 1) - idx]
|
||||||
|
|
||||||
|
if(p.size < 1):
|
||||||
|
p.size -= 0.01
|
||||||
|
else:
|
||||||
|
p.size -= 0.05
|
||||||
|
if(p.size < 0):
|
||||||
|
self.waterParts.remove(p)
|
||||||
|
|
||||||
|
if(key(67)):
|
||||||
|
x1 = mouseX + (sin(self.counter * 0.1) * 60)
|
||||||
|
y1 = mouseY + (cos(self.counter * 0.1) * 60)
|
||||||
|
|
||||||
|
x2 = mouseX + (sin(self.counter * 0.1 + (3.141592653)) * 60)
|
||||||
|
y2 = mouseY + (cos(self.counter * 0.1 + (3.141592653)) * 60)
|
||||||
|
for i in range(2):
|
||||||
|
self.fireParts.append(Particle(x1, y1, rnd(-1,1) + mouseVelX * 0.25, rnd(-1,1) + mouseVelY * 0.25))
|
||||||
|
self.waterParts.append(Particle(x2, y2, rnd(-1,1) + mouseVelX * 0.25, rnd(-1,1) + mouseVelY * 0.25))
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
clear(44)
|
||||||
|
for p in self.fireParts:
|
||||||
|
if(p.size <= 1):
|
||||||
|
pixel(p.x, p.y, p.c)
|
||||||
|
else:
|
||||||
|
circle(p.x, p.y, int(p.size), p.c)
|
||||||
|
|
||||||
|
for p in self.waterParts:
|
||||||
|
if(p.size <= 1):
|
||||||
|
pixel(p.x, p.y, p.c)
|
||||||
|
else:
|
||||||
|
circle(p.x, p.y, int(p.size), p.c)
|
||||||
|
text("C to add particles, T to toggle palette", 3, 3, 0)
|
||||||
|
text("C to add particles, T to toggle palette", 2, 2, 30)
|
||||||
|
text("Num Particles: " + str(len(self.fireParts) + len(self.waterParts)), 3, 16, 0)
|
||||||
|
text("Num Particles: " + str(len(self.fireParts) + len(self.waterParts)), 2, 15, 9)
|
||||||
|
|
||||||
|
|
||||||
|
paletteScene = Palette()
|
||||||
|
mainScene = Main()
|
||||||
|
current_scene = mainScene
|
||||||
|
|
||||||
|
oldMouseX = 0
|
||||||
|
oldMouseY = 0
|
||||||
|
|
||||||
|
mouseVelX = 0
|
||||||
|
mouseVelY = 0
|
||||||
|
|
||||||
|
KEY_T = 84
|
||||||
|
|
||||||
|
switch = False
|
||||||
|
|
||||||
test = {"1" : 0, "2" : 4}
|
|
||||||
counter = 0
|
|
||||||
x = 0
|
|
||||||
def update():
|
def update():
|
||||||
cls()
|
global switch
|
||||||
global x
|
global current_scene
|
||||||
global counter
|
global mouseVelX
|
||||||
counter += 0.008
|
global mouseVelY
|
||||||
x += 0.5
|
global oldMouseX
|
||||||
if(counter > 1):
|
global oldMouseY
|
||||||
counter = 0
|
|
||||||
|
|
||||||
for i in range(len(funcs)):
|
mouseVelX = (mouseX - oldMouseX)
|
||||||
pos = funcs[i](counter)
|
mouseVelY = (mouseY - oldMouseY)
|
||||||
circle(100 + (pos * 200), 10 + (14 * i), 5, i + 35)
|
|
||||||
|
|
||||||
text("The FPS: " + str(fps()), 2, 2, 9)
|
oldMouseX = mouseX
|
||||||
|
oldMouseY = mouseY
|
||||||
|
|
||||||
|
|
||||||
def cls():
|
if(keyp(KEY_T)):
|
||||||
for i in range(width):
|
switch = not switch
|
||||||
for j in range(height):
|
if(switch):
|
||||||
pixel(i , j, int(i + j + x) % 64)
|
current_scene = paletteScene
|
||||||
|
else:
|
||||||
|
current_scene = mainScene
|
||||||
|
|
||||||
|
current_scene.update()
|
||||||
|
current_scene.draw()
|
||||||
|
|
||||||
|
circle(mouseX, mouseY, 3, 30)
|
||||||
|
|||||||
10
python/scene.py
Normal file
10
python/scene.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
class Scene:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
pass
|
||||||
@ -19,7 +19,7 @@ Graphics::Graphics(int screenWidth, int screenHeight, int startupScale) : m_scre
|
|||||||
InitWindow(m_startupScreenWidth, m_startupScreenHeight, "test");
|
InitWindow(m_startupScreenWidth, m_startupScreenHeight, "test");
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
m_virtualScreen = LoadRenderTexture(screenWidth, screenHeight);
|
m_virtualScreen = LoadRenderTexture(screenWidth, screenHeight);
|
||||||
|
m_origin = {0,0};
|
||||||
m_virtualScreenLocalBounds = {0.0f, 0.0f, (float)m_virtualScreen.texture.width, -(float)m_virtualScreen.texture.height };
|
m_virtualScreenLocalBounds = {0.0f, 0.0f, (float)m_virtualScreen.texture.width, -(float)m_virtualScreen.texture.height };
|
||||||
m_virtualScreenWindowBounds = {0.0f, 0.0f, (float)m_windowWidth, (float)m_windowHeight};
|
m_virtualScreenWindowBounds = {0.0f, 0.0f, (float)m_windowWidth, (float)m_windowHeight};
|
||||||
calculateScreenPositionInWindow();
|
calculateScreenPositionInWindow();
|
||||||
@ -36,17 +36,6 @@ void Graphics::draw(StateManager* stateManager) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
BeginTextureMode(m_virtualScreen);
|
BeginTextureMode(m_virtualScreen);
|
||||||
// //////////
|
|
||||||
// try{
|
|
||||||
// if(updateFunction != nullptr)
|
|
||||||
// vm->call(updateFunction);
|
|
||||||
// } catch(pkpy::Exception e){
|
|
||||||
// std::cout << e.summary() << std::endl;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// Circle(150,100,50,1);
|
|
||||||
// Text("Hello from C++", 120, 95, 9);
|
|
||||||
// //////////
|
|
||||||
|
|
||||||
stateManager->Draw(this);
|
stateManager->Draw(this);
|
||||||
|
|
||||||
@ -148,6 +137,16 @@ void Graphics::bindMethods(pkpy::VM *vm) {
|
|||||||
return vm->None;
|
return vm->None;
|
||||||
}});
|
}});
|
||||||
|
|
||||||
|
vm->bind(vm->builtins, "rectangle(x: int, y: int, width: int, height: int, color: int)", [this](pkpy::VM* vm, pkpy::ArgsView args){{
|
||||||
|
float x = pkpy::py_cast<float>(vm, args[0]);
|
||||||
|
float y = pkpy::py_cast<float>(vm, args[1]);
|
||||||
|
float width = pkpy::py_cast<float>(vm, args[2]);
|
||||||
|
float height = pkpy::py_cast<float>(vm, args[3]);
|
||||||
|
float paletteIndex = pkpy::py_cast<float>(vm, args[4]);
|
||||||
|
this->Rectangle(x, y, width, height, paletteIndex);
|
||||||
|
return vm->None;
|
||||||
|
}});
|
||||||
|
|
||||||
vm->bind(vm->builtins, "text(t: string, x: int, y: int, color: int)", [this](pkpy::VM* vm, pkpy::ArgsView args){
|
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* func_str = vm->builtins->attr("str");
|
||||||
pkpy::PyObject* result = vm->call(func_str, args[0]);
|
pkpy::PyObject* result = vm->call(func_str, args[0]);
|
||||||
@ -174,6 +173,10 @@ void Graphics::Circle(int x, int y, int radius, int paletteIndex) {
|
|||||||
DrawCircle(x, y, radius, Palette[paletteIndex]);
|
DrawCircle(x, y, radius, Palette[paletteIndex]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Graphics::Rectangle(int x, int y, int width, int height, int paletteIndex) {
|
||||||
|
DrawRectangle(x, y, width, height, Palette[paletteIndex]);
|
||||||
|
}
|
||||||
|
|
||||||
void Graphics::Text(std::string s, int x, int y, int paletteIndex) {
|
void Graphics::Text(std::string s, int x, int y, int paletteIndex) {
|
||||||
DrawText(s.c_str(), x, y, 5, Palette[paletteIndex]);
|
DrawText(s.c_str(), x, y, 5, Palette[paletteIndex]);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,10 +29,10 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// virtual screen
|
// virtual screen
|
||||||
int m_screenWidth;
|
int m_screenWidth = 0;
|
||||||
int m_screenHeight;
|
int m_screenHeight = 0;
|
||||||
|
|
||||||
bool m_windowShouldClose;
|
bool m_windowShouldClose = false;
|
||||||
|
|
||||||
static std::vector<Color> Palette;
|
static std::vector<Color> Palette;
|
||||||
|
|
||||||
@ -58,6 +58,7 @@ public:
|
|||||||
void Clear(int paletteIndex);
|
void Clear(int paletteIndex);
|
||||||
void Pixel(int x, int y, int paletteIndex);
|
void Pixel(int x, int y, int paletteIndex);
|
||||||
void Circle(int x, int y, int radius, int paletteIndex);
|
void Circle(int x, int y, int radius, int paletteIndex);
|
||||||
|
void Rectangle(int x, int y, int width, int height, int paletteIndex);
|
||||||
void Text(std::string s, int x, int y, int paletteIndex);
|
void Text(std::string s, int x, int y, int paletteIndex);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -60,22 +60,24 @@ void Pycron::StartGameLoop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Pycron::bindMethods() {
|
void Pycron::bindMethods() {
|
||||||
m_vm->bind(m_vm->builtins, "rnd(min: int, max: int) -> int", getRandomNumber);
|
m_vm->bind(m_vm->builtins, "rnd(min: float, max: float) -> int", getRandomNumber);
|
||||||
m_vm->bind(m_vm->builtins, "sin(num: float) -> float", getSin);
|
m_vm->bind(m_vm->builtins, "sin(num: float) -> float", getSin);
|
||||||
m_vm->bind(m_vm->builtins, "cos(num: float) -> float", getCos);
|
m_vm->bind(m_vm->builtins, "cos(num: float) -> float", getCos);
|
||||||
m_vm->bind(m_vm->builtins, "fps() -> int", getFPS);
|
m_vm->bind(m_vm->builtins, "fps() -> int", getFPS);
|
||||||
|
m_vm->bind(m_vm->builtins, "keyp(keycode: int) -> bool", getKeyPressed);
|
||||||
|
m_vm->bind(m_vm->builtins, "key(keycode: int) -> bool", getKeyDown);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pkpy::PyObject* Pycron::getRandomNumber(pkpy::VM* vm, pkpy::ArgsView args) {
|
pkpy::PyObject* Pycron::getRandomNumber(pkpy::VM* vm, pkpy::ArgsView args) {
|
||||||
int min = pkpy::py_cast<int>(vm, args[0]);
|
float min = pkpy::py_cast<float>(vm, args[0]);
|
||||||
int max = pkpy::py_cast<int>(vm, args[1]);
|
float max = pkpy::py_cast<float>(vm, args[1]);
|
||||||
// Seed the random number generator with a random device
|
// Seed the random number generator with a random device
|
||||||
std::random_device rd;
|
std::random_device rd;
|
||||||
std::mt19937 gen(rd());
|
std::mt19937 gen(rd());
|
||||||
|
|
||||||
// Define a uniform distribution for the range [min, max]
|
// Define a uniform distribution for the range [min, max]
|
||||||
std::uniform_int_distribution<int> distribution(min, max);
|
std::uniform_real_distribution<float> distribution(min, max);
|
||||||
|
|
||||||
// Generate a random number within the specified range
|
// Generate a random number within the specified range
|
||||||
return pkpy::py_var(vm, distribution(gen));
|
return pkpy::py_var(vm, distribution(gen));
|
||||||
@ -95,3 +97,13 @@ pkpy::PyObject* Pycron::getFPS(pkpy::VM* vm, pkpy::ArgsView args) {
|
|||||||
return pkpy::py_var(vm, GetFPS());
|
return pkpy::py_var(vm, GetFPS());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pkpy::PyObject *Pycron::getKeyPressed(pkpy::VM *vm, pkpy::ArgsView args) {
|
||||||
|
bool pressed = IsKeyPressed(pkpy::py_cast<int>(vm, args[0]));
|
||||||
|
return pkpy::py_var(vm, pressed);
|
||||||
|
}
|
||||||
|
|
||||||
|
pkpy::PyObject *Pycron::getKeyDown(pkpy::VM *vm, pkpy::ArgsView args) {
|
||||||
|
bool held = IsKeyDown(pkpy::py_cast<int>(vm, args[0]));
|
||||||
|
return pkpy::py_var(vm, held);
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@ -33,5 +33,7 @@ public:
|
|||||||
static pkpy::PyObject* getSin(pkpy::VM* vm, pkpy::ArgsView args);
|
static pkpy::PyObject* getSin(pkpy::VM* vm, pkpy::ArgsView args);
|
||||||
static pkpy::PyObject* getCos(pkpy::VM* vm, pkpy::ArgsView args);
|
static pkpy::PyObject* getCos(pkpy::VM* vm, pkpy::ArgsView args);
|
||||||
static pkpy::PyObject* getFPS(pkpy::VM* vm, pkpy::ArgsView args);
|
static pkpy::PyObject* getFPS(pkpy::VM* vm, pkpy::ArgsView args);
|
||||||
|
static pkpy::PyObject* getKeyPressed(pkpy::VM* vm, pkpy::ArgsView args);
|
||||||
|
static pkpy::PyObject* getKeyDown(pkpy::VM* vm, pkpy::ArgsView args);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user