Re: [pygame] Rain Generator
I tried but nothing happens.. Have to talk to the WingIDE guys. Good hint tough, tnx! Am 28.10.2006 um 03:37 schrieb Ryan Charpentier: Just use the Execute Current File option in the debug menu.
Re: [pygame] Rain Generator
Kamilche wrote: Kai Kuehne wrote: Looks similar to http://www.scriptedfun.com/pygame-starfield-rain/ but it's cool. Yep, that's the site that inspired me. Mine is a different solution to the same problem, but the raindrops look similar, that's true. If you look closely, his are leaving trails behind - mine isn't, it's using an entire raindrop with a tail that 'peters out.' Luke noticed the dirty rectangle code wasn't working quite correctly, so I've modified the code and reposted it at http://incarnation.danbo.com/ubbthreads/modifypost.php . I also added some features, such as: * Speeding up big raindrops so they appear closer * Changing the translucency of raindrops so smaller ones are dimmer and look farther away * Optimized the particle engine so the raindrops are created once when the generator is created (as opposed to being continually created and destroyed) * Put the drops on a timer so they move at the desired speed regardless of how often you call the rain generator * Made the left and right arrow keys decrease/increase the rain speed. plus miscellaneous enhancements. Now just add little splashes at the bottom, a rain sound effect, and randomly change the background to white for a few frames (to simulate lightning) and you can sell it as a screensaver! I'm excited :D
Re: [pygame] Rain Generator
Luke Paireepinart wrote: Now just add little splashes at the bottom, a rain sound effect, and randomly change the background to white for a few frames (to simulate lightning) and you can sell it as a screensaver! I'm excited :D Glad you find it entertaining! :-D Uh, BTW, that URL was incorrect - the new version is at http://incarnation.danbo.com/ubbthreads/showflat.php?Cat=0Number=10483Main=10483#Post10483
Re: [pygame] Rain Generator
On 10/27/06, Farai Aschwanden [EMAIL PROTECTED] wrote: Erm... now you mention it... Shame on me. I really work with WingIDEand never came to the conclusion this could be the lag source. :o Ihave to try out.. tnx!!!Farai, I came on this board and complained about a lag every 3 seconds just like you did. I'd only written programs like database access stuff before I started using pygame, and of course you'd never notice a little lag when your doing stuff like that.
Re: [pygame] Rain Generator
Agree, its only visible in games/demos that requires permanent graphic refreshing. In my case WingIDE could cause that problem. I have to try it out once from shell, "just" have to define the python path. :/Glad, Im not the only one. ;)Am 28.10.2006 um 02:16 schrieb Ryan Charpentier:On 10/27/06, Farai Aschwanden [EMAIL PROTECTED] wrote: Erm... now you mention it... Shame on me. I really work with WingIDEand never came to the conclusion this could be the lag source. :o Ihave to try out.. tnx!!!Farai, I came on this board and complained about a lag every 3 seconds just like you did. I'd only written programs like database access stuff before I started using pygame, and of course you'd never notice a little lag when your doing stuff like that.
Re: [pygame] Rain Generator
Just use the Execute Current File option in the debug menu.
Re: [pygame] Rain Generator
Nice looking rain and proper code! I experienced a tiny lag like every 3 seconds. I experienced that also in other games/demos, couldnt find out why. Did other ppl also experienced this and/or knew why or is it only on my Mac? Greetings Farai Am 26.10.2006 um 20:40 schrieb Kamilche: I was inspired to create a rain generator after seeing someone else's on the Internet today, and thought I'd post it here: [code] import pygame import random import time SCREENSIZE = 640, 480 class Rain(object): ' Rain generator' drops = [] height = 160 speed = 1 color = (255, 255, 255, 255) chance = .05 def __init__(self, **kwargs): ' Allow programmer to change settings of rain generator' self.__dict__.update(kwargs) def Render(self, screen): ' Render the rain' dirtyrects = [] for drop in self.drops: drop.Render(dirtyrects, screen) if drop.dead: self.drops.remove(drop) else: dirtyrects.append(drop.rect) if random.random() self.chance: self.drops.append(Rain.Drop(self.height, self.speed, self.color)) return dirtyrects class Drop(object): ' Rain drop used by rain generator' pos = None dead = 0 def __init__(self, height, speed, color): ' Initialize the rain drop' w, h = 3, int((random.randint(80, 120) * height) / 100.0) self.pic = pygame.Surface((w, h), pygame.SRCALPHA, 32).convert_alpha() self.height = self.pic.get_height() self.maxy = SCREENSIZE[1] + h self.speed = 1 self.pos = [random.random() * SCREENSIZE[0], -self.height] factor = float(color[3])/h r, g, b = color[:3] for i in range(h): self.pic.fill( (r, g, b, int(factor * i)), (1, i, w-2, 1) ) pygame.draw.circle(self.pic, (255, 255, 255), (1, h-2), 2) self.rect = pygame.Rect(self.pos[0], self.pos[1], self.pic.get_width(), self.pic.get_height()) def Render(self, dirtyrects, screen): ' Draw the rain drop' self.pos[1] += self.speed self.rect.topleft = self.pos self.speed += .2 if self.pos[1] self.maxy: self.dead = 1 else: screen.blit(self.pic, self.pos) def main(): # Initialize pygame pygame.init() screen = pygame.display.set_mode(SCREENSIZE, 0, 32) # Create rain generator rain = Rain() # Main loop nexttime = time.time() ctr = 0 quit = 0 while not quit: # Uncomment the following line to make the rain go slower #time.sleep(.01) # Track FPS if time.time() nexttime: nexttime = time.time() + 1 print '%d fps' % ctr ctr = 0 ctr += 1 # Draw rain dirtyrects = rain.Render(screen) # Update the screen for the dirty rectangles only pygame.display.update(dirtyrects) # Fill the background with the dirty rectangles only for r in dirtyrects: screen.fill((0, 0, 0), r) # Look for user quit pygame.event.pump() for e in pygame.event.get(): if e.type in [pygame.QUIT, pygame.KEYDOWN, pygame.MOUSEBUTTONDOWN]: quit = 1 break # Terminate pygame pygame.quit() if __name__ == __main__: main() [/code] If the spaces have been eaten, I also posted it at http://incarnation.danbo.com/ubbthreads/showflat.php? Cat=0Number=10483an=0page=0#Post10483
Re: [pygame] Rain Generator
Farai Aschwanden wrote: Nice looking rain and proper code! I experienced a tiny lag like every 3 seconds. I experienced that also in other games/demos, couldnt find out why. Did other ppl also experienced this and/or knew why or is it only on my Mac? Greetings Farai Thanks! It doesn't lag on my Windows 2000 box, but I believe ya. Try commenting out the line that prints the FPS - I know printing can slow things down.
Re: [pygame] Rain Generator
No lag here, although I'm on XP. I'm getting like 1200 FPS though, so if there were such a periodic slowdown I probably wouldn't notice. -Jasper Farai Aschwanden wrote: Nice looking rain and proper code! I experienced a tiny lag like every 3 seconds. I experienced that also in other games/demos, couldnt find out why. Did other ppl also experienced this and/or knew why or is it only on my Mac? Greetings Farai Am 26.10.2006 um 20:40 schrieb Kamilche: [rain code snipped]
Re: [pygame] Rain Generator
Good point! I got around 200-300 fps and I still realise the lag, tough its a bit less w/o fps output. Guess its better to test such games/demos on a faster machine. 1200 FPS... *sigh*. Am 26.10.2006 um 21:24 schrieb Kamilche: Farai Aschwanden wrote: Nice looking rain and proper code! I experienced a tiny lag like every 3 seconds. I experienced that also in other games/demos, couldnt find out why. Did other ppl also experienced this and/or knew why or is it only on my Mac? Greetings Farai Thanks! It doesn't lag on my Windows 2000 box, but I believe ya. Try commenting out the line that prints the FPS - I know printing can slow things down.
Re: [pygame] Rain Generator
I didnt had a doubt on the code when I saw the lag first time, cause I already saw it in other games. Its no biggy at all and gives me a good feeling that my whenever coming game is well optimized. ;) I still got that lag uncommenting the line. I already checked the background activities and there no jumping up performance consumpting task. What I didnt try this time is to start the demo outside WingIDE (I already did that on other games earlier with same type of lag). All over no biggy and thanks for all answers Farai . Am 26.10.2006 um 21:56 schrieb Brian Fisher: what you describe sounds like what happens when a game hogs 100% of the CPU, but some background process needs to periodically do something (like say every 3 seconds) Looking at the code, it doesn't have anything in it that will yield the CPU. The solution is usually to sleep if you are getting a faster frame rate than you need. As a quick hack test/fix, you could try uncommenting the sleep(.01) line, I would bet that you don't get the periodic lag anymore if you do that On 10/26/06, Farai Aschwanden [EMAIL PROTECTED] wrote: Nice looking rain and proper code! I experienced a tiny lag like every 3 seconds. I experienced that also in other games/demos, couldnt find out why. Did other ppl also experienced this and/or knew why or is it only on my Mac? Greetings Farai Am 26.10.2006 um 20:40 schrieb Kamilche: I was inspired to create a rain generator after seeing someone else's on the Internet today, and thought I'd post it here: [code] import pygame import random import time SCREENSIZE = 640, 480 class Rain(object): ' Rain generator' drops = [] height = 160 speed = 1 color = (255, 255, 255, 255) chance = .05 def __init__(self, **kwargs): ' Allow programmer to change settings of rain generator' self.__dict__.update(kwargs) def Render(self, screen): ' Render the rain' dirtyrects = [] for drop in self.drops: drop.Render(dirtyrects, screen) if drop.dead: self.drops.remove(drop) else: dirtyrects.append(drop.rect) if random.random() self.chance: self.drops.append(Rain.Drop(self.height, self.speed, self.color)) return dirtyrects class Drop(object): ' Rain drop used by rain generator' pos = None dead = 0 def __init__(self, height, speed, color): ' Initialize the rain drop' w, h = 3, int((random.randint(80, 120) * height) / 100.0) self.pic = pygame.Surface((w, h), pygame.SRCALPHA, 32).convert_alpha() self.height = self.pic.get_height() self.maxy = SCREENSIZE[1] + h self.speed = 1 self.pos = [random.random() * SCREENSIZE[0], - self.height] factor = float(color[3])/h r, g, b = color[:3] for i in range(h): self.pic.fill( (r, g, b, int(factor * i)), (1, i, w-2, 1) ) pygame.draw.circle(self.pic, (255, 255, 255), (1, h-2), 2) self.rect = pygame.Rect(self.pos[0], self.pos[1], self.pic.get_width(), self.pic.get_height()) def Render(self, dirtyrects, screen): ' Draw the rain drop' self.pos[1] += self.speed self.rect.topleft = self.pos self.speed += .2 if self.pos[1] self.maxy: self.dead = 1 else: screen.blit(self.pic, self.pos) def main(): # Initialize pygame pygame.init() screen = pygame.display.set_mode(SCREENSIZE, 0, 32) # Create rain generator rain = Rain() # Main loop nexttime = time.time() ctr = 0 quit = 0 while not quit: # Uncomment the following line to make the rain go slower #time.sleep(.01) # Track FPS if time.time() nexttime: nexttime = time.time() + 1 print '%d fps' % ctr ctr = 0 ctr += 1 # Draw rain dirtyrects = rain.Render(screen) # Update the screen for the dirty rectangles only pygame.display.update(dirtyrects) # Fill the background with the dirty rectangles only for r in dirtyrects: screen.fill((0, 0, 0), r) # Look for user quit pygame.event.pump() for e in pygame.event.get(): if e.type in [pygame.QUIT, pygame.KEYDOWN, pygame.MOUSEBUTTONDOWN]: quit = 1 break # Terminate pygame pygame.quit() if __name__ == __main__: main() [/code] If the spaces have been eaten, I also posted it at http://incarnation.danbo.com/ubbthreads/showflat.php? Cat=0Number=10483an=0page=0#Post10483
Re: [pygame] Rain Generator
Farai said: Good point! I got around 200-300 fps and I still realise the lag, tough its a bit less w/o fps output. Guess its better to test such games/demos on a faster machine. 1200 FPS... *sigh*. Hmm, I'm getting upwards of 2500 fps, I was wondering if that means the rain was dropping faster than it would if the FPS were lower, or if the speed of the rain is based upon real time? I did a quick glance through the code and I can't see where the rain is being moved. I'm guessing that since uncommenting time.sleep() slows the rain down, the speed's based on iterations through the loop, and therefore FPS, right? Also, I was wondering if the dirty rects were supposed to be filled with non-black? After the program runs a while, I can see the raindrops leaving slightly-transparent stuff behind. I assume this is what you wanted to happen, but wanted to make sure. Nice-looking rain by the way.
Re: [pygame] Rain Generator
DANG! 2.5k fps, guys you really let me stand in the rain. :) Am 26.10.2006 um 22:38 schrieb Luke Paireepinart: Farai said: Good point! I got around 200-300 fps and I still realise the lag, tough its a bit less w/o fps output. Guess its better to test such games/demos on a faster machine. 1200 FPS... *sigh*. Hmm, I'm getting upwards of 2500 fps, I was wondering if that means the rain was dropping faster than it would if the FPS were lower, or if the speed of the rain is based upon real time? I did a quick glance through the code and I can't see where the rain is being moved. I'm guessing that since uncommenting time.sleep() slows the rain down, the speed's based on iterations through the loop, and therefore FPS, right? Also, I was wondering if the dirty rects were supposed to be filled with non-black? After the program runs a while, I can see the raindrops leaving slightly-transparent stuff behind. I assume this is what you wanted to happen, but wanted to make sure. Nice-looking rain by the way.
Re: [pygame] Rain Generator
Very nice effect! While people are posting effects, I'll whip out my old snow and rain tests.Here is the snow - When I first coded it it would eventually form a triangle, now it bunches up on the left side of the screen. I never figured out where the error was (but I didn't spend more than an hour on it either). Rain - I used a motion blur technique which I thought looks pretty cool. The physics of the rain is the best part. With the mouse you can move around a block and watch the rain bounce off. If the rain touches the red block on the bottom of the screen you lose (and have to exit by pressing escape), so you have to hold the white umbrella to keep him dry, lol.
Re: [pygame] Rain Generator
I always forget to attach files - here are the rain and snow I just talked about :P import random import pygame import psyco psyco.full() screen = pygame.display.set_mode([320,240]) colmap = [[0 for x in range(320)] for y in range(240)] col_width = len(colmap[0]) col_height = len(colmap) lowestflake = [col_height] windtunnel = [0,60,320,10] windpower = [0,0] buggy = [10,10,10,5] def p_in_c(point): Returns 1 if that coordinate is in the coordinate map return point[1]col_height and point[1]=0 and point[0]col_width and point[0]=0 def getc(point): Returns 1 if that point is out of bounds or if that point contains a snowflake if p_in_c(point): if point[0]=buggy[0] and point[0]=buggy[0]+buggy[2] and point[1]=buggy[1] and point[1]=buggy[1]+buggy[3]: return 1 return colmap[point[1]][point[0]] if point[1]col_height: if point[1]lowestflake[0]: return 1 return 0 return 1 def setc(point,val): if p_in_c(point): if point[1]lowestflake[0]: lowestflake[0] = point[1] colmap[point[1]][point[0]] = val img_flake = pygame.Surface([1,1]) img_flake.fill([255,255,255]) grav = 9.8/60.0 wind = [-20.0/60.0,-1.0/60.0] class SnowFlake: def __init__(self,pos): self.lastpos = pos[:] self.pos = pos[:] #setc([int(x) for x in self.pos],1) self.vel = [0,0.2] self.lastsurf = pygame.Surface([1,1]) self.deadtime = 3 self.rolltime = 10 def move(self): force = [0,grav] if self.pos[0]=windtunnel[0] and self.pos[0]=windtunnel[0]+windtunnel[2] and self.pos[1]=windtunnel[1] and self.pos[1]=windtunnel[1]+windtunnel[3]: force[0]+=windpower[0] force[1]+=windpower[1] self.lastpos = self.pos[:] self.pos[0]+=self.vel[0]+0.2*random.choice([-1,1])+wind[0] self.pos[1]+=self.vel[1]/20.0+wind[1] self.vel[0]+=force[0] self.vel[1]+=force[1] lp = [int(x) for x in self.lastpos] #last pixel np = [int(x) for x in self.pos] #Now pixel dx = np[0]-lp[0] #x change in pixels dy = np[1]-lp[1] #y change in pixels if dx: dx = dx/abs(dx) #Get direction of change x if dy: dy = dy/abs(dy) #Get directionof change y #Draw line from last pixel to current pixel, find walls lx,ly = lp[0],lp[1] #lastx pixel and lasty pixel x,y=lp[0],lp[1] p=None #p is a spot where we were blocked while x!=np[0] or y!=np[1]: lx,ly=x,y if x!=np[0]: x+=dx if y!=np[1]: y+=dy if getc([x,y]): p = [lx,ly] #blocked break x,y = np[0],np[1] if getc([x,y]) and not p: p = [lx,ly] if p: x,y = p left = getc([x-1,y+1]) right = getc([x+1,y+1]) down = getc([x,y+1]) canroll = self.rolltime0 if not down: self.pos = [x,y+1] elif left and not right: self.pos = [x+1,y+1] elif not left and right: self.pos = [x-1,y+1] elif not left and not right: self.pos = [random.choice([x-1,x+1]),y] else: self.pos = [x,y] np = [int(x) for x in self.pos] self.deadtime -= 1 setc(np,1) #if self.pos[0]0: #print x out of bounds #raise Error #if self.pos[1]240: #print oops?,self.pos[1],p,lp,np #raise Error def draw(self): np = [int(x) for x in self.pos] self.lastsurf.blit(screen,np) screen.blit(img_flake,np) if self.lastpos != self.pos: screen.blit(self.lastsurf,[int(x) for x in self.lastpos]) flakes = [] numflakes = 500 def addflake(): y = random.randint(-500,10) x = random.randint(0,420) f = SnowFlake([x,y]) f.vel[0] = random.choice([-1,1])*0.05 f.vel[1] = (y+1000)*.02 flakes.append(f) [addflake() for i in range(numflakes)] pygame.event.clear() pygame.event.pump() c = pygame.time.Clock() pygame.draw.rect(screen,[255,0,0],[10,10,10,5]) while 1: c.tick(60) flakes = [f for f in flakes if f.deadtime0] [f.draw() for f in flakes] pygame.display.update() [f.move() for f in flakes] pygame.event.pump() if pygame.event.get(pygame.KEYDOWN): break [addflake() for x in xrange(numflakes-len(flakes))] #init stuff import pygame import pygame.locals as l import random screen = pygame.display.set_mode([640,480]) class drop: def __init__(self,posx,posy): self.pos = [posx,posy] def __getattr__(self,attr): if attr==x: return self.pos[0] if attr==y: return self.pos[1] return drop.__get__(self,attr) def __getitem__(self,it): return self.pos[it] class splash(drop): def
Re: [pygame] Rain Generator
Could only check the rain.py that is pretty to look at. The ground touch of the rain is really well done! Crashes also on my machine after about 5 seconds. Unfortunately I cant start snow.py, Im not psyco enough. ;) Should try it out once, tough they say its not stable yet. Am 27.10.2006 um 00:51 schrieb Patrick Mullen: I always forget to attach files - here are the rain and snow I just talked about :P snow.py rain.py
Re: [pygame] Rain Generator
Farai Aschwanden wrote: Could only check the rain.py that is pretty to look at. The ground touch of the rain is really well done! Crashes also on my machine after about 5 seconds. Unfortunately I cant start snow.py, Im not psyco enough. ;) Should try it out once, tough they say its not stable yet. Yeah, snow.py is pretty as well! Psyco is really easy to turn off - just comment out the first two lines, and you should be able to run the second demo. On the first example - it didn't crash for me, but it stopped responding at 5 seconds, because I lost the game! The object of this mini game is to keep that box at the bottom covered and not let any rain touch it. Really cute! --Kamilche
Re: [pygame] Rain Generator
Farai Aschwanden wrote: I experienced a tiny lag like every 3 seconds. There's a chance it's due to Python's cyclic garbage collector. You could try disabling that and see if the pauses go away. (If it does, you'd better then check whether you're gobbling up increasing amounts of memory and take steps to break cycles if necessary.) -- Greg
Re: [pygame] Rain Generator
I have this exact problem with lag when I run a pygame in a debugger like Wing IDE.
Re: [pygame] Rain Generator
Looks similar to http://www.scriptedfun.com/pygame-starfield-rain/ but it's cool. Greetings Kai