Pygame级别/菜单创建

时间:2014-04-28 21:57:06

标签: python pygame

我用python创建了一个简单的太空入侵者游戏,但是,我想创建一个菜单和更多级别,但我不知道如何去做这个。有人可以告诉我如何创建菜单级别和其他附加级别?

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)

2 个答案:

答案 0 :(得分:0)

阅读finite-state machines,然后将主循环变成一个。

答案 1 :(得分:0)

鉴于你的代码,我认为你是一个初学者程序员 - 因为你没有使用像functions那样的代码结构(也没有任何关于面向对象的东西,但这并不是真的需要)。所以,现在你必须更多地了解你所学到的东西:寻找Python教程并了解函数。做一些练习直到你理解它们。然后你“重构”你的代码,这样整个现有的级别就存在于一个函数中(它应该调用较小的函数,而不是像这样的大块)。

然后就完成了,你将创建你的菜单,使用完全不同的逻辑,可能使用一些其他帮助项目,使用Pygame创建菜单(它没有设置自己创建菜单,它可能比你的更复杂整场比赛)。然后一个“主要功能”将驱动你的游戏,要么调用要显示的菜单,要么调用要播放的级别。这个主要功能可以将参数传递给你的“级别”功能(基本上是你当前的整个游戏),这将使相同的代码对每个级别的行为都不同。 (例如,敌舰的数量,或速度是很好的参数,以告诉一个级别的另一个级别)