Re: [pygame] Why does my ball vibrate?
Douglas Bagnall wrote: FLOOR = 384 if ypos = FLOOR and velocity 0: overstep = ypos - FLOOR ypos = FLOOR - overstep * bounce velocity = -velocity * bounce I tried using this code and I go back to the situation where the ball continues to bounce when it should have come to rest. I would imagine this is because each time the ball passes below the floor we are giving it a little bit of upwards velocity.
[pygame] Bouncing ball - separating the physics from the frame rate
Hi, I know have the following code for my bouncing ball program: #! /usr/bin/python import sys, pygame, math pygame.init() xpos = 92 ypos = 0 gravity = 9.8 velocity = 0 # How much of the velocity of the ball is retained on a bounce bounce = 0.8 screen = pygame.display.set_mode((200, 400), 0, 32) # The ball is a 16px sprite ball = pygame.image.load('ball.png') clock = pygame.time.Clock() # The main loop while True: # Test for exit for event in pygame.event.get(): if event.type == pygame.QUIT: exit() # The physics # Reverse velocity taking into account bounciness if we hit the ground if ypos = 384 and velocity 0: # Avoid the ball sinking into the ground ypos = 384 velocity = -velocity * bounce time_passed = clock.tick(60) / 1000.0 newvelocity = velocity + (gravity * time_passed) # Use the average velocity over the period of the frame to change position ypos = ypos + int(((velocity + newvelocity) / 2) * time_passed * 160) velocity = newvelocity # Update the screen screen.fill((0, 0, 0)) screen.blit(ball, (xpos, ypos)) pygame.display.update() The bounce is pretty realistic and I can live with the slight error caused by returning the ball to the floor exactly each time it bounces. Next I am going to have a look at adding in some sideways movement and bouncing off walls as well. Before I do that I would like to separate the physics calculations from the frame rate calculations and drawing loop. I really don't have a clue how to do this. I have never programmed anything in real time before. Having had a think about this I can see two possible ways of doing it. 1. Have the physics and graphics running in separate threads. I don't have a clue how to implement this! 2. Call a function from the main loop every time it passes which does the physics calculations based on a clock independent of the frame rate but which pauses when the simulation is paused (window not in focus?) Is there a function in the sys module that I can use to test for a pause? Can anyone give me some more pointers on the best and most pythonic way of doing this. Thanks. Matt
[pygame] Ludum Dare X - Dec 14-16 - Update
Hey, Ludum Dare 10 is coming! Ludum Dare is a regular community driven game development competition. The goal is, given a theme and 48 hours, to develop a game from scratch. Ludum Dare aims to encourage game design experimentation, and provide a platform to develop and practice rapid game prototyping. Website: http://www.imitationpickles.org/ludum/ Rules Stuff: http://www.imitationpickles.org/ludum/wiki/start IRC: #ludumdare on irc.afternet.org Dec. 7-14th - Registration and Theme Voting Dec. 14 at 7pm PST and runs until the 16th at 7pm PST We're also building an archive of entries from previous compos. Goto the compo site and add your old entries! They will be added to the screenshot grids: Overall: http://www.imitationpickles.org/ludum/?mythumb_nav=1 Per Author: http://www.imitationpickles.org/ludum/author/hamumu/ Per Compo: http://www.imitationpickles.org/ludum/category/ld8/ Per Tag: http://www.imitationpickles.org/ludum/tag/farm/ Lastly, if you have participated in previous LD's and have a game-dev related blog, I'd be thrilled to aggregate it onto the site. Create an account and then contact me with your blog info and I will add it ASAP. Hope to see you there! -Phil - Never miss a thing. Make Yahoo your homepage.
[pygame] Problem accessing pygame.Rect() members
I wrote a custom pygame.sprite.RenderPlain() class. It modifies .draw() to include a source rect of the sprite. Now I need to modify the dest rect, converting its world() coordinates to screen() coordinates. I wrote: Map().coord_to_screen( world ) and Map().coord_to_world( screen ) and they are working. But I can't access the member spr.rect.left or spr.rect.top of the sprite's Rect() to modify it. I get an AttributeError. The classes I'm using in this sprite group are: class Unit(pygame.sprite.Sprite): Which derives .Sprite(), adding movement. It works fine until I try accessing the .rect in RenderSrcRect().draw(): to blit with global coordinates converted to screen coordinates. (1) The documentation says that these aren't members( .top, .left, etc...), but are virtual attributes. What is a virtual attribute? (2) What's wrong with my code? The first code snippet is my working class, that just uses spr.rect as the dest rect. The second code snippet is my failed attempts to modify to convert the .rect world() coords to screen() coords. # code: original class, before screen() coords. This class *does* work. # file: RenderSrcRect.py # Author: jake bolton [created: 2007/11/14] # About: RenderSrcRect # child of RenderPlain, adds source rect support import pygame class RenderSrcRect(pygame.sprite.RenderPlain): custom sprite group rendering class. Uses src Rect()s of sprite. the inheritance is: RenderPlain() == Group() which derives AbstractGroup() members used from the sprites to blit: .image = the sprite's surface .rect = the blit dest rect .rect_source = the blit source rect def __init__(self): initialize Unit() pygame.sprite.RenderPlain.__init__(self) def draw(self, surface): draw(surface). Draw all sprites onto the surface same as .RenderPlain.draw() except spr.rect_source is used to blit sprites = self.sprites() surface_blit = surface.blit for spr in sprites: # blit with src rect self.spritedict[spr] = surface_blit(spr.image, spr.rect, spr.rect_source) self.lostsprites = [] # modified class, uses screen() coords. This class has AttributeError: # the error: AttributeError: 'tuple' object has no attribute 'left' on line: spr.rect.left = 40 If you comment out the whole (try 1) block, (try 2) will still fail. # code: # file: RenderSrcRect.py # Author: jake bolton [created: 2007/11/14] # About: RenderSrcRect # child of RenderPlain, adds source rect support, and uses screen() coords from euclid import Vector2 import pygame from pygame.locals import * # needed for Rect() ? class RenderSrcRect(pygame.sprite.RenderPlain): custom sprite group rendering class. Uses src rects of sprite. the inheritance is: RenderPlain() == Group() which derives AbstractGroup() members used from the sprites to blit: .image = the sprite's surface .rect = the blit dest rect .rect_source = the blit source rect def __init__(self, game): initialize Unit() pygame.sprite.RenderPlain.__init__(self) # reference to the Map() class, for the later .coord_to_screen() call self.game = game self.map = self.game.map def draw(self, surface): draw(surface). Draw all sprites onto the surface same as .RenderPlain.draw() except spr.rect_source is used to blit sprites = self.sprites() surface_blit = surface.blit for spr in sprites: # use screen coords for dest rect # == (try 1): I want to do something like this: but it fails: == # note: self.map.coord_to_screen() returns a euclid.Vector2() dest_rect = pygame.Rect( self.map.coord_to_screen( spr.rect.topleft ).x, self.map.coord_to_screen( spr.rect.topleft ).y, spr.rect.x, spr.rect.y, 0, 0 ) # dest rect ignores w,h values # (try 2): try 1 didn't work, so I test just Rect() member access: fails spr.rect.left = 40 # this line gives an attribute error self.spritedict[spr] = surface_blit(spr.image, dest_rect, spr.rect_source)
[pygame] Help configuring Pygame on OS X Leopard
Hello there, I am a Python / Pygame newbie... Installed Pygame by installing the following files located at: http://pythonmac.org/packages/py25-fat/index.html Am running Python 2.5 on OS X Leopard... Copied the following code from Pygame online tutorial: #!/usr/local/bin/python import pygame from pygame.locals import * def main(): # Initialize screen pygame.init() screen = pygame.display.set_mode((400,100)) pygame.display.set_caption('Basic Pygame program') # Fill background background = pygame.Surface(screen.get_size()) background = background.convert() background.fill((250, 250, 250)) # Display some text font = pygame.font.Font(None, 36) text = font.render(Hello Unnsse!, 1, (10, 10, 10)) textpos = text.get_rect() textpos.centerx = background.get_rect().centerx # Blit everything to the screen screen.blit(background, (0, 0)) pygame.display.flip() # Event loop while 1: for event in pygame.event.get(): if event.type == QUIT: return screen.blit(background, (0, 0)) pygame.display.flip() if __name__ == '__main__': main() When I ran this program, I receive the following error message: Traceback (most recent call last): File /Users/untz/DevResources/Python/pygame_helloworld/hello.py, line 3, in module import pygame ImportError: No module named pygame What am I possibly doing wrong? Happy programming, Unnsse
Re: [pygame] Help configuring Pygame on OS X Leopard
On Dec 1, 2007 7:10 PM, Unnsse Khan [EMAIL PROTECTED] wrote: Installed Pygame by installing the following files located at: http://pythonmac.org/packages/py25-fat/index.html There isn't a pygame package for python 2.5 at that page... I am not aware of a prebuilt pygame for python 2.5 for Mac OSX (I use python 2.4 on mac for that reason) If you want to tackle building it, there are some instructions here: http://www.pygame.org/wiki/MacCompile?parent=index
Re: [pygame] Help configuring Pygame on OS X Leopard
On Dec 1, 2007 7:10 PM, Unnsse Khan [EMAIL PROTECTED] wrote: When I ran this program, I receive the following error message: Traceback (most recent call last): File /Users/untz/DevResources/Python/pygame_helloworld/hello.py, line 3, in module import pygame ImportError: No module named pygame That error means you don't have pygame installed.
Re: [pygame] Help configuring Pygame on OS X Leopard
On Dec 1, 2007, at 8:12 PM, Brian Fisher wrote: On Dec 1, 2007 7:10 PM, Unnsse Khan [EMAIL PROTECTED] wrote: Installed Pygame by installing the following files located at: http://pythonmac.org/packages/py25-fat/index.html There isn't a pygame package for python 2.5 at that page... I am not aware of a prebuilt pygame for python 2.5 for Mac OSX (I use python 2.4 on mac for that reason) If you want to tackle building it, there are some instructions here: http://www.pygame.org/wiki/MacCompile?parent=index You can use fink (http://finkproject.org/) to install pygame with python 2.5. It's also handy for installing other libraries you might want (PIL, pyrex, ode, py2app, etc.). hth, -Casey