我对python很新,我试图制作一个小型太空入侵者游戏。我的问题是我在屏幕上不能有一个子弹。我希望能够在原始游戏中至少射出3个。最好的方法是什么?
这是我的代码:
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 - 30) and (s1_x < s2_x + 30) and (s1_y > s2_y - 30) and (s1_y < s2_y + 30):
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
count = 0
for count in range(10):
if count < 10:
enemies.append(Sprite(50 * x + 50, 50, 'data/baddie.bmp'))
x += 1
count += 1
else:
x = 0
enemies.append(Sprite(100 * x + 100, 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')
gameover = image.load('data/gameover.bmp')
quit = 0
enemyspeed = 4
timer = 0
while quit == 0:
heromid = hero.x + 16
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 < 640 and ourmissile.y > 0:
ourmissile.render()
ourmissile.y += -5
timer = timer + 1
if enemymissile.y >= 480 and len(enemies) > 0:
enemymissile.x = enemies[random.randint(0, len(enemies) - 1)].x + 16
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:
if timer < 400:
timer = 0
ourmissile.x = heromid
ourmissile.y = hero.y
enemymissile.render()
enemymissile.y += 5
hero.render()
display.update()
time.delay(10)
while quit == 1:
screen.blit(gameover,(0,0))
display.update()
if ourevent.type == KEYDOWN:
if ourevent.key == K_SPACE:
quit == 0
答案 0 :(得分:1)
好吧,让我们坦率地说。你将不得不重新编写其中的大部分内容。这是我如何做到的一个例子:
我创建了一个精灵组(它拥有多个精灵)
bulletGroup = pygame.sprite.Group()
然后我为子弹做了一个课
# The pygame.sprite.Sprite is necessary
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
self.x = x
self.y = y
# What this line means is that whenever a bullet is created it goes in the group "bulletGroup"
pygame.sprite.Sprite.__init__(self, bulletGroup)
现在要创建一个子弹:
# You can execute this same line as many times as you want and the bullets will be seperate
bullet = Bullet(x, y)
如果您需要帮助以这种格式移动它们,请告诉我,希望它有所帮助!
并不是听起来不错,但我真的试着获得声望:)
答案 1 :(得分:1)
不是拥有一枚导弹,而是制造导弹清单。所以不要这样:
if ourevent.key == K_SPACE:
if timer < 400:
timer = 0
ourmissile.x = heromid
ourmissile.y = hero.y
创建一个新导弹并将其添加到列表中:
if timer < 400:
timer = 0
missiles.append(Sprite(heromid, hero.y, 'data/heromissile.bmp')
添加循环以绘制和移动所有导弹:
for m in missiles:
if m.y < 640 and m.y > 0:
m.render()
m.y += -5