Friday, April 5, 2024

Jump!!

 New tutorials in the Aspen Game series form Codemy. Player now jumps up

at speed 15 (-1*), when the spacebar is pressed.




import pygame
import self as self

#Define a 2d vector
vector = pygame.math.Vector2

#Initialize the game
pygame.init()

# Set display surface (divisible by 32 tile size)
WINDOW_WIDTH = 960 # 30 columns
WINDOW_HEIGHT = 640 # 20 rows
display_surface = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
pygame.display.set_caption("Aspen Platformer - Codemy.com")

# Set FPS and clock
FPS = 60
clock = pygame.time.Clock()

# Tile Class
class Tile(pygame.sprite.Sprite):
# Read and Create tiles and put em on the screen
def __init__(self, x, y, image_integer, main_group, sub_group=""):
super().__init__()
# Load image and add to the tile subgroups
if image_integer == 1:
self.image = pygame.image.load('images/Brown.png')
elif image_integer == 2:
self.image = pygame.image.load('images/Green.png')
sub_group.add(self)
elif image_integer == 3:
self.image = pygame.image.load('images/Blue.png')
sub_group.add(self)

# add every tile to main tile group
main_group.add(self)

# Get rect of images and position within the grid
self.rect = self.image.get_rect()
self.rect.topleft = (x,y)

# Apsen Player Class
class Aspen(pygame.sprite.Sprite):
def __init__(self, x, y, grass_tiles, water_tiles):
super().__init__()
# Define our aspen image
self.image = pygame.image.load("images/P.png")
# Get rect
self.rect = self.image.get_rect()
# Position aspen
self.rect.bottomleft = (x,y)

#Define our grass and water
self.grass_tiles = grass_tiles
self.water_tiles = water_tiles

# Kinematic Vectors (x,y)
self.position = vector(x,y)
self.velocity = vector(0,0)
self.acceleration = vector(0,0)

# Kinematic Constants
self.HORIZONTAL_ACCELERATION = 2
self.HORIZONTAL_FRICTION = 0.20
self.VERTICAL_ACCELERATION = 0.5 #Gravity
self.VERTICAL_JUMP_SPEED = 15

def jump(self):
# Only when player is on grass
if pygame.sprite.spritecollide(self, self.grass_tiles, False):
self.velocity.y = -1 * self.VERTICAL_JUMP_SPEED # jumping upwards thus negative

def update(self):
# set initial acceleration to 0,0 to start
self.acceleration = vector(0, self.VERTICAL_ACCELERATION)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.acceleration.x = -1 * self.HORIZONTAL_ACCELERATION
if keys[pygame.K_RIGHT]:
self.acceleration.x = self.HORIZONTAL_ACCELERATION

# Calculate new Kinematics
self.acceleration.x -= self.velocity.x * self.HORIZONTAL_FRICTION
self.velocity += self.acceleration
self.position += self.velocity + 0.5 * self.acceleration

# update rect
self.rect.bottomleft = self.position

# Check for collisions with Grass
touched_platforms = pygame.sprite.spritecollide(self, self.grass_tiles, False) # Return a python list of tiles we touched
if touched_platforms:
self.position.y = touched_platforms[0].rect.top + 1
self.velocity.y = 0

# Check for collisions with Water
if pygame.sprite.spritecollide(self, self.water_tiles, False):
print("You Died!")




# Define our sprite groups
main_tile_group = pygame.sprite.Group()
grass_tile_group = pygame.sprite.Group()
water_tile_group = pygame.sprite.Group()
aspen_group = pygame.sprite.Group()


# Create a tile map, nested python list: 0=no tile, 1=dirt, 2=grass, 3=water, 4=aspen
tile_map = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[2,2,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2],
[1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2],
[1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0,0,2,2,2,2,2],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,1,1,1,1,1]
]

# Create Tile objects from the tile map
# 2 for loops because tile map is nested. 20 i down
for i in range(len(tile_map)):
# loop through the 30 elements in each list, j across
for j in range(len(tile_map[i])):
# Check for 0,1,2,3
if tile_map[i][j] == 1:
# dirt
Tile(j*32, i*32, 1, main_tile_group)
elif tile_map[i][j] == 2:
# grass
Tile(j*32, i*32, 2, main_tile_group, grass_tile_group)
elif tile_map[i][j] == 3:
# water
Tile(j*32, i*32, 3, main_tile_group, water_tile_group)
elif tile_map[i][j] == 4:
aspen = Aspen(j*32, i*32 + 32, grass_tile_group, water_tile_group)
aspen_group.add(aspen)


