我使用PyGame模块在Python中创建了一个Space Invaders克隆,但是当到达游戏画面的边缘时,我遇到了一些困难。
当外星人到达游戏画面的边缘时,他们如何同时改变方向并降低等级?
import pygame
import random
class spaceInvader(pygame.sprite.Sprite):
def __init__(self):
self.image = pygame.image.load("spaceInvader.png")
self.x = 200
self.y = 390
self.shots = []
def handle_keys(self):
key = pygame.key.get_pressed()
dist = 5
if key[pygame.K_RIGHT]:
self.x+=dist
elif key[pygame.K_LEFT]:
self.x-=dist
def draw(self, surface):
surface.blit(self.image,(self.x,self.y))
for s in self.shots:
s.draw(screen)
class Alien(pygame.sprite.Sprite):
def __init__(self,x,y,direction,alienType):
pygame.sprite.Sprite.__init__(self)
self.AlienType = alienType
self.Direction = direction
if alienType == 1:
alienImage = pygame.image.load("alien1.png")
self.Speed = 1
self.Score = 5
if alienType == 2:
alienImage = pygame.image.load("alien2.png")
self.Score = 15
self.Speed = 1
if alienType == 3:
alienImage = pygame.image.load("alien3.png")
self.Score = 10
self.Speed = 1
if alienType == 4:
alienImage = pygame.image.load("alien4.png")
self.Score = 20
self.Speed = 1
if alienType == 5:
alienImage = pygame.image.load("alien5.png")
self.Score = 25
self.Speed = 1
self.image = pygame.Surface([26, 50])
self.image.set_colorkey(black)
self.image.blit(alienImage,(0,0))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def moveAliens(self):
if self.Direction == "right":
self.rect.x += self.Speed
if self.Direction == "left":
self.rect.x -= self.Speed
pygame.init()
pygame.mouse.set_visible(False)
screen = pygame.display.set_mode([400,400])
allAliens = pygame.sprite.Group()
spaceInvader = spaceInvader()
pygame.display.set_caption("Space Attack")
background_image = pygame.image.load("Galaxy.png").convert()
pygame.mouse.set_visible(True)
done = False
clock = pygame.time.Clock()
black =( 0, 0, 0)
white =( 255,255,255)
red = (255, 0, 0)
score = 0
enemies = []
#For X coords
spawnPositions = [50,100,150,200,250,300,350]
yCoord = 10
for n in range(5):
for i in range(len(spawnPositions)):
xCoord = spawnPositions[i]
alienType = random.randint(1,5)
alien = Alien(xCoord, yCoord,"right", alienType)
allAliens.add(alien)
yCoord = yCoord + 15
loop = 0
while done == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
for alien in (allAliens.sprites()):
if alien.rect.x < 0:
alien.rect.y = alien.rect.y + 15
alien.Direction = "right"
if alien.rect.x > 395:
alien.rect.y = alien.rect.y + 15
alien.Direction = "left"
loop =+1
for alien in (allAliens.sprites()):
alien.moveAliens()
spaceInvader.handle_keys()
screen.blit(background_image,[0,0])
spaceInvader.draw(screen)
allAliens.draw(screen)
pygame.display.flip()
clock.tick(20)
pygame.quit()
感谢。
答案 0 :(得分:4)
你的问题在于:
for alien in (allAliens.sprites()):
if alien.rect.x < 0:
alien.rect.y = alien.rect.y + 15
alien.Direction = "right"
if alien.rect.x > 395:
alien.rect.y = alien.rect.y + 15
alien.Direction = "left"
loop =+1
我认为外星人目前正在单独下降?
你需要对此进行更改,以便当一个外星人触发这些if语句时,所有外星人y
和direction
都会被恰当地设定,而不仅仅是那个击中一方的人。
答案 1 :(得分:0)
# Space Invader
import turtle
import os
import random
import math
# Set up the screen.
wn = turtle.Screen()
wn.setup(700,700)
wn.bgcolor("lightblue")
wn.title("Space Invaders")
# Draw border
border = turtle.Turtle()
border.speed("fastest")
border.color("white")
border.penup()
border.setposition(-300,-270)
border.pendown()
border.pensize(3)
for side in range(4):
border.forward(560)
border.left(90)
border.hideturtle()
# Set the score to 0.
score = 0
# Draw the score.
score_pen = turtle.Turtle()
score_pen.speed("fastest")
score_pen.color("white")
score_pen.penup()
score_pen.setposition(-290,260)
scorestring = "Score: %s" %score
score_pen.write(scorestring, False, align="left", font = ("Arial",14,"normal"))
score_pen.hideturtle()
# Create the player turtle
player = turtle.Turtle()
player.color("black")
player.speed("fastest")
player.shape("triangle")
player.penup()
player.setposition(0,-265)
player.setheading(90)
playerspeed = 15
# Create enemy
#enemy = turtle.Turtle()
#enemy.shape("circle")
#enemy.color("red")
#enemy.penup()
#enemy.speed(2)
#enemy.setposition(-200,250)
#enemyspeed = 2
# Choose number of enemies.
number_of_enemies = 5
# Create an empty list of enemies.
enemies = []
# Add enemies to list.
for i in range(number_of_enemies):
# Create enemy
enemies.append(turtle.Turtle())
for enemy in enemies:
enemy.shape("circle")
enemy.color("red")
enemy.penup()
enemy.speed(2)
x = random.randint(-200,200)
y = random.randint(100,250)
enemy.setposition(x,y)
enemyspeed = 2
# Create the player's bullet.
bullet = turtle.Turtle()
bullet.color("yellow")
bullet.shape("triangle")
bullet.penup()
bullet.speed("fastest")
bullet.setheading(90)
bullet.shapesize(.5,.5)
bullet.hideturtle()
bulletspeed = 20
# Define bullet state
# ready - ready to fire
# fire - bullet is firing
bulletstate = "ready"
# Move the player left and right.
def move_left():
x = player.xcor()
x -= playerspeed
if x < -280:
x = -280
player.setx(x)
def move_right():
x = player.xcor()
x += playerspeed
if x >250:
x = 250
player.setx(x)
def fire_bullet():
# Declare the bulletstate as a global if it needs changed.
global bulletstate
if bulletstate == "ready":
bulletstate = "fire"
# Move the bullet just above the player.
x = player.xcor()
y = player.ycor() + 10
bullet.setposition(x,y)
bullet.showturtle()
def isCollision(t1,t2):
dx = t1.xcor() - t2.xcor()
dy = t1.ycor()- t2.ycor()
distance = dx**2 + dy**2
distance1 = math.sqrt(distance)
if distance1 < 15:
return True
else:
return False
# Create keyboard bindings
wn.onkey(move_left, "Left")
wn.onkey(move_right,"Right")
wn.onkey(fire_bullet,"space")
wn.listen()
# Main game loop
while True:
for enemy in enemies:
# Move enemy
x = enemy.xcor()
x += enemyspeed
enemy.setx(x)
# Move the enemy back and down
if enemy.xcor()>250:
# Move all the enemies down
for e in enemies:
y = e.ycor()
y -= 40
e.sety(y)
# Change the enemy direction
enemyspeed *= -1
if enemy.xcor()< -280:
# Move all the enemies down
for e in enemies:
y = e.ycor()
y -= 40
e.sety(y)
# change the enemy direction
enemyspeed *= -1
# Check for the collision between bullet and the enemy.
if isCollision(bullet ,enemy):
# Reset the bullet
bullet.hideturtle()
bulletstate = "ready"
bullet.setposition(0,-400)
# Reset the enemy
x = random.randint(-200,200)
y = random.randint(100,250)
enemy.setposition(x,y)
# Update the score.
score += 1
scorestring = "Score: %s" %score
score_pen.clear()
score_pen.write(scorestring, False, align = "left",font = ("Arial",14, "normal"))
if isCollision(player,enemy):
player.hideturtle()
enemy.hideturtle()
print("Game Over")
break
# Move the bullet.
if bulletstate == "fire":
y = bullet.ycor()
y += bulletspeed
bullet.sety(y)
# Check to see if the bullet has gone to the top.
if bullet.ycor() > 275:
bullet.hideturtle()
bulletstate = "ready"
turtle.mainloop()