我怎样才能让敌人死后丢钱?

2024-06-01 13:26:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我想让一些敌人在我的游戏下降“上升点”(升级点的形式小黄色方块)当我射击他们。我尝试了一些不同的方法,但似乎不知道如何在敌人刚刚死亡的地方产生这些可收集的点。有人知道我该如何实现这一点吗?在

上流社会:

class Up(pygame.sprite.Sprite):

    def __init__(self, color):
        super().__init__()
        self.image = pygame.Surface([5, 5])
        self.image.fill(color)
        self.rect = self.image.get_rect()

下面是一个敌人被击中死亡的循环:

^{pr2}$

很抱歉没有发布我的全部代码,主游戏循环在底部:)

import pygame
import math
import random

BLACK  = (0, 0, 0)
WHITE  = (255, 255, 255)
GREEN  = (0, 255, 0)
RED    = (255, 0, 0)
BLUE   = (0, 0, 255)
ORANGE = (255, 119, 0)
ZOMBIE_GREEN = (122, 172, 34)
YELLOW = (255, 255, 0)

cursor_x = 100
cursor_y = 100

class Player(pygame.sprite.Sprite):

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

        # pygame.Surface will create a rectangle with the width and height given
        # and the command below it tells it to fill it in with that color
        self.image = pygame.Surface([15, 15])
        self.image.fill(color)
        self.rect = self.image.get_rect()

        # This defines the starting position (x, y)
        # of whatever sprite is passed through
        self.rect.x = 600
        self.rect.y = 300

        # This is the current speed it will move when drawn
        self.change_x = 0
        self.change_y = 0
        self.walls = None

    # Defines how the player will move
    def movement(self, x, y):
        self.change_x += x
        self.change_y += y


    # Updates the information so the screen shows the player moving
    def update(self):
        self.rect.x += self.change_x

        block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
        for block in block_hit_list:
            if self.change_x > 0:
                self.rect.right = block.rect.left
            else:
                self.rect.left = block.rect.right

        self.rect.y += self.change_y

        block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
        for block in block_hit_list:
            if self.change_y > 0:
                self.rect.bottom = block.rect.top
            else:
                self.rect.top = block.rect.bottom

class Enemy(pygame.sprite.Sprite):
    def __init__(self, color):
        super().__init__()
        self.image = pygame.Surface([20, 20])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.pos_x = self.rect.x = random.randrange(35, screen_width - 35)
        self.pos_y = self.rect.y = random.randrange(35, screen_height - 135)

        # How Zombies move towards player

    def update(self):
        zombie_vec_x = self.rect.x - player.rect.x
        zombie_vec_y = self.rect.y - player.rect.y
        vec_length = math.sqrt(zombie_vec_x ** 2 + zombie_vec_y ** 2)

        if self.rect.x != player.rect.x and self.rect.y != player.rect.y:
            zombie_vec_x = (zombie_vec_x / vec_length) * 1 # These numbers determine
            zombie_vec_y = (zombie_vec_y / vec_length) * 1 # zombie movement speed

        self.pos_x -= zombie_vec_x
        self.pos_y -= zombie_vec_y

        self.rect.x = self.pos_x
        self.rect.y = self.pos_y


        block_hit_list = pygame.sprite.spritecollide(self, sprites_list, False)
        for block in block_hit_list:
            if self.rect.x > 0:
                self.rect.right = block.rect.left
            elif self.rect.x < 0:
                self.rect.left = block.rect.right
            elif self.rect.y > 0:
                self.rect.bottom = block.rect.top
            else:
                self.rect.top = block.rect.bottom       

class Wall(pygame.sprite.Sprite):

    def __init__(self, color, x, y, width, height):
        super().__init__()

        self.image = pygame.Surface([width, height])
        self.image.fill(color)

        self.rect = self.image.get_rect()

        self.rect.x = x
        self.rect.y = y

