Python Pygame Enemy Collision

时间:2015-08-18 16:56:07

标签: python pygame

我在游戏中有一个pygame游戏,问题是我想要当玩家与lvl1a和/或lvl1b发生碰撞时,它们会被破坏,玩家会变大。我怎么做到这一点?我知道我不得不重新绘制场景,但是当我尝试了lvl1a和lvl1b就不会消失......

{{1}}

1 个答案:

答案 0 :(得分:2)

首先,你需要让玩家通过pygame.Rect.colliderect()与敌人的碰撞发生冲突。 Rect.colliderect()的文档是here

为此,您可以向播放器类添加一个新功能,如下所示:

def collide(self,enemy,enemy_list):
    if self.rect.colliderect(enemy.rect):  # Tests if the player is touching an enemy
        enemy_list.remove(enemy)  # Removes the enemy from the enemy list (Explained lower)

你可以做的就是把敌人放在一个列表中,让它们像这样用for循环渲染:

lvl1a = Lvl1a()
lvl1b = Lvl1b()
enemies = [lvl1a,lvl1b]

然后使用for循环在主循环中渲染它们:

for enemy in enemies:  # Runs through the enemy list
    enemy.move()  # Moves the enemy
    player.collide(enemy,enemies)  # Checks if enemy is touching player
    enemy.draw(screen)  # Renders Enemies in the list

通过这种方式,当玩家与敌人发生碰撞时,敌人将被从敌人列表中移除,并且不再被渲染。

我希望这会有所帮助。如果你没有得到任何东西,发表评论,我会看看我是否可以澄清一下我的答案。 :)

固定代码应如下所示:

#! /usr/bin/env python

import os
import random
import pygame
import math
import sys

os.environ["SDL_VIDEO_CENTERED"] = "1"

winw = 600
winh = 600
screen = pygame.display.set_mode((winw, winh))
pygame.display.set_caption(" Charger V1 Lvl1")

clock = pygame.time.Clock()

player1 = pygame.rect.Rect(32, 32, 24, 24)
rect1 = pygame.rect.Rect(550, 550, 16, 16)
rect2 = pygame.rect.Rect(500, 850, 16, 16)

class Player(object):
    def __init__(self):
        self.rect = player1

    def handle_keys(self):
        key = pygame.key.get_pressed()
        dist = 1
        if key[pygame.K_LEFT]:
            self.rect.move_ip(-2, 0)
        if key[pygame.K_RIGHT]:
            self.rect.move_ip(2, 0)
        if key[pygame.K_UP]:
           self.rect.move_ip(0, -2)
        if key[pygame.K_DOWN]:
           self.rect.move_ip(0, 2)

    def collide(self,enemy,enemy_list):
        if self.rect.colliderect(enemy.rect):  # Tests if the player is touching an enemy
            enemy_list.remove(enemy)  # Removes the enemy from the enemy list (Explained lower)           

    def draw(self, surface):
        pygame.draw.rect(surface, (0, 0, 128), self.rect)

class Lvl1a(object):
    def __init__(self):
        self.rect = rect1
        self.x = 0
        self.y = 0

    def move(self):
        self.rect.move_ip(0, -1)

    def draw(self, surface):
        pygame.draw.rect(surface, (100, 100, 100), self.rect)

class Lvl1b(object):
    def __init__(self):
        self.rect = rect2
        self.x = 0
        self.y = 0

    def move(self):
        self.rect.move_ip(0, -1)

    def draw(self, surface):
        pygame.draw.rect(surface, (100, 100, 100), self.rect)

pygame.init()

player = Player()
clock = pygame.time.Clock()
lvl1a = Lvl1a()
lvl1b = Lvl1b()
enemies = [lvl1a,lvl1b]  # Create the enemies list

running = True       
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
           break
           running = False

    screen.fill((255, 255, 255))

    for enemy in enemies:  # Runs through the enemy list
        enemy.move()  # Moves the enemy
        player.collide(enemy,enemies)  # Checks if enemy is touching player
        enemy.draw(screen)  # Renders Enemies in the list

    player.draw(screen)
    player.handle_keys()
    pygame.display.update()

    clock.tick(40)