[Tutor] Delay Between iterations

ANKUR AGGARWAL coolankur2006 at gmail.com
Thu Jan 12 18:31:18 CET 2012


import pygame
from pygame.locals import *
import random

pygame.init()
screen=pygame.display.set_mode((640,480),0,24)
pygame.display.set_caption("Hit The Stone")
background=pygame.Surface(screen.get_size())
background=background.convert()
screen.blit(background,(0,0))

class Plane(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load('plane.gif').convert()
        self.rect=self.image.get_rect()
        self.rect.centerx=random.randint(0,screen.get_width())
        self.distancefromcenter=30
        self.rect.centery=screen.get_height()-self.distancefromcenter
        self.dx=2
        self.dy=2

    def update(self):
        self.pressed=pygame.key.get_pressed()
        if self.pressed[K_DOWN]:
            self.rect.centery+=self.dy
        elif self.pressed[K_UP]:
            self.rect.centery-=self.dy
        elif self.pressed[K_LEFT]:
            self.rect.centerx-=self.dx
        elif self.pressed[K_RIGHT]:
            self.rect.centerx+=self.dx


        if self.rect.bottom>=screen.get_height():
            self.rect.bottom=screen.get_height()
        elif self.rect.top<=0:
            self.rect.top=0

        if self.rect.centerx>=screen.get_width()-self.distancefromcenter:
            self.rect.centerx=screen.get_width()-self.distancefromcenter
        elif self.rect.centerx<=self.distancefromcenter:
            self.rect.centerx=self.distancefromcenter




class Bullet(pygame.sprite.Sprite):
    def __init__(self,posx,posy,image):
        pygame.sprite.Sprite.__init__(self)
        if self.shootCount>10:
            self.image=image
            self.rect=self.image.get_rect()
            self.rect.center=(posx,posy-30)
            self.dy=5
        else:
            self.shootCount+=1


    def update(self):
        self.rect.centery-=self.dy
        self.rect.center=(self.rect.centerx,self.rect.centery)
        if self.rect.top<=0:
            self.kill()


def main():
    image=pygame.image.load('geometrybullet.png').convert()
    plane=Plane()
    allSprites=pygame.sprite.Group(plane)
    clock=pygame.time.Clock()

    while 1:
        pressed=pygame.key.get_pressed()
        for i in pygame.event.get():
            if i.type==QUIT or pressed[K_q]:
                exit()
*        if  pressed[K_SPACE]:*
*                bullet=Bullet(plane.rect.centerx,plane.rect.centery,image)*
*                bullet.shootCount=0*
*                allSprites.add(bullet)*


        allSprites.clear(screen,background)
        allSprites.update()
        allSprites.draw(screen)
        pygame.display.flip()


if __name__=='__main__':
    main()


I was trying to make shooting game. whenever I press the space key bullet
object is called (bold code) . I want delay between iterations to produce
the gap between the bullets.Tried the time.wait() but its working. Any
Ideas??

Thanks In Advance

Regards
Ankur Aggarwal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120112/6b73f6fc/attachment.html>


More information about the Tutor mailing list