class Cursor(pygame.sprite.Sprite):

    def __init__(self, width, height):
        self.groups = all_sprites_list
        self._layer = 1

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([width, height])
        self.image.fill(RED)

        self.rect = self.image.get_rect()
        self.walls = None

    # This updates the cursor to move along with your
    # mouse position (defined in control logic)
    def update(self):
        self.rect.x = cursor_x
        self.rect.y = cursor_y

class Bullet(pygame.sprite.Sprite):

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

        self.image = pygame.Surface([8, 8])
        self.image.fill(ORANGE)
        self.rect = self.image.get_rect()

        # Instead of using the rect. positions, we'll use pos_ variables
        # to calculate position. This is because the rect. uses integers
        # while a variable can have exact float numbers. This will keep
        # the bullets trajectory exact istead of useing a general
        # (rounded) whole number <3
        self.pos_x = player.rect.x + 4  # Set up pos_x and pos_y here
        self.pos_y = player.rect.y + 4  # rather than rect.x and rect.y

        self.walls = None
        self.change_x = 0
        self.change_y = 0

        speed = 6
        bullet_vec_x = (cursor.rect.x - 4) - player.rect.x
        bullet_vec_y = (cursor.rect.y - 4) - player.rect.y
        vec_length = math.sqrt(bullet_vec_x ** 2 + bullet_vec_y ** 2)
        bullet_vec_x = (bullet_vec_x / vec_length) * speed
        bullet_vec_y = (bullet_vec_y / vec_length) * speed
        self.change_x += bullet_vec_x
        self.change_y += bullet_vec_y

    def update(self):
        self.pos_x += self.change_x  # Update pos_x and pos_y. They will become floats
        self.pos_y += self.change_y  # which will let them maintain sub-pixel accuracy.

        self.rect.x = self.pos_x # Copy the pos values into the rect, where they will be
        self.rect.y = self.pos_y # rounded off. That's OK since we never read them back.



pygame.init()

screen_size = pygame.display.Info()

size = (1300, 720)
screen = pygame.display.set_mode(size)

#size = (screen_size.current_w, screen_size.current_h)
#screen = pygame.display.set_mode(
#    ((screen_size.current_w, screen_size.current_h)),pygame.FULLSCREEN
#    )

screen_width = screen_size.current_w
screen_height = screen_size.current_h

pygame.display.set_caption("Zombie Shooter")



wall_list = pygame.sprite.Group()
zombie1_list = pygame.sprite.Group()
sprites_list = pygame.sprite.Group()
bullet_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()

# Walls are made here = (x_coord for where it starts,
# y_coord for where it starts, width of wall, height of wall)
# These walls are made with fullscreen dimentions, not any set dimentions
# Left
wall = Wall(BLUE, 0, 0, 10, screen_height)
wall_list.add(wall)
all_sprites_list.add(wall)
# Top
wall = Wall(BLUE, 0, 0, screen_width, 10)
wall_list.add(wall)
all_sprites_list.add(wall)
# Bottom
wall = Wall(BLUE, 0, screen_height - 10, screen_width, 10)
wall_list.add(wall)
all_sprites_list.add(wall)
# Right
wall = Wall(BLUE, screen_width - 10, 0, 10, screen_width)
wall_list.add(wall)
all_sprites_list.add(wall)
# HUD Border
wall = Wall(BLUE, 0, screen_height - 100, screen_width, 10)
wall_list.add(wall)
all_sprites_list.add(wall)

# This creates the actual player with the parameters set in ( ).
# However, we must add the player to the all_sprites_list
# so that it will actually be drawn to the screen with the draw command
# placed right after the screen.fill(BLACK) command.
player = Player(WHITE)
player.walls = wall_list
all_sprites_list.add(player)

zombie = Enemy(ZOMBIE_GREEN)
zombie.walls = wall_list
for i in range(5):
    zombie = Enemy(ZOMBIE_GREEN)
    all_sprites_list.add(zombie)
    zombie1_list.add(zombie)
    sprites_list.add(zombie)

