如何添加来自左右的敌人?

时间:2018-01-10 06:30:24

标签: python pygame

我正在创建一个射击游戏,其中底部中间的玩家可以射击来自左下角和右下角的敌人。我看过在线教程,所有的射击游戏都是空间射击,敌人来自顶部到底部。这是我现在的代码:

import pygame
import time
import random

# --- constants --- (UPPER_CASE_NAMES)

DISPLAY_WIDTH = 800
DISPLAY_HEIGHT = 600

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

FPS = 60

# - init -

pygame.init()

gameDisplay = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
pygame.display.set_caption("The Adventure of the Excorist's Meter")

# - objects -

background_image = pygame.image.load("skyline_background.jpg").convert()
background_position = [0, 0]


player_image_left = pygame.image.load('character_left.png')
player_image_right = pygame.image.load('character_right.png')

player_image = player_image_left

# Load enemy image
zombie_image = pygame.image.load('zombie_image.png')
devil_image = pygame.image.load('devil_image.png')
ghost_image = pygame.image.load('ghost_image.png')


def enemy(enemy_x,enemy_y):
    gameDisplay.blit(zombie_image, (enemy_x, enemy_y))

player_speed = 0
player_x = DISPLAY_WIDTH * 0.45
player_y = DISPLAY_HEIGHT * 0.65
player_x_change = 0

# - mainloop -

clock = pygame.time.Clock()
crashed = False

while not crashed:

    # - events -

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

        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_LEFT:
               player_x_change = -5
               player_image = player_image_left

            elif event.key == pygame.K_RIGHT:
                player_x_change = 5
                player_image = player_image_right

        if event.type == pygame.KEYUP:
            if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
                player_x_change = 0

    # - updates -

    player_x += player_x_change

    # - draws -

    gameDisplay.blit(background_image, background_position)

    gameDisplay.blit(player_image, (player_x, player_y))

    pygame.display.update()

    clock.tick(FPS)

# - end -

pygame.quit()

1 个答案:

答案 0 :(得分:2)

为每个敌人创建pygame.Rect()以保持其大小和位置

zombie_rect = zombie_image.get_rect()
zombie_rect.y = 300
zombie_speed = 10

devil_rect = devil_image.get_rect()
devil_rect.right = DISPLAY_WIDTH
devil_rect.y = 200
devil_speed = -10

并更改x

中的敌人- updates -位置
zombie_rect.x += zombie_speed
devil_rect.x += devil_speed

# change direction
if zombie_rect.left <= 0 or zombie_rect.right >= DISPLAY_WIDTH:
    zombie_speed = -zombie_speed
if devil_rect.left <= 0 or devil_rect.right >= DISPLAY_WIDTH:
    devil_speed = -devil_speed

并在- draws -中吸引敌人。

gameDisplay.blit(zombie_image, zombie_rect)

gameDisplay.blit(devil_image, devil_rect)

简单地说,你必须做一些与player类似的事情。

如果教程展示了如何从上到下移动,则必须更改y。要从左向右移动,您必须使用x而不是y

BTW:我使用Rect()作为玩家位置(player_rect),所以我可以检查它是否触及了敌人。

import pygame
import random

# --- constants --- (UPPER_CASE_NAMES)

DISPLAY_WIDTH = 800
DISPLAY_HEIGHT = 600

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

FPS = 60

# --- classes ---

# --- functions ---

# --- main ---

# - init -

pygame.init()

gameDisplay = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
pygame.display.set_caption("The Adventure of the Excorist's Meter")

# - objects -

background_image = pygame.image.load("skyline_background.jpg").convert()
background_rect = background_image.get_rect()

player_image_left = pygame.image.load('character_left.png').convert()
player_image_right = pygame.image.load('character_right.png').convert()

player_image = player_image_left
player_rect = player_image.get_rect()

player_rect.x = DISPLAY_WIDTH * 0.45
player_rect.y = DISPLAY_HEIGHT * 0.65

player_speed = 0
player_x_change = 0

# Load enemy image
zombie_image = pygame.image.load('zombie_image.png').convert()
devil_image = pygame.image.load('devil_image.png').convert()
ghost_image = pygame.image.load('ghost_image.png').convert()

zombie_rect = zombie_image.get_rect()
zombie_rect.y = 300
zombie_speed = 10

devil_rect = devil_image.get_rect()
devil_rect.right = DISPLAY_WIDTH
devil_rect.y = 200
devil_speed = -10

# - mainloop -

clock = pygame.time.Clock()
crashed = False

while not crashed:

    # - events -

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

        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_LEFT:
               player_x_change = -5
               player_image = player_image_left

            elif event.key == pygame.K_RIGHT:
                player_x_change = 5
                player_image = player_image_right

        if event.type == pygame.KEYUP:
            if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
                player_x_change = 0

    # - updates -

    player_rect.x += player_x_change

    zombie_rect.x += zombie_speed
    devil_rect.x += devil_speed

    # change direction
    if zombie_rect.left <= 0 or zombie_rect.right >= DISPLAY_WIDTH:
        zombie_speed = -zombie_speed
    if devil_rect.left <= 0 or devil_rect.right >= DISPLAY_WIDTH:
        devil_speed = -devil_speed

    if player_rect.colliderect(zombie_rect):
        print("You touch zombie")
    if player_rect.colliderect(devil_rect):
        print("You touch devil")

    # - draws -

    gameDisplay.blit(background_image, background_rect)

    gameDisplay.blit(player_image, player_rect)

    gameDisplay.blit(zombie_image, zombie_rect)

    gameDisplay.blit(devil_image, devil_rect)

    pygame.display.update()

    clock.tick(FPS)

# - end -

pygame.quit()