我正在尝试为大学制作一个太空入侵者游戏,我有大部分代码来获得通行证,我让外星人/入侵者向左,向下,向右移动,但我做了一个类并导入它,现在,当我试图让它工作时,它比它应该更加正确。我以为我已经正确地遵循了大学幻灯片,但我无法弄清楚问题。我认为这可能与xPos
数字有关。
这是我的代码:
import sys, pygame, os, Invader
from pygame.locals import *
class SpaceInvaders:
# Constructor of the basic game class.
# This constructor calls initialize and main_loop method.
def __init__(self):
self.initialize()
self.main_loop()
# Initialization method. Allows the game to initialize different
# parameters and load assets before the game runs
def initialize(self):
pygame.init()
pygame.key.set_repeat(1, 1) # This means when I hold A or D it repeats it for you so it doesn't move only a little
os.environ['SDL_VIDEO_CENTERED'] = '1' # centres the game in the window on launch
self.width = 1260
self.height = 700
self.screen = pygame.display.set_mode((self.width, self.height))
self.caption = "Space Invaders!" # This makes the top window say "Brick Breaker"
pygame.display.set_caption(self.caption)
self.framerate = 60
self.clock = pygame.time.Clock()
# Sprites
self.background = pygame.image.load("background.jpg")
self.defender = pygame.image.load("defender.png")
self.alien = pygame.image.load("alien1.png")
self.rocket = pygame.image.load("rocket.png")
# Locations
self.defenderPosX = 570
self.defenderPosY = 610
self.rocketPosX = -1
self.rocketPosY = -1
self.rocketFired = False
self.alienDirection = -1
self.alienSpeed = -1
self.ticks = 0
self.invaders = []
xPos = 300
for i in range(11):
invader = Invader.Invader()
invader.setPosX(xPos)
invader.setPosY(100)
self.invaders.append(invader)
xPos += 65
# main loop method keeps the game running. This method continuously
# calls the update and draw methods to keep the game alive.
def main_loop(self):
while True:
gametime = self.clock.get_time()
self.update(gametime)
self.draw(gametime)
self.clock.tick(self.framerate)
# Update method contains game update logic, such as updating the game
# variables, checking for collisions, gathering input, and
# playing audio.
def update(self, gametime):
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type ==pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE: # Makes the game exit when you press escape
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a or event.key == pygame.K_LEFT: # Makes the Defender move left
self.defenderPosX = self.defenderPosX - 15
elif event.key == pygame.K_d or event.key == pygame.K_RIGHT: # Makes the Defender move right
self.defenderPosX = self.defenderPosX + 15
elif event.key == pygame.K_SPACE and self.rocketFired == False: # Makes the defender shoot a rocket
self.rocketPosX = self.defenderPosX + 47 # Position of the rocekt on launch
self.rocketPosY = 620 - self.rocket.get_height() # Position of the rocket on launch
self.rocketFired = True
if self.rocketFired == True:
self.rocketPosY = self.rocketPosY - 10 # If True, the rocket will move up 10px
if self.rocketPosY < 10:
self.rocketFired = False # When it reaches 10px it'll reset the variable to False to start again
# Stopping the aliens and rocket from going off screen
if self.defenderPosX < 0:
self.defenderPosX = 0
if self.defenderPosX > 1156:
self.defenderPosX = 1156
if self.rocketPosX < 48:
self.rocketPosX = 48
if self.rocketPosX > 1204:
self.rocketPosX = 1204
self.ticks = self.ticks + gametime
if self.ticks > 500:
for i in range(11):
self.invaders[i].moveHorizontal(self.alienSpeed * self.alienDirection)
if self.invaders[0].getPosX() < 100:
self.alienDirection = +1
xPos = 96
for i in range(11):
self.invaders[i].moveVertical(8)
self.invaders[i].setPosX(xPos)
#xPos = xPos + self.alien.get_width()
if self.invaders[10].getPosX() > 1160:
self.invaderDirection = -1
xPos = 1260 - self.alien.get_width() * 11
for i in range(11):
self.invaders[i].moveVertical(8)
self.invaders[i].setPosX(xPos)
#xPos = xPos + self.alien.get_width()
self.ticks = 0
# Draw method, draws the current state of the game on the screen
def draw(self, gametime):
self.screen.blit(self.background, (0, 0))
self.screen.blit(self.defender, (self.defenderPosX, self.defenderPosY))
for i in range(11):
self.screen.blit(self.alien, self.invaders[i].getPosition())
if self.rocketFired == True:
self.screen.blit(self.rocket, (self.rocketPosX, self.rocketPosY))
#self.sprites.update()
pygame.display.flip()
if __name__ == "__main__":
game = SpaceInvaders()
这是我的入侵者类:
class Invader:
def __init__(self):
self.__alienPosX = 0
self.__alienPosY = 50
def setPosX(self, x):
self.__alienPosX = x
def setPosY(self, y):
self._alienPosY = y
def getPosX(self):
return self.__alienPosX
def getPosY(self):
return self.__alienPosY
def getPosition(self):
return (self.__alienPosX, self.__alienPosY)
def moveHorizontal(self, amount):
self.__alienPosX = self.__alienPosX + amount
def moveVertical(self, amount):
self.__alienPosY = self.__alienPosY + amount
答案 0 :(得分:0)
if self.invaders[0].getPosX() < 100:
self.alienDirection = +1
if self.invaders[10].getPosX() > 1160:
self.invaderDirection = -1
其中一件事与另一件事情不一样。
编辑:
另外,你不是把它们全部移到这里吗?
for i in range(11):
self.invaders[i].moveVertical(8)
self.invaders[i].setPosX(xPos)
#xPos = xPos + self.alien.get_width()
不应该有基于i的偏移吗?为什么最后一行被注释掉了?
答案 1 :(得分:0)
我不知道你在这个项目中是否有一些限制,你可以使用或不使用哪些功能和类别,但如果你只使用自定义事件检查here就会更简单!