4

I have created a simple space invaders game with python, however, I would like to create a menu and more levels but I do not know how to go about doing this. Could anybody please show me how to create a menu level and other additional levels?

from pygame import *
import random

init()
screen = display.set_mode((640, 480))
key.set_repeat(1, 1)
display.set_caption("Py-Invaders")
background = image.load("data/background.bmp")

class Sprite:
    def __init__ (self, xposition, yposition, filename):
        self.x = xposition
        self.y = yposition
        self.bitmap = image.load(filename)
        self.bitmap.set_colorkey((0, 0, 0))
    def set_position (self, xposition, yposition):
        self.x = xposition
        self.y = yposition
    def render (self):
        screen.blit(self.bitmap, (self.x, self.y))
    def setImage (self, filename):
        self.bitmap = image.load(filename)
    def removeColor (self, colorkey):
        self.bitmap.set_colorkey(colorkey)

def Intersect(x1, y1, x2, y2):
    if (x1 > x2 - 32) and (x1 < x2 + 32) and (y1 > y2 - 32) and (y1 < y2 + 32):
        return (1)
    else:
        return (0)
def rocketIntersect(x1, y1, x2, y2):
    if (x1 > x2 - 32) and (x1 < x2 + 143) and (y1 > y2 - 32) and (y1 < y2 + 143):
        return (1)
    else:
        return (0)

#Creating invaders1
invaders = []

x = 0
for i in range (10):
    invaders.append(Sprite(50 * x + 50, 50, "data/invader.bmp"))
    x += 1

#Creating invaders2
invaders2 = []

y = 0
for i in range (10):
    invaders2.append(Sprite(50 * y + 50, 100, "data/invader.bmp"))
    y += 1

ship = Sprite(20, 400, "data/ship/shipup.bmp")
shiplaser = Sprite(0, 480, "data/lasers/shiplaser.bmp")
shiprocket = Sprite(0, 480, "data/shiprocket.bmp")
invaderlaser = Sprite(0, 480, "data/lasers/invaderlaser.bmp")
invaderlaser2 = Sprite(0, 480, "data/lasers/invaderlaser.bmp")

quit = 0
invaderspeed = 3
invaderspeed2 = 3

while quit == 0: #Main game while loop
    screen.blit(background, (0, 0))

#Moving the invaders
    for i in range(len(invaders)):
        invaders[i].x += + invaderspeed
        invaders[i].render()

    if invaders[len(invaders)-1].x > 590:
        invaderspeed = -3
        for i in range(len(invaders)):
            invaders[i].y += 5

    if invaders[0].x < 10:
        invaderspeed = 3
        for i in range(len(invaders)):
            invaders[i].y += 5

#Moving the invaders2
    for i in range(len(invaders2)):
        invaders2[i].x += + invaderspeed2
        invaders2[i].render()

    if invaders2[len(invaders2)-1].x > 590:
        invaderspeed2 = -3
        for i in range(len(invaders2)):
            invaders2[i].y += 5

    if invaders2[0].x < 10:
        invaderspeed2 = 3
        for i in range(len(invaders2)):
            invaders2[i].y += 5

#Moving the ship laser
    if shiplaser.y < 479 and shiplaser.y > 0:
        shiplaser.render()
        shiplaser.y -= 5

#Moving the ship rocket
    if shiprocket.y < 479 and shiprocket.y > -150:
        shiprocket.render()
        shiprocket.y -= 5

#Moving the invaders laser
    if invaderlaser.y >= 480 and len(invaders) > 0:
        invaderlaser.x = invaders[random.randint(0, len(invaders) - 1)].x
        invaderlaser.y = invaders[0].y

#Moving the invaders2 laser
    if invaderlaser2.y >= 480 and len(invaders2) > 0:
        invaderlaser2.x = invaders2[random.randint(0, len(invaders2) - 1)].x
        invaderlaser2.y = invaders2[0].y

#Ship collisions with invaders laser
    if Intersect(ship.x, ship.y, invaderlaser.x, invaderlaser.y):
        quit = 1

#Ship collisions with invaders2 laser
    if Intersect(ship.x, ship.y, invaderlaser2.x, invaderlaser2.y):
        quit = 1

#Invaders collision with ship laser
    for i in range(0, len(invaders)):
        if Intersect(shiplaser.x, shiplaser.y, invaders[i].x, invaders[i].y):
            del invaders[i]
            shiplaser.x  = -1
            shiplaser.y = -1
            break

