到达特定点后,我该如何停止精灵运动?

时间:2019-05-10 21:16:30

标签: python pygame

所以我想拥有它,以便一旦精灵到达网格上的某个点,它们就会停止移动。

我试图通过将if语句放在我调用drawGrid()的位置并将其设置为零的方式来完成此操作,但这并不成功。

import pygame
import time
pygame.init()

clock = pygame.time.Clock()
clock.tick(25)

pygame.mixer.music.load('Game Theme.mp3')

window = pygame.display.set_mode((650, 630))

pygame.display.set_caption("PeaShooters")

avatar = pygame.image.load('Sprite 1 Red.png')
enemy1 = pygame.image.load('Sprite 3 Red.png')
enemy2 = pygame.image.load('Sprite 3 Yellow.png')
background = pygame.image.load('Bg.jpg')
white = (255, 255, 255)

class player(object):
    def __init__(self, x, y, width, height, shots):
        self.x = 300
        self.y = 500
        self.width = 40
        self.height = 60
        self.vel = shots


def drawGrid():
    window.blit(background, (0,0))
    window.blit(enemy1, (en.x, en.y))
    window.blit(enemy1, (200, en.y))
    window.blit(enemy1, (100, en.y))
    window.blit(enemy1, (400, en.y))
    window.blit(enemy1, (500, en.y))
    window.blit(enemy1, (en.x, en.y))
    window.blit(enemy2, (en2.x, en2.y))
    window.blit(enemy2, (150, en2.y))
    window.blit(enemy2, (250, en2.y))
    window.blit(enemy2, (350, en2.y))
    window.blit(enemy2, (450, en2.y))
    window.blit(enemy2, (530, en2.y))
    window.blit(avatar, (av.x, av.y))
    pygame.draw.line(window, white, [50,50], [50, 600], 5)
    pygame.draw.line(window, white, [50,50], [600, 50], 5)
    pygame.draw.line(window, white, [600,600], [600, 50], 5)
    pygame.draw.line(window, white, [50,600], [600, 600], 5)
    pygame.draw.line(window, white, [50,450], [600, 450], 5)

    for bullet in bullets:
        bullet.draw(window)
    pygame.display.update()


class shots(object):
    def __init__(self, x, y, radius, colour):
        self.x = x
        self.y = y
        self.radius = radius
        self.colour = colour
        self.vel = shots

    def draw(self, window):
        pygame.draw.circle(window, self.colour, (self.x,self.y), self.radius)

class enemy(object):

    def __init__(self, x, y, width, height, end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.end = end
        self.path = [self. x, self.end]
        self.vel = 4

    def draw(self,window):
        self.move()

    def move(self):
        pass

av = player(300, 500, 40, 60, 9)
en = enemy(300, 100, 40, 60, 500)
en2 = enemy(50, 200, 40, 60, 500)

pygame.mixer.music.play(-1)

bullets = []
running = True
while running:
    pygame.time.delay(100) 

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

    for bullet in bullets:
        if bullet.y < 600 and bullet.y > 70:
            bullet.y -= 8
        else:
            bullets.pop(bullets.index(bullet))

    keys = pygame.key.get_pressed()

    if keys[pygame.K_w] and av.y > 440:
        av.y -= av.vel

    if keys[pygame.K_a] and av.x > 65:
        av.x -= av.vel

    if keys[pygame.K_s] and av.y < 535:
        av.y += av.vel

    if keys[pygame.K_d] and av.x < 530:
        av.x += av.vel

    if keys[pygame.K_SPACE]:
        if len(bullets) < 5:
            bullets.append(shots(round(av.x + av.width//2), round(av.y + av.height//2), 6, (0,0,0)))

    time.sleep(0.5)
    en.y += 10
    time.sleep(0.5)
    en2.y += 10
    if en.y == 500:
        en.y += 0

    drawGrid()

window.blit(enemy, (en.x, en.y))
window.blit(avatar, (x,y))

pygame.quit()

我希望它能使敌人的精灵到达某个点(底部网格线)。敌人的精灵停止了。

1 个答案:

答案 0 :(得分:0)

您想查看en.y是否小于500,然后增加10。

    time.sleep(0.5)
    if en.y < 500:
        en.y += 10
    time.sleep(0.5)
    if en2.y < 500:
        en2.y += 10

    drawGrid()

执行此操作的另一种方法是使用变量

    time.sleep(0.5)
    en.y += en.changey
    en2.y += en2.changey
    if en.y == 500:
        en.changey = 0
    if en2.y == 500:
        en2.changey = 0