Need help with basic animation - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Need help with basic animation (/thread-13705.html) |
Need help with basic animation - niro - Oct-28-2018 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 RE: Need help with basic animation - nilamo - Oct-28-2018 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 :) RE: Need help with basic animation - niro - Oct-28-2018 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 () RE: Need help with basic animation - niro - Oct-28-2018 Anyone? I really need help with this, it's driving me crazy RE: Need help with basic animation - nilamo - Oct-28-2018 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() RE: Need help with basic animation - niro - Oct-28-2018 Thank you so much, I am going to exercise it RE: Need help with basic animation - niro - Nov-04-2018 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
|