用空间入侵者克隆反弹动画难度

时间:2015-05-27 00:28:15

标签: python pygame python-3.4

import time
import pygame
import random


player = pygame.image.load('player.png')

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)




class enemy(pygame.sprite.Sprite):
    def __init__(self, color):
        super().__init__()
        self.image = pygame.Surface([10, 15])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        enemy_spd = 1


class Barrier(pygame.sprite.Sprite):
    def __init__(self, color):
        super().__init__()
        self.image = pygame.Surface([30, 10])
        self.image.fill(color)
        self.rect = self.image.get_rect()


class Player(pygame.sprite.Sprite):

    def __init__(self):
        super().__init__()

        self.image = player
        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().__init__()

        self.image = pygame.Surface([2, 10])
        self.image.fill(GREEN)

        self.rect = self.image.get_rect()

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



pygame.init()


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



all_sprites_list = pygame.sprite.Group()
enemy_list = pygame.sprite.Group()
bullet_list = pygame.sprite.Group()
barrier_list = pygame.sprite.Group()

#Create the moving space enemies

###############
enemy1 = enemy(WHITE)
enemy2 = enemy(WHITE)
enemy3 = enemy(WHITE)
enemy4 = enemy(WHITE)
enemy5 = enemy(WHITE)
enemy6 = enemy(WHITE)
enemy7 = enemy(WHITE)
enemy8 = enemy(WHITE)
################
#create barriers
###################
barrier1 = Barrier(GREEN)
barrier2 = Barrier(GREEN)
barrier3 = Barrier(GREEN)
barrier4 = Barrier(GREEN)
barrier5 = Barrier(GREEN)
barrier6 = Barrier(GREEN)
barrier7 = Barrier(GREEN)
barrier8 = Barrier(GREEN)
barrier9 = Barrier(GREEN)
barrier10 = Barrier(GREEN)
barrier11 = Barrier(GREEN)
barrier12 = Barrier(GREEN)
barrier13 = Barrier(GREEN)
barrier14 = Barrier(GREEN)
barrier15 = Barrier(GREEN)
barrier16 = Barrier(GREEN)
barrier17 = Barrier(GREEN)
barrier18 = Barrier(GREEN)
barrier19 = Barrier(GREEN)
barrier20 = Barrier(GREEN)
barrier21 = Barrier(GREEN)
barrier22 = Barrier(GREEN)
barrier23 = Barrier(GREEN)
barrier24 = Barrier(GREEN)
barrier25 = Barrier(GREEN)
barrier26 = Barrier(GREEN)
barrier27 = Barrier(GREEN)
barrier28 = Barrier(GREEN)
barrier29 = Barrier(GREEN)
barrier30 = Barrier(GREEN)
barrier31 = Barrier(GREEN)
barrier32 = Barrier(GREEN)
###################

enemy1.rect.x =30
enemy1.rect.y =50
enemy2.rect.x =50
enemy2.rect.y =50
enemy3.rect.x =70
enemy3.rect.y =50
enemy4.rect.x =90
enemy4.rect.y =50
enemy5.rect.x =110
enemy5.rect.y =50
enemy6.rect.x =130
enemy6.rect.y =50
enemy7.rect.x =150
enemy7.rect.y =50
enemy8.rect.x =170
enemy8.rect.y =50
####################
barrier1.rect.x =0
barrier1.rect.y =320
barrier2.rect.x =30
barrier2.rect.y =320
barrier3.rect.x =60
barrier3.rect.y =320
barrier4.rect.x =150
barrier4.rect.y =320
barrier5.rect.x =180
barrier5.rect.y =320
barrier6.rect.x =210
barrier6.rect.y =320
barrier7.rect.x =300
barrier7.rect.y =320
barrier8.rect.x =330
barrier8.rect.y =320
barrier9.rect.x =350
barrier9.rect.y =320
barrier10.rect.x =440
barrier10.rect.y =320
barrier12.rect.x =470
barrier12.rect.y =320
barrier13.rect.x =500
barrier13.rect.y =320
barrier14.rect.x =590
barrier14.rect.y =320
barrier15.rect.x =620
barrier15.rect.y =320
barrier16.rect.x =650
barrier16.rect.y =320
barrier17.rect.x =0
barrier17.rect.y =330
barrier18.rect.x =30
barrier18.rect.y =330
barrier19.rect.x =60
barrier19.rect.y =330
barrier20.rect.x =150
barrier20.rect.y =330
barrier21.rect.x =180
barrier21.rect.y =330
barrier22.rect.x =210
barrier22.rect.y =330
barrier23.rect.x =300
barrier23.rect.y =330
barrier24.rect.x =330
barrier24.rect.y =330
barrier25.rect.x =350
barrier25.rect.y =330
barrier26.rect.x =440
barrier26.rect.y =330
barrier27.rect.x =470
barrier28.rect.y =330
barrier29.rect.x =500
barrier29.rect.y =330
barrier30.rect.x =590
barrier30.rect.y =330
barrier31.rect.x =620
barrier31.rect.y =330
barrier32.rect.x =650
barrier32.rect.y =330

