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
了解一下有限状态机的概念,然后把你的主循环改成一个有限状态机。
1
根据你的代码,我猜你是个初学者,因为你没有使用像函数
这样的代码结构(也没有涉及面向对象的内容,不过那并不是必须的)。所以,现在你需要把你学到的东西整理得更清楚一些:找一个Python教程,学习一下函数。多练习,直到你理解它们为止。然后你可以“重构”你的代码,也就是说,把现有的代码放进一个函数里(而不是像现在这样放在一个大块里,应该调用更小的函数)。
完成这些后,你可以创建一个菜单,逻辑会完全不同,可能会用到一些其他的辅助项目,这些项目可以用Pygame来创建菜单(因为Pygame本身没有创建菜单的功能,这可能会比你的整个游戏还复杂)。然后一个“主函数”会控制你的游戏,要么调用菜单显示,要么调用某个关卡进行游戏。这个主函数可以给你的“关卡”函数(基本上就是你现在的整个游戏)传递参数,这样同样的代码在不同的关卡中会表现得不一样。(比如,敌舰的数量或速度都是很好的参数,可以用来区分不同的关卡)