Forum Archive

Creating a Sunset scene

FarmerPaco

Hi, I am very new at this. I am trying to get a sunset background, preferably using a HSB with dwindling value of the brightness.

I have tried just importing time using Range and Sleep which of course didn't work.

I tried with the code below to at least transition with delay from one color to another but can't seam to figure it out delay. Surely there is a simple way to create sunset where I get blue background to transition to black while leaving a Sprite alien to watch it.
Please help. I learn best from examples. I believe I am supposed to use action delay but would really appreciate if someone could show me an example of code that transitions color in the background so I can understand it better. Thanks in advance.

from scene import *
import time

class FirstScene(Scene):
def setup(self):
self.start_time = time.time()
SpriteNode(anchor_point=(0, 0), color='white', parent=self,
size=self.size)

def update(self):
    newColor = ['blue','green']
    if not self.presented_scene and time.time() - self.start_time > 2:
        SpriteNode(anchor_point=(0, 0), color= newColor[0], parent=self,
               size=self.size)
        self.start_time = time.time()
    if not self.presented_scene and time.time() - self.start_time > 2:
        SpriteNode(anchor_point=(0, 0), color= newColor[1], parent=self,
               size=self.size)

main_view = SceneView()
main_view.scene = FirstScene()
main_view.present(hide_title_bar=True, animated=False)

FarmerPaco

Okay, so I tried to work it out, and it does work but I know there is a better way. Any hints? Please help.

Here is my last version:

from scene import *
import time
import colorsys
colors = []

for i in range(0, 100, 1):
    b = i / 100.0
    color = colorsys.hsv_to_rgb(0.56, 0.88, (b))
    colors.append(color)
    print(color)



i= -1
class sunsetScene(Scene):
    def setup(self):
        # get starting time
        self.start_time = time.time()
        global i
        if i <  (len(colors)-1):
            i+=1
        SpriteNode(anchor_point=(0, 0), color= colors[i], parent=self,
                   size=self.size) 

    def update(self):
        # move to new scene after 2 seconds
        if not self.presented_scene and time.time() - self.start_time > .2:
            self.present_modal_scene(sunsetScene())

main_view = SceneView()
main_view.scene = sunsetScene()
main_view.present(hide_title_bar=True, animated=False)
Phuket2

@FarmerPaco , I don't use Scene. I just wanted to look at what you had done. I can't spot what's wrong here, but something is very wrong. One of the loops must be out of control and I think allocating a lot of memory. It didn't crash my iPad Pro, but it was unresponsive. Even double tap on the home button took forever. I say, I think it's eating memory, because even when I could get into another app the app I changed to also was struggling to show the keyboard. But seems something major is wrong. Sorry I don't have the answer for you

FarmerPaco

@Phuket2 Thanks for the guidance. I made a simpler version which runs smoothly, but I am still using Scene. Was hoping someone could show me or point me in a better direction.

This is the improved version tha doesn't choke on memory.

```
from scene import *
import time
import colorsys
colors = []

for i in range(0, 100, 1):
b = i / 100.0
color = colorsys.hsv_to_rgb(0.56, 0.88, (b))
colors.append(color)

i = 0
class sunsetScene(Scene):
def setup(self):
# get starting time
self.start_time = time.time()
global i
self.background_color = 'black'

def update(self):
    global i
    # update background color after .1 seconds
    if time.time() - self.start_time > .1:

        self.background_color = colors[i]
        if i <  (len(colors)-1):
            i+=1
        self.start_time = time.time()

main_view = SceneView()
main_view.scene = sunsetScene()
main_view.present(hide_title_bar=True, animated=False)```

omz

@FarmerPaco Here's a simplified version of your code, hope it helps:

from scene import *
import colorsys

class SunsetScene(Scene):
    def setup(self):
        self.background_color = 'black'
        self.b = 0.0

    def update(self):
        # NOTE: self.t is a built-in timestamp variable, no need to calculate this yourself.
        if self.t > 0.1:
            self.background_color = colorsys.hsv_to_rgb(0.56, 0.88, self.b)
            self.b += 0.005

main_view = SceneView()
main_view.scene = SunsetScene()
main_view.present(hide_title_bar=True, animated=False)
omz

Alternative (slightly shorter) version, using Actions:

from scene import *
import colorsys
A = Action

class SunsetScene(Scene):
    def setup(self):
        self.background_color = 'black'
        def transition(node, p):
            node.background_color = colorsys.hsv_to_rgb(0.56, 0.88, p)
        self.run_action(A.sequence(A.wait(0.1), A.call(transition, 1.0)))

main_view = SceneView()
main_view.scene = SunsetScene()
main_view.present(hide_title_bar=True, animated=False)
FarmerPaco

Thank you Ole, this is what I was looking for, and thank you especially for taking time to answer bye so I can see the difference.

I want to mention that I use your app at a code school in Matsumoto, Japan. Until now I have taught with a block code app called Hopscotch but we needed to move up and I decided Python over Ruby. Your app makes it possible to learn Python on iPad.

I am very grateful for this. Your app is a huge win for education!