How can I get camera scrolling to work in Lua (using the PlayDate SDK)? - lua

I have three issues with my PlayDate game, one big, two small:
#1 (small): I can't get camera scrolling to work;
#2 (small): I can't figure out how to not get circles to generate inside of eachother;
#3 (big): I have NO IDEA how to make a menu screen and have it change the songs (I haven't added the songs yet, I'm still writing them)
Here's my code:
import "CoreLibs/graphics"
import "CoreLibs/object"
import "CoreLibs/sprites"
import "CoreLibs/nineslice"
import "CoreLibs/timer"
import "CoreLibs/frameTimer"
import "config"
import "core/definitions"
import "core/cotton/all"
import "scripts/index"
import "core/lieb/all"
import "core/CoreGame"
bpm = 60
lastX = math.random(80, 320)
lastY = math.random(80, 160)
circles = {}
tf = playdate.geometry.affineTransform.new()
local circles = {}
local count = 0
function addCircle()
tf:rotate(math.random(360))
x, y = (tf * playdate.geometry.point.new(80, 0)):unpack()
x += lastX
y += lastY
local new_circle = playdate.graphics.drawCircleAtPoint(x, y, 40)
count = count + 1
table.insert(circles, new_circle)
lastX = x
lastY = y
playdate.graphics.setDrawOffset(x, y)
end
beatTimer = playdate.timer.performAfterDelay(60000 / bpm, addCircle)
beatTimer.repeats = true
function playdate.update()
playdate.timer.updateTimers()
end
For the menu, I know I should use grid view, but I don't know how to make things happen when something is selected, and different things when something else is selected.
For the scrolling, I want it so that you're always seeing the circles and they never go off screen, so I'm trying to change the camera's origin point to where the circles are, but it's not working.
And finally, #2 is pretty self explanatory.

Related

Multi-Mobject FadeIn and FadeOut Simultaneously in manim

I want to draw multiple Circles that appear and disappear almost simultaneously.
How should I write the code?
If the scene I want to draw is the scene like all the circles appear and then all the circles disappear, then, I could write it like case A in the code below, but that is not what I want to do.
How can I draw the scene where some circles appear after some disappear?
I tried Trial1 but it didn't show anything.
from manim import *
class MultiMobjectFadeInAndOut(Scene):
def construct(self):
circles = [Circle(r / 10) for r in range(10)]
anims_in = [FadeIn(c, scale=10) for c in circles]
anims_out = [FadeOut(c, scale=10) for c in circles]
# Case A
self.play(LaggedStart(*anims_in, lag_ratio=0.1))
self.play(LaggedStart(*anims_out, lag_ratio=0.1))
# Trial 1
anims = []
anims.extend(anims_in)
anims.extend(anims_out)
self.play(LaggedStart(*anims, lag_ratio=0.1))
I'm using Manim Community v0.16.0.post0

Resize selected area of image

I want to increase / decrease the height of the image for the selected area only (The area between the white lines) as depicted in the image and not the outside of that area.
This is the same functionality which is performed in the app Manly - Body Muscle Editor Pro
How can I achieve that? Any help is appreciated.
I've never written code for IOS but I know OpenCV also works in IOS. Here I use the cv2.resize.
import cv2
import numpy as np
img = cv2.imread("1.jpg")
print(img.shape)
h = img.shape[0]
w = img.shape[1]
part_to_resize = img[120:240,:]
old_height = 120 #240-120
new_height = 200
final_result = np.zeros((h-(240-120)+new_height,w,3),dtype='uint8')
final_result[0:119,:] = img[0:119,:]
final_result[120:320,:] = cv2.resize(part_to_resize, (w, new_height))
final_result[321:h-old_height+new_height,:] = img[241:h,:]
cv2.imshow("final_result", final_result)
cv2.imshow("img", img)
cv2.waitKey()

Kotlin Foreach loop isn't updating width