# Add a background
bg_image = pygame.image.load('images/bg.png')
bg_image_rect = bg_image.get_rect()
bg_image_rect.topleft = (0,0)



# Game Loop
running = True
while running:
# Check to quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Jump
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
aspen.jump()

# fill the display or blit an image
# display_surface.fill("black")
display_surface.blit(bg_image, bg_image_rect)

# Draw the Tiles
main_tile_group.draw(display_surface)

# Update and draw sprites
aspen_group.update()
aspen_group.draw(display_surface)


# Update Display
pygame.display.update()
clock.tick(FPS)

# End the game
pygame.quit()

Set-Up

 Just thought I would share a set-up model which I find quite useful

when I want to go through a difficult exercise. I keep two screens running, and it is very

easy to do. 

Go to a Web page of interest, click on the windows icon and the left arfrow at the

same time. Page one now occupies the left half of the screen. Repeat with a helper

site, and click windows + the right arrow. 


                                                        




                                                                 *     *     *

Big nostalgia moment for German group Alphaville, right now.


Thursday, April 4, 2024

Stops

 Still not happy with my Eclipse Gif. It is to be expected that the

observer's surroundings will not go completely dark but have something

of a twilight effect because the sun is still shining from behind the

occultation. but how do I show this:

                                                    

Perplexity just helped me figure out how to  run an opacity gradient at the same

time as a color one. It turns out that the opacity on a gradient can be set for each

color point independently.

Here is my test:

The background layer is bright red.

                                                                        


    The layer on top has variable opacity:

                                                                           


This is set when one defines each of the stops. First a royal blue set at 50%:

                                                                            


And a white stops set at 100%:


                                                                                     


The result:

                                                                               


 One is free to add as many stops as one wants.                                
                                                             

                                                                     *     *     *

I can go further, and put the speed of the transition on a curve. One goes to the

gradient layer, right-clicks for a Distortions layer, then Curved Warp.


                                                                      















Sanity

 Powering through this no coffee exercise. Another seven days to go

before it ends. Will I start drinking coffee again once this is over. I wish

I could be sure I won't. 


One issue that keeps coming up is that of sleep patterns. Those who have 

successfully quit say they now sleep through the night, are more rested, and

remember their dreams. It is, at this point, the contrary for me. I have sleepless 

hours in the night, and crash sleep during the day. And yes, I have been remembering

my dreams again, but my dreams are anxious and nightmarish. 


I know that dreaming is really about bits of memory being moved about in the brain. 

I dream of dead people; of having bad relationships with dead people I have in reality 

never even met. The only advantage, here, is that I know these episodes were dreams

rather than a colorful life. Just have to settle for sanity, I guess...


So who needs dreams when there is Facebook. All manner of weird things show

up on my feed: Housing renovation, recipes, Royalty,  language lessons (really enjoying 

the German language ones). Then, recently, a poem by Aragon in the original French

about his beloved combing her hair 'in the  midst of this, our tragedy' (1942 and war with

no end in sight). Below:

https://www.bacfrancais.com/commentaire/poesie/aragon-elsa-au-miroir#Po%C3%A8me%20%C3%A9tudi%C3%A9

Wednesday, April 3, 2024

Receptors

 Starting week 4 of no coffee. Woke up this morning looking forward

to researching adenosine receptors, and thought to myself, 'I'll just make the

coffee quickly so I can get to work'...Which just goes to show that we 

consume things out of habit, and not just cravings!!


So the story on adenosine receptors is this. One person on YouTube explained

caffeine addiction by the body ending up with a surfeit of adenosine receptors

and not adenosine as such. This actually makes sense if we consider that

adenosine in the body is present as a result of the breakdown of adenosine triphosphate

or ATP, the energy component of the Krebs cycle, the digestion cycle.


Finally got a straight-up overview from Gemeni AI. Below:

                                                                     


                                                                             


So, too many receptors it is. Good work from a layman caffeine victim:

https://youtu.be/iRD29IVwwe4?si=cJO7JYBX6xwQtnn2


                                                                             


Tuesday, April 2, 2024

Some Terms

 

                                                                                  




Partial eclipses occur 2, 3 times per year; total eclipses every three years. But

for any one specific location, a total eclipse might happen once every 375 years. 

So this one, which will affect the eastern US and Canada is a big deal for us.


Below, a NASA interactive to appreciate the scale of the event.

https://science.nasa.gov/eclipses/future-eclipses/eclipse-2024/where-when/

Monday, April 1, 2024

GIFS

 Been experimenting with making a GIF file from Synfig:


Just uploaded as an image...


Now I need to create a better effect!!