【发布时间】:2020-07-18 08:04:23
【问题描述】:
首先,我的意思是制作 3 种具有不同属性和不同颜色的不同子弹。用空格键射击它们,用 1,2 和 3 键选择它们。我没有将整个代码放在这里,但如果您需要更多信息,请随时询问。
LaserDict = {
"redLaser": (RED_LASER,4,1), #Bad Velocity #Bad Firerate
"greenLaser": (GREEN_LASER,10,3), #Excellent Velocity #Good Firerate
"blueLaser": (BLUE_LASER,5,2) #Medium Velocity #Good Firerate
}
class Lasers():
def __init__(self,x,y,laserIndex):
self.x = x
self.y = y
self.laserNames = ["redLaser","greenLaser","blueLaser"]
self.laserIndex = laserIndex
self.laserImg, self.laserVel, self.laserCooldown = LaserDict[self.laserNames[self.laserIndex]]
def draw(self,window):
window.blit(self.laserImg,(self.x,self.y))
def move(self,p):
self.y += p
def offScreen(self,height):
return not (self.y>0 and self.y<=height) #if not in screen return True
class Player():
fpsCooldown = 30
def __init__(self,x,y,health,laserIndex):
blah blah...
self.cooldownReady = 0
self.lasers = []
self.laserIndex = laserIndex
def cooldown(self):
if self.cooldownReady >= self.fpsCooldown:
self.cooldownReady = 0
elif self.cooldownReady > 0:
self.cooldownReady += Lasers(self.x,self.y,self.laserIndex).laserCooldown
def shoot(self):
if self.cooldownReady == 0:
laser = Lasers(self.x+int(self.getWidth()/8) ,self.y, self.laserIndex)
self.lasers.append(laser)
self.cooldownReady = 1
def moveLasers(self):
self.cooldown()
for laser in self.lasers:
p = Lasers(self.x,self.y,self.laserIndex).laserVel
laser.move(-p)
if laser.offScreen(height):
self.lasers.remove(laser)
def main():
blah blah...
if keys[pygame.K_SPACE]:
player.shoot()
if keys[pygame.K_1]:
player.laserIndex = 0
if keys[pygame.K_2]:
player.laserIndex = 1
if keys[pygame.K_3]:
player.laserIndex = 2
代码在一个错误上运行良好。当您通过以下方式更改激光类型时 按 1,2 或 3 它会改变你所有的激光的统计数据存在于 窗户。例如,如果您发射了 5 次“红色激光”并且它们是 还没有离开屏幕,如果您将激光类型更改为“GreenLasers”,所有“红色激光”仍保持红色,但移动就像 绿色的。
【问题讨论】:
-
请发布
Lasers.move方法。
标签: python python-3.x pygame