Python Forum
Need help with basic animation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with basic animation
#1
Hi

I will first say I am very new to python, so I'll need your patience :)
I am trying to gradually fill the screen with thin vertical rectangles/ thicker lines. I would like them to appear in sequence, one after the other, and continue for as long as the program run.

I was able to create with pygame the first rectangle in the dimensions and position I wanted, but I am not able to gradually add more rectangles.

ANy suggestions on how I should approach this? preferably using pygame, but not necessarily.

there is more to it, but I would like to first get it working

Thank you
Reply
#2
If you're successfully getting the first step done, then show us what you've got, and we'll give tips on how you can keep going. It doesn't really do you any good if we just hand you a bunch of code you don't understand :)
Reply
#3
I agree, so here is what i have so far, I want the second rectangle to appear half a second to a second after the first one does.Ideally, i would like it to continue generating rectangles to fill the screen one after the other
import pygame
from pygame.locals import *
from sys import exit

pygame.init()

disp_info = pygame.display.Info()
width = disp_info.current_w 
height = disp_info.current_h

blue = 0, 0, 255
red = 255, 0, 0
orange = 255, 128, 0
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('color theory')
done = False
clock = pygame.time.Clock()


while not done:
    for event in pygame.event.get ():
        if event.type == pygame.QUIT:
            done = True
            
    screen.fill((255,255,255))
    rect1 = pygame.draw.rect(screen,(red), (0,0,30,height),0)
    rect2 = pygame.draw.rect(screen,(orange), (30,0,30,height),0)

    pygame.display.update()
    clock.tick(0)
pygame.quit ()
Reply
#4
Anyone? I really need help with this, it's driving me crazy
Reply
#5
Ok, so the easy answer is to keep track of when you started, and then do things based on how long you've been running:
import pygame
import time
 
pygame.init()
 
disp_info = pygame.display.Info()
width = disp_info.current_w 
height = disp_info.current_h
 
blue = 0, 0, 255
red = 255, 0, 0
orange = 255, 128, 0
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('color theory')
done = False
clock = pygame.time.Clock()
 
start = time.time()
while not done:
    for event in pygame.event.get ():
        if event.type == pygame.QUIT:
            done = True
             
    screen.fill((255,255,255))
    rect1 = pygame.draw.rect(screen,(red), (0,0,30,height),0)
    running_time = time.time() - start
    if running_time > 0.5:
        rect2 = pygame.draw.rect(screen,(orange), (30,0,30,height),0)
 
    pygame.display.update()
    clock.tick(0)
pygame.quit()
However, for more than just a few things, that'll quickly grow into a big chain of if-statements. So what might be better, is a list of bars that are visible, and you just add a new one every half a second. This might be a way to start, though I never handle (0, 255, 0), so that's an exercise for you lol
import pygame
import time
 
pygame.init()
 
disp_info = pygame.display.Info()
width = disp_info.current_w 
height = disp_info.current_h
 
blue = 0, 0, 255
red = 255, 0, 0
orange = 255, 128, 0
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('color theory')
done = False
clock = pygame.time.Clock()
 
last_bar_time = time.time()
visible_colors = []
while not done:
    for event in pygame.event.get ():
        if event.type == pygame.QUIT:
            done = True
             
    screen.fill((255,255,255))

    since_last_bar = time.time() - last_bar_time
    if since_last_bar > 0.5:
        last_bar_time = time.time()
        new_bar = [0, 0, 0]
        if visible_colors:
            new_bar = visible_colors[-1][:]
        step = 51
        for ndx, value in enumerate(new_bar):
            if value <= (255-step):
                new_bar[ndx] += step
                break
        visible_colors.append(new_bar)

    width = 30
    for ndx, color in enumerate(visible_colors):
        pygame.draw.rect(screen, color, (ndx*width, 0, width, height), 0)

    pygame.display.update()
    clock.tick(0)
pygame.quit()
Reply
#6
Thank you so much, I am going to exercise it
Reply
#7
Hi,thank you so much for last time that helped a lot
I have a new challenge, again, very basic, I have one vertical rectangle that moves in steps through the screen, I would like to know how i can keep the previous squares on the screen, and just add to them, similar idea to what you helped me with last time, but I would like to use my color changing rectangle at the "leader" and to keep the last color it changed to.
Thank you

here is what i have, I hope this time im adding it the right way

Output:
import pygame from pygame.locals import * from sys import exit pygame.init() disp_info = pygame.display.Info() width = disp_info.current_w height = disp_info.current_h #blue = 0, 0, 255 red = 255 green = 10 blue = 0 green2 = 0 realRed = (red, green, blue) orange = 255, 128, 0 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption('color theory') done = False clock = pygame.time.Clock() counter = 1 posX = 0 #width1 = 30 widthX = posX + 30 while True: for event in pygame.event.get (): if event.type == pygame.QUIT: done = True screen.fill((255,255,255)) rect = pygame.draw.rect(screen, (red, green, blue), (posX, 0, widthX, height), 0) # pygame.draw.rect(screen, (red, green2, blue), (posX - , 0, widthX, height), 0) green += 10 # green2 = green - 10 if green > 255: green = 10 posX += 30 if posX >= width: posX = 0 # width1 += 30 print('rgb = ',red, green, blue) print('posX = ', posX, 'widthX=', widthX) ## print("realRed =", realRed) pygame.display.update() clock.tick(4) pygame.quit ()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020