#Invaders2 collision with ship laser
    for i in range(0, len(invaders2)):
        if Intersect(shiplaser.x, shiplaser.y, invaders2[i].x, invaders2[i].y):
            del invaders2[i]
            shiplaser.x = -1
            shiplaser.y = -1
            break

#Invaders collision with ship rocket
    for i in range(0, len(invaders)):
        if Intersect(shiprocket.x, shiprocket.y, invaders[i].x, invaders[i].y):
            del invaders[i]
            break

#Invaders2 collision with ship rocket
    for i in range(0, len(invaders2)):
        if Intersect(shiprocket.x, shiprocket.y, invaders2[i].x, invaders2[i].y):
            del invaders2[i]
            break

    if len(invaders) and len(invaders2) == 0:
        quit = 1

    for ourevent in event.get():
        if ourevent.type == QUIT:
            quit = 1
        if ourevent.type == KEYDOWN:
            if ourevent.key == K_RIGHT and ship.x < 590:
                ship.x += 5
                ship.setImage("data/ship/shipright.bmp")
                ship.removeColor((0, 0, 0))

            if ourevent.key == K_LEFT and ship.x > 10:
                ship.x -= 5
                ship.setImage("data/ship/shipleft.bmp")
                ship.removeColor((0, 0, 0))

            if ourevent.key == K_UP and ship.y > 10:
                ship.y -= 5
                ship.setImage("data/ship/shipup.bmp")
                ship.removeColor((0, 0, 0))

            if ourevent.key == K_DOWN and ship.y < 450:
                ship.y += 5
                ship.setImage("data/ship/shipdown.bmp")
                ship.removeColor((0, 0, 0))

            if ourevent.key == K_SPACE:
                shiplaser.x = ship.x
                shiplaser.y = ship.y

            if ourevent.key == K_z:
                shiprocket.x = ship.x
                shiprocket.y = ship.y

            if ourevent.key == K_p:
                time.delay(10000)

    invaderlaser.render()
    invaderlaser.y += 5

    invaderlaser2.render()
    invaderlaser2.y += 5

    ship.render()

    display.update()
    time.delay(5)
Kara
  • 6,115
  • 16
  • 50
  • 57
ZON
  • 49
  • 2
  • This is a bit too general, what specifically are you having trouble with? We would rather have a concrete and solvable question rather than 'what should I do?' – TankorSmash Apr 28 '14 at 21:58
  • Okay, so pretty much I have no clue on how to create a menu. I want to create a start up page that says "Press space to start game" and then once you press space it takes you to the first level, the code above. Also, I want to add in a second level, so once all the invaders have been killed on the first level, the code above, it takes me to another. I am having trouble with finding a way to create and these levels and move between levels. Thank you for your help. – ZON Apr 28 '14 at 22:03
  • 1
    A small note about your game in general: consider loading each image from disk to memory only once, and reusing it; it'll save a lot of loading time and memory usage. One of many ways would be `shipdownPic = image.load("data/ship/shipup.bmp")` and then `ship.setImage(shipdownPic)`. – SimonT Apr 28 '14 at 23:15
  • possible duplicate of [Pygame level/menu states](http://stackoverflow.com/questions/14700889/pygame-level-menu-states) – sloth Apr 29 '14 at 07:57

2 Answers2

1

Given your code, I suppose you are a begginer programmer - since you used no code structures like functions (nor anything about Object Orientation, but that is not really needed). So, now you have to formalize a little bit more what you have learned: look for a Python tutorial and learn about functions. Take some practice until you understand them. Then you "refactor" your code, so that the whole, existing level, exists inside a function (which should call smaller functions, not be in a big block like this).

Then that is done, you will create your menu, with a complete different logic, possibly using some other helper project which creates menus using Pygame (it has no facilities to create menus by itself, and it could be more complex than your whole game). Then a "main function" will drive your game, either calling the menu to be displayed, or a level to be played. This main function may pass parameters to your "level" function (basically your whole current game) that will make the same code behave differently for each level. (for example, number of enemy ships, or speed are nice parameters to tell one level from the other)

jsbueno
  • 99,910
  • 10
  • 151
  • 209
0

Read up on finite-state machines, then turn your main loop into one.

kitti
  • 14,663
  • 31
  • 49