彼此相邻的物品清单?

时间:2016-06-04 22:55:04

标签: python pygame

我正在pygame上制作太空入侵者,但我目前只是将外星人安排在彼此旁边的一条线上。外星人列在一个列表中(其中60个)我需要帮助他们排成一行。以下是我到目前为止的情况:

import pygame, random, sys
from time import sleep
from pygame.locals import *

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
NAVYBLUE = (0,0,45)
GREY = (128,128,128)

pygame.init()
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])

class Block(pygame.sprite.Sprite):
    def __init__(self, image):
        aliens = pygame.image.load('aliens.png')
        aliens = pygame.transform.scale(aliens, (20,20))
        super(Block, self).__init__()

        self.image = aliens

        self.rect = self.image.get_rect()

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.mouse.set_visible(0)
        ship = pygame.image.load('spaceship.png')
        ship = pygame.transform.scale(ship, (20,30))
        super(Player, self).__init__()

        self.image = ship
        self.rect = self.image.get_rect()
    def update(self):
        pos = pygame.mouse.get_pos()
        self.rect.x = pos[0]

class Bullet(pygame.sprite.Sprite):
    def __init__(self):
        super(Bullet, self).__init__()
        self.image = pygame.Surface([4, 10])
        self.image.fill(WHITE)
        self.rect = self.image.get_rect()

    def update(self):
        self.rect.y -= 3

all_sprites_list = pygame.sprite.Group()
block_list = pygame.sprite.Group()
bullet_list = pygame.sprite.Group()

for i in range(60):
    block = Block(BLUE)
    block.rect.x = random.randrange(screen_width)
    block.rect.y = random.randrange(350)
    block_list.add(block)
    all_sprites_list.add(block)

player = Player()
all_sprites_list.add(player)
done = False
clock = pygame.time.Clock()
score = 0
time = 1500
player.rect.y = 370
font = pygame.font.SysFont(None, 30)

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        elif event.type == pygame.MOUSEBUTTONDOWN:
            bullet = Bullet()
            bullet.rect.x = player.rect.x + 9.5
            bullet.rect.y = player.rect.y
            all_sprites_list.add(bullet)
            bullet_list.add(bullet)

    all_sprites_list.update()



    for bullet in bullet_list:


        block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)


        for block in block_hit_list:
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)
            score += 1


        if bullet.rect.y < -10:
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)

    screen.fill(NAVYBLUE)
    scoredisplay = font.render("Score {0}".format(score), 1, (WHITE))
    screen.blit(scoredisplay, (5, 10))
    all_sprites_list.draw(screen)
    timedisplay = font.render("Time {0}".format(time), 1, (WHITE))
    screen.blit(timedisplay, (5, 30))

    pygame.display.flip()


    clock.tick(60)

    time -= 1
    pygame.time.delay(10)

    if time == 0:
        done = True
        screen.fill(WHITE)
        display = font.render("YOU RAN OUT OF TIME!! :( Your final score was {}".format(score), 1, (BLACK))
        screen.blit(display, (35, screen_height/2))
        pygame.display.flip()
        sleep(3)


pygame.quit() 

1 个答案:

答案 0 :(得分:1)

您将外国人置于随机位置

for i in range(60):
    block = Block(BLUE)
    block.rect.x = random.randrange(screen_width)
    block.rect.y = random.randrange(350)
    block_list.add(block)
    all_sprites_list.add(block)

要将它们排成一行,请尝试以下

for i in range(60):
    block = Block(BLUE)
    block.rect.x = 30 * i
    block.rect.y = 120
    block_list.add(block)
    all_sprites_list.add(block)

但是,由于你有60个,一些将不在屏幕上。