#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Anyone can help me with this code?


    Greetings, I am new to Python, which I am finding a great programming language. I just copied a Python Space Invaders clone program from an article on the Net. I can't post the link for forum rules.

    Problem is, when I run the module, the program freezes and nothing happens (both in Windows and OSX.)

    Do you think that I am missing something? Any help, suggestions? Many thanks.

    Here is the code:

    Code:
    from pygame import *
    import random
    
    class Sprite:
        def __init__(self, xpos, ypos, filename):
            self.x = xpos
            self.y = ypos
            self.bitmap = image.load(filename)
            self.bitmap.set_colorkey((0,0,0))
        def set_position(self, xpos, ypos):
            self.x = xpos
            self.y = ypos
        def render(self):
            screen.blit(self.bitmap, (self.x, self.y))
    
        def Intersect(s1_x, s1_y, s2_x, s2_y):
            if (s1_x > s2_x - 32) and (s1_x < s2_x + 32) and (s1_y > s2_y - 32) and (s1_y < s2_y + 32):
                return 1
            else:
                return 0
    
    init()
    screen = display.set_mode((640, 480))
    key.set_repeat(1, 1)
    display.set_caption('PyInvaders')
    backdrop = image.load('data/backdrop.bmp')
    
    enemies = []
    
    x = 0
    
    for count in range(10):
        enemies.append(Sprite(50 * x + 50, 50, 'data/baddie.bmp'))
        x += 1
    
    hero = Sprite(20, 400, 'data/hero.bmp')
    ourmissile = Sprite(0, 480, 'data/heromissile.bmp')
    enemymissile = Sprite(0, 480, 'data/baddiemissile.bmp')
    
    quit = 0
    enemyspeed = 3
    
    while quit == 0:
        screen.blit(backdrop, (0, 0))
    
        for count in range(len(enemies)):
            enemies[count].x += enemyspeed
            enemies[count].render()
    
        if enemies[len(enemies)-1].x > 590:
            enemyspeed = -3
            for count in range(len(enemies)):
                enemies[count].y += 5
    
        if enemies[0].x < 10:
            enemyspeed = 3
            for count in range(len(enemies)):
                enemies[count].y += 5
    
        
    if ourmissile.y < 479 and ourmissile.y > 0:
        ourmissile.render()
        ourmissile.y += -5
    
    if enemymissile.y >= 480 and len(enemies) > 0:
        enemymissile.x = enemies[random.randint(0, len(enemies) -1)].x
        enemymissile.y = enemies[0].y
    
    if Intersect(hero.x, hero.y, enemymissile.x, enemymissile.y):
        quit = 1
    
    for count in range(0, len(enemies)):
        if Intersect(ourmissile.x, ourmissile.y, enemies[count].x, enemies[count].y):
            del enemies[count]
            break
    
    
    if len(enemies) == 0:
        quit = 1
    
    for ourevent in event.get():
        if ourevent.type == QUIT:
            quit = 1
        if ourevent.type == KEYDOWN:
            if ourevent.key == K_RIGHT and hero.x < 590:
                hero.x += 5
            if ourevent.key == K_LEFT and hero.x > 10:
                hero.x -= 5
            if ourevent.key == K_SPACE:
                ourmissile.x = hero.x
                ourmissile.y = hero.y
    
    enemymissile.render()
    enemymissile.y += 5
    
    hero.render()
    
    display.update()
    time.delay(5)
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,897
    Rep Power
    481
    Please post python code according to instructions at my signature. Have you installed pygame? Do you have the various bitmap files?
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Please post python code according to instructions at my signature. Have you installed pygame? Do you have the various bitmap files?
    I just edited my code with the [code] tag as required.

    Yes I have Pygame installed both on Mac and PC, and running the program is not giving me any error of the type "pygame not found".

    For some reason I can use Pygame only with Python 2.x however, it doesn't work here with Python 3.x.

    And yes I have the bitmaps from the code in the same directory of the Python code.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,897
    Rep Power
    481
    I substituted bitmaps on Ubuntu linux distribution with result that the program behaves exactly as you described.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I substituted bitmaps on Ubuntu linux distribution with result that the program behaves exactly as you described.
    So is there something wrong with this code? What? You can find the source article with a Google search for Code Project: Build a Space Invaders clone Python.

    Thank you.

IMN logo majestic logo threadwatch logo seochat tools logo