cursor = Cursor(7, 7)
cursor.walls = wall_list
all_sprites_list.add(cursor)

bullet = Bullet()

font = pygame.font.SysFont("crushed", 30)

score = 0
up_score = 0

done = False

clock = pygame.time.Clock()

pygame.mouse.set_visible(0)

# -------- Main Program Loop -----------
while not done:

    # --- Main event loop ---
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        # Press 'P' to quit the game_____
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_p:
                done = True
        #________________________________

        # Keyboard controls. The numbers inside change the speed of the player
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                player.movement(-4, 0)
            elif event.key == pygame.K_d:
                player.movement(4, 0)
            elif event.key == pygame.K_w:
                player.movement(0, -4)
            elif event.key == pygame.K_s:
                player.movement(0, 4)

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                player.movement(4, 0)
            elif event.key == pygame.K_d:
                player.movement(-4, 0)
            elif event.key == pygame.K_w:
                player.movement(0, 4)
            elif event.key == pygame.K_s:
                player.movement(0, -4)
        # ___________________________________________________________________

        # Mouse Controls----------------------------
        pos = pygame.mouse.get_pos()
        cursor_x = pos[0]
        cursor_y = pos[1]

        if cursor_x <= 10:
            cursor_x = 10
        if cursor_x >= (screen_width - 17):
            cursor_x = (screen_width - 17)

        if cursor_y <= 10:
            cursor_y = 10
        if cursor_y >= (screen_height - 107):
            cursor_y = (screen_height - 107)

        elif event.type == pygame.MOUSEBUTTONDOWN:
            bullet = Bullet()
            all_sprites_list.add(bullet)
            bullet_list.add(bullet)
        #--------------------------------------------

    all_sprites_list.update()

    # How bullets vanish when they hit a sprite or a wall______________________
    for bullet in bullet_list: #For each bullet:
        # Whenever a bullet collides with a zombie,
        block_hit_list = pygame.sprite.spritecollide(bullet, zombie1_list, True)
        for i in block_hit_list:
            # Destroy the bullet and zombie and add to the score
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)
            score += 100


    for bullet in bullet_list:
        block_hit_list = pygame.sprite.spritecollide(bullet, wall_list, False)
        for i in block_hit_list:
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)
    #--------------------------------------------------------------------------

    cursor.update()
    bullet_list.update()
    sprites_list.update()
    pygame.mouse.set_visible(0)


    screen.fill(BLACK)

    all_sprites_list.draw(screen)

    text = font.render("Score: " + str(score), True, WHITE)
    screen.blit(text, [30, screen_height - 64])

    pygame.display.flip()

    clock.tick(60)

pygame.quit()

Tags: theposrectselfblockscreenpygamecursor
2条回答

你要做的是在你摧毁僵尸精灵之前,抓住它的位置,然后抽出硬币。然后确保每当玩家控制精灵时,在这种情况下子弹,在接触时“收集”硬币。我没怎么用过python,但主要代码如下所示:

def drawCoin():  
    zombieCoords = grabZombieCoords()   
    drawSprite(zombieCoords())

这基本上就是得到僵尸的坐标,摧毁它,然后把硬币放在僵尸最后一个已知的位置。 希望这有帮助。在

更改您的Up类以接受参数pos,并使用它来设置起始位置:

class Up(pygame.sprite.Sprite):

    def __init__(self, color, pos):
        super().__init__()
        self.image = pygame.Surface([5, 5])
        self.image.fill(color)
        self.rect = self.image.get_rect(center=pos)

现在,当你击中一个僵尸,创建一个Up,使用你刚刚杀死的僵尸的位置。在

^{pr2}$

您没有显示其余的代码,但是我想您在您的.draw和{}并创建一个名为up_list的{}。在

相关问题 更多 >