在一定时间后删除使用类创建的项目

时间:2016-11-29 20:21:12

标签: python pygame

我正在使用一个类来生成一个飞过屏幕的刀,但我想在它离开屏幕后将其删除。我尝试使用pygame.time.delay(),但这导致程序崩溃,我尝试使用list.remove(),但我无法做到这一点。

import pygame
import sys
from pygame.locals import *
import time
from anim import Anim

LEFT, RIGHT = 0, 1

def resetanim():
    global ninja_anim
    if x_change !=0:
        ninja_anim=ninja_run
    elif x_change==0:
        ninja_anim = ninja_stand
    old_dir = ninja_anim.dir
    ninja_anim.face(old_dir)
    ninja_anim.reset()

pygame.init()
display_width=1000
display_height=800
game_display=pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("EPQ Game")
forest_img=pygame.image.load("forest.jpg")
game_display.blit(forest_img,(0,0))
clock=pygame.time.Clock()
ninja_stand=Anim(lambda _: "Idle__000.png",1, 10, (95, 180), True, 1)
ninja_stand.load()
kunai=pygame.image.load("Kunai.png")
kunai=pygame.transform.scale(kunai, (13,55))
ninja_run=Anim("Run__00%s.png".__mod__, 0, 9, (150, 180), True, 10)
ninja_run.load()
ninja_slide=Anim("Slide__00%s.png".__mod__, 0, 9, (150, 145), True, 10)
ninja_slide.load()
ninja_jump=Anim("Jump__00%s.png".__mod__, 0, 9, (150, 180), True, 15)
ninja_jump.load()
ninja_throw = Anim("Throw__00%s.png".__mod__, 0, 9, (150, 180), False, 10, oncomplete=resetanim)
ninja_throw.load()
ninja_anim = ninja_stand

def ninja(x,y):
    game_display.blit(ninja_anim.next(),(x,y))

class Knife():
    def __init__(self, vx):
        self.x=display_width//2+30
        self.y=int(ground+65)
        self.vx=vx
        self.angle = 0
        self.vangle = 30
    def update(self):
        self.x+=self.vx
        self.angle += self.vangle

    def draw(self):
        game_display.blit(pygame.transform.rotate(kunai, self.angle), (self.x, self.y))

x= 0     
ground = (display_height * 0.6)
y = ground - 10
x_change = 0
y_change = 0
gravity = 1
jump_speed = 15
img_width = 1600
img_facing_right = True
bx=0
ix=0
knifes=[]


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_LEFT:
                ninja_anim=ninja_run
                ninja_anim.face(LEFT)
                img_facing_right=False
                x_change = 5
            elif event.key==pygame.K_RIGHT:
                ninja_anim=ninja_run
                ninja_anim.face(RIGHT)
                img_facing_right=True
                x_change = -5
            elif event.key == pygame.K_UP and y >= ground:
                y_change -= jump_speed
                old_dir = ninja_anim.dir
                ninja_anim=ninja_jump
                ninja_anim.face(old_dir)
                ninja_anim.reset()
            elif event.key == pygame.K_DOWN and y >=ground:
                ninja_anim=ninja_slide
                if img_facing_right==True:
                    ninja_anim.face(RIGHT)
                elif img_facing_right==False:
                    ninja_anim.face(LEFT)
            elif event.key == pygame.K_SPACE and y >= ground:
                knifes.append(Knife(20 if img_facing_right else -20))
                old_dir = ninja_anim.dir
                ninja_anim = ninja_throw
                ninja_anim.face(old_dir)
                ninja_anim.reset()
        if event.type==pygame.KEYUP:
            if event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT:
               x_change = 0
               ninja_anim=ninja_stand
               if img_facing_right==True:
                   ninja_anim.face(RIGHT)
               elif img_facing_right==False:
                    ninja_anim.face(LEFT)
               ninja_anim.reset()
            elif event.key==pygame.K_DOWN:
                if x_change !=0:
                    ninja_anim=ninja_run
                elif x_change == 0:
                    ninja_anim=ninja_stand
                ninja_anim.reset()

    x -= x_change
    bx+=x_change
    ix+=x_change
    if y < ground:
        y_change += gravity
    y += y_change
    if y > ground:
        y = ground
        y_change = 0
        if x_change !=0:
            ninja_anim=ninja_run
        elif x_change==0:
            ninja_anim=ninja_stand
        ninja_anim.reset()
    game_display.fill([255,255,255])
    if ix < -1600 or ix > 1600:
        ix = 0
    game_display.blit(forest_img, (ix,0))
    game_display.blit(forest_img, (ix+1600,0))
    game_display.blit(forest_img, (ix-1600,0))

    for knife in knifes:
        knife.draw()
        knife.update()



    ninja(500, y)
    pygame.display.update()
    clock.tick(60)

有人可以帮助我吗?感谢。

1 个答案:

答案 0 :(得分:0)

首先,pygame.time.delay()将暂停整个程序。您可以使用Clock.tick()来跟踪时间,也可以通过向更新功能添加此类代码来检查刀具的x或y坐标是否在屏幕外。

if self.x > display_width:
     knifes.remove(self)
if self.x < 0:
     knifes.remove(self)
if self.y < 0:
     knifes.remove(self)
if self.y > display_height:
     knifes.remove(self)