从列表中绘制的对象不停留在屏幕上(pygame)

时间:2019-07-30 09:49:32

标签: python pygame

我开始更多地使用类,并且尝试将旧的无类代码转换为这种新格式。之前,我将列表定义为全局变量,可以毫无问题地将多个敌人吸引到屏幕上。但是,这次,有关与班级合作的某些事情会导致敌人被吸引并立即从他们的位置消失。

一些帮助或解决方法将不胜感激。这是代码:

import pygame
import sys
import random
import math

pygame.init()

class enemies(object):
    enemies = []
    def __init__(self, enemy_num, diff, enemy_size):
        self.enemy_num = enemy_num
        self.diff = diff
        self.enemy_s = enemy_size

    def add_enemies(self):
        counter = 0
        if len(self.enemies) < self.enemy_num:
            for enemy in range(0, self.enemy_num):
                if len(self.enemies) < 5:
                    self.enemies.append([counter * (self.enemy_s * 0.5), 200, (50, 168, 82)])
                elif len(self.enemies) > 5 and len(self.enemies) <= 9:
                    self.enemies.append([counter * (self.enemy_s * 0.5), 250, (168, 160, 50)])
                else:
                    self.enemies.append([counter * (self.enemy_s * 0.5), 300, (50, 52, 168)])
                counter += 1

    def move(self, surface):
        for enemy in self.enemies:
            surface.fill((0, 0, 0))
            pygame.draw.rect(surface, enemy[2], (enemy[0], enemy[1], self.enemy_s, self.enemy_s))
            pygame.display.update()

class ship(object):
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color

    def draw_all(self, surface, ship_size):
        surface.fill((0, 0, 0))
        pygame.draw.rect(surface, self.color, (self.x, self.y, ship_size, ship_size))
        pygame.display.update()

    def move(self):
        keys = pygame.key.get_pressed()

        for key in keys:
            if keys[pygame.K_LEFT]:
                self.x -= 0.2
            if keys[pygame.K_RIGHT]:
                self.x += 0.2
            if keys[pygame.K_UP]:
                self.y -= 0.2
            if keys[pygame.K_DOWN]:
                self.y += 0.2

def main():
    width = 800
    height = 1000
    ship_size = 35
    difficulty = 0
    enemy_number = 12
    enemy_size = 35
    player = ship(width / 2, height - 100, (255, 0, 0))
    aliens = enemies(enemy_number, difficulty, enemy_size)
    screen = pygame.display.set_mode((width, height))
    enemy_list = 12
    run = True
    clock = pygame.time.Clock()
    aliens.add_enemies()

    while run:
        player.draw_all(screen, ship_size)
        player.move()
        aliens.move(screen)
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.display.quit()

main()

1 个答案:

答案 0 :(得分:4)

您必须在应用程序的主循环中进行1次清除显示并进行1次显示更新。
.fill().pygame.display.update方法中分别删除.move() .draw_all。清除显示一次,绘制所有对象,然后更新显示:

while run:
    clock.tick(30)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            pygame.display.quit()

    screen.fill((0, 0, 0))  
    player.draw_all(screen, ship_size)
    player.move()
    aliens.move(screen)
    pygame.display.update()

请注意,您没有看到敌人,因为在enemies.move()中绘制单个敌人之前显示清晰,而在ship.draw_all()中绘制船时再次清晰显示