enemy_list.add(enemy1, enemy2, enemy3, enemy4, enemy5, enemy6, enemy7, enemy8, barrier1, barrier2, barrier3, barrier4, barrier5, barrier6, barrier7, barrier8, barrier9, barrier10, barrier11, barrier12, barrier13, barrier14, barrier15, barrier16, barrier17, barrier18, barrier19, barrier20, barrier21, barrier22, barrier23, barrier24, barrier25, barrier26, barrier27, barrier28, barrier29, barrier30, barrier31, barrier32)
all_sprites_list.add(enemy1, enemy2, enemy3, enemy4, enemy5, enemy6, enemy7, enemy8, barrier1, barrier2, barrier3, barrier4, barrier5, barrier6, barrier7, barrier8, barrier9, barrier10, barrier11, barrier12, barrier13, barrier14, barrier15, barrier16, barrier17, barrier18, barrier19, barrier20, barrier21, barrier22, barrier23, barrier24, barrier25, barrier26, barrier27, barrier28, barrier29, barrier30, barrier31, barrier32)




player = Player()
all_sprites_list.add(player)


done = False


clock = pygame.time.Clock()

score = 0
player.rect.y = 370

# Main Program 
while not done:

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

        elif event.type == pygame.MOUSEBUTTONDOWN:
            # Fire a bullet if the user clicks the mouse button
            bullet = Bullet()
            # Set the bullet so it is where the player is
            bullet.rect.x = player.rect.x + 14
            bullet.rect.y = player.rect.y + 8
            # Add the bullet to the lists
            all_sprites_list.add(bullet)
            bullet_list.add(bullet)
            pygame.mouse.set_visible(0)




  ################STACKOVERFLOW#################
    if enemy1.rect.x<=700:
        enemy1.rect.x += 1
        if enemy1.rect.x>=560 and enemy1.rect.y<=55:
            enemy1.rect.y += 5
            enemy1.rect.x -= 2

    if enemy2.rect.x<=700:
        enemy2.rect.x += 1
        if enemy2.rect.x>=580 and enemy2.rect.y<=55:
            enemy2.rect.y += 5
    if enemy3.rect.x<=700:
        enemy3.rect.x += 1
        if enemy3.rect.x>=600 and enemy3.rect.y<=55:
            enemy3.rect.y += 5
    if enemy4.rect.x<=700:
        enemy4.rect.x += 1
        if enemy4.rect.x>= 620 and enemy4.rect.y<=55:
            enemy4.rect.y += 5
    if enemy5.rect.x<=700:
        enemy5.rect.x += 1
        if enemy5.rect.x>=640 and enemy5.rect.y<=55:
            enemy5.rect.y += 5
    if enemy6.rect.x<=700:
        enemy6.rect.x += 1
        if enemy6.rect.x>=660 and enemy6.rect.y<=55:
            enemy6.rect.y += 5
    if enemy7.rect.x<=700:
        enemy7.rect.x += 1
        if enemy7.rect.x>=680 and enemy7.rect.y<=55:
            enemy7.rect.y += 5
    if enemy8.rect.x<= 700:
        enemy8.rect.x += 1
        if enemy8.rect.x>=700 and enemy8.rect.y<=55:
            enemy8.rect.y += 5




    all_sprites_list.update()

    for bullet in bullet_list:
        enemy_hit_list = pygame.sprite.spritecollide(bullet, enemy_list, True)


        for enemy in enemy_hit_list:
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)


#Make bullet disappear after going off screen
        if bullet.rect.y < -10:
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)

    pygame.display.set_caption("Speece Invoders")
    # Clear the screen
    screen.fill(BLACK)

    # Draw all the spites
    all_sprites_list.draw(screen)

    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()


    clock.tick(40)

pygame.quit()

在它所说的部分##### STACKOVERFLOW #####是我遇到问题的地方,我试图让第一个敌人在太空入侵者向下移动后离开(游戏是一个克隆人) )。然而,所发生的一切都是敌人倒下并完全停顿。有关如何解决它的任何想法?我是一名初学程序员,任何事情都会有所帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

Enemy课程制作曲目的一种可能方法是拥有这样的默认曲目:

class Enemy(pygame.sprite.Sprite):
  path = [(0,0), (1,5), (2,11) .... ]
  def __init__(self, color, x, y): #adding additional x, y arguments will declutter your code
    super().__init__()
    self.x, self.y = x, y #assign coordinates NOW
    self.start_x, self.start_y = x, y #added to using path
    self.current_path_index = 0
    #et cetera

  def update(self):
    self.x = self.start_x + path[self.current_path_index][0]
    self.y = self.start_y + path[self.current_path_index][0]
    self.current_path_index += 1

创建的路径可以Enemy.path访问,或者在Enemy的实例中访问self.path,有点像self.update()

这样做会为你的敌人制作一个默认路径,并在每次为精灵调用update()函数时增加它。