New to Kotlin, working on a simple chain of circles. I have been able to get two circles to connect the way I want but can seem to grow the chain further. Seems like the width (w2) doesn't get updated after the first iteration. Let me know why my code isn't working and how I can improve it.
Thank you in advance :) Stay woke!
val iterator = (0..12).iterator()
if (iterator.hasNext()) {
canvas.drawCircle(w.toFloat(), h.toFloat(), (100).toFloat(),brush1)
iterator.next()
}
iterator.forEach {
val w2 = w-100
canvas.drawCircle((w2).toFloat(), h.toFloat(), (100).toFloat(),brush1)
}
here is the kind of effect I'm looking to create
w2 will never change because it's based on w which is never modified.
You can use parameter provided to lambda (it) which tells you what iteration you're on, and not use weird iterator:
val x = 100 // starting x
val inc = 100 // offset for following circles
repeat(12){
val targetX = x + inc * it
canvas.drawCircle(targetX.toFloat(), y.toFloat(), 100.toFloat(), brush)
}

making a UIimage move every 1 second

I have a UIimage which I want to move according to X and Y given by certain equations ( physics projectile time equations) and I want it to move every 1 second so that it would appear to the user as if it's actually moving not just disappearing and reappearing at the last position, plus the positions given off are wrong I think. I would appreciate any help for either or both problems.
So far I tried this:
The movement function:
func moveCannonBall(toX:Int , toY:Int ){
var frm: CGRect = ballimageview.frame
frm.origin.x = frm.origin.x + CGFloat(toX)
frm.origin.y = frm.origin.y - CGFloat(toY)
ballimageview.frame = frm
}
On button click it's supposed to take the user's inputs (gravity, angle, and the initial speed)
#IBAction func getAlpha( sender:AnyObject){
gravity = Float(g[pickerView.selectedRow(inComponent: 0)])
xMax = ((V0*V0)*sin(2.0*angle))/gravity
It's supposed to stop every 1 second but only the calculations pause every 1 second and the UIimage just disappears and reappears just
while Int(toX) < Int(xMax) {
sleep(1)
t = t + 1
toX = V0*cos(angle)*t
toY = -0.5*gravity*t*t + V0*sin(angle)*t
moveCannonBall(toX: Int(toX), toY: Int(toY))
}
}
Any help is appreciated.
As Losiowaty said UIView.animationWithDuration is the way to go. Check out this answer to see how the syntax is: https://stackoverflow.com/a/30992214/5257729

Need help in using turtle module

So I'm writing a program using the turtle module. I have 2 questions:
I have 4 turtles. How can I make their names show up on the screen while drawing?
After I finish drawing, how can I exit one screen and open another within the same program?
I have 4 turtles. How can I make their names show up on the screen
while drawing?
Sure, here's a crude example:
from random import choice
from turtle import Turtle, Screen
NAMES = ['Donnie', 'Raph', 'Mickey', 'Leo']
SPEEDS = ["slowest", "slow", "normal", "fast", "fastest"]
FONT = ("Arial", 12, "normal")
MAGNIFICATION = 5
STAMP_SIZE = 20
MAXIMUM_SPEED = 10
LINE_OFFSET = 50
screen = Screen()
WINDOW_WIDTH = screen.window_width()
START, FINISH = LINE_OFFSET - WINDOW_WIDTH//2, WINDOW_WIDTH//2 - (LINE_OFFSET + MAXIMUM_SPEED)
turtles = {name: Turtle(shape='turtle') for name in NAMES}
for offset, turtle in enumerate(turtles.values(), start=-len(NAMES)//2):
turtle.turtlesize(MAGNIFICATION)
turtle.color("black", "white")
turtle.penup()
turtle.goto(START, offset * MAGNIFICATION * STAMP_SIZE)
turtle.speed(choice(SPEEDS))
turtle.write("", font=FONT) # dummy write for 1st undo
winner = False
while not winner:
for name, turtle in turtles.items():
turtle.undo() # unwrite name
turtle.forward(turtle.speed() + 1)
turtle.write(name, font=FONT) # rewrite name
if turtle.xcor() >= FINISH:
winner = True
break
screen.exitonclick()
To do better with respect to text centering and eliminating flicker, we would need one or more extra invisible turtles to just handle the text. However, when the turtles turn, the text won't turn with them, it'll always be horizontal.

Resources