Добавление кулдауна при выстреле
Хотел добавить кулдаун при выстреле класса snaryad1 но ничего не получалось, помогите пожалуйста... вообще не имею понятия как работать с time и cooldown
pygame.init()
win = pygame.display.set_mode((1000, 600))
pygame.display.set_caption("Runner")
walkRight = [pygame.image.load('1_nright.png'), pygame.image.load('1_nright.png'),pygame.image.load('1_nright.png'),pygame.image.load('1_nright.png'),pygame.image.load('1_nright.png'),pygame.image.load('1_nright.png')]
walkLeft = [pygame.image.load("1_nleft.png"), pygame.image.load("1_nleft.png"),pygame.image.load("1_nleft.png"),pygame.image.load("1_nleft.png"),pygame.image.load("1_nleft.png"),pygame.image.load("1_nleft.png")]
swordAttackright = [pygame.image.load("sword_atack1.png"),pygame.image.load("sword_atack2.png"),pygame.image.load("sword_atack3.png"),pygame.image.load("sword_atack4.png"),pygame.image.load("sword_atack5.png"),pygame.image.load("sword_atack6.png")]
jump = pygame.image.load("jump.png")
playerStand = pygame.image.load("nstay.png")
monu1 = pygame.image.load("bg.png")
clock = pygame.time.Clock()
x = 50
y = 430
width = 60
height = 71
speed = 15
isSword = False
swordCount = 10
isJump = False
jumpCount = 10
score = 0
attack = False
left = False
up = False
down = False
right = False
animCount = 0
lastMove = "right"
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
class Player(object):
def __init__(self,x,y,width,height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.isJump = False
self.left = False
self.right = False
self.walkCount = 0
self.jumpCount = 10
self.standing = True
self.hitbox = (self.x + 20, self.y, 28, 60)
self.health = 10
self.visible = True
def draw(self, win):
if self.visible:
if self.walkCount + 1 >= 30:
self.walkCount = 0
if not(self.standing):
if self.left:
win.blit(walkLeft[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
elif self.right:
win.blit(walkRight[self.walkCount//3], (self.x,self.y))
self.walkCount +=1
else:
if self.right:
win.blit(char, (self.x,self.y))
else:
win.blit(walkRight[0], (self.x, self.y))
self.hitbox = (self.x + 0, self.y + 0,44,66)
class snaryad1():
def __init__(self, x , y , radius, color, facing):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.facing = facing
self.vel = 15 * facing
def draw(self, win):
pygame.draw.circle(win, self.color, (self.x, self.y), self.radius)
class snaryad():
def __init__(self, x , y , radius, color, facing):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.facing = facing
self.vel = 15 * facing
def draw(self, win):
pygame.draw.circle(win, self.color, (self.x, self.y), self.radius)
class enemy(object):
walkRight = [pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png")]
walkLeft = [pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png"),pygame.image.load("gobling.png")]
def __init__(self, x, y, width, height, end):
self.x = x / 2
self.y = y / 2
self.width = width
self.height = height
self.end = end
self.path = [self.x, self.end,self.y,self.end]
self.walkCount = 0
self.vel = 3
self.health = 10
self.hitbox = (self.x , self.y , 0, 0)
self.visible = True
def draw(self, win):
self.move()
if self.visible:
if self.walkCount + 1 >= 33:
self.walkCount = 0
if self.vel > 0:
win.blit(self.walkRight[self.walkCount // 20], (self.x, self.y))
self.walkCount += 1
else:
win.blit(self.walkLeft[self.walkCount // 20], (self.x, self.y))
self.walkCount += 1
pygame.draw.rect(win, (128,0,0), (self.hitbox[0], self.hitbox[1] - 20, 70, 10))
pygame.draw.rect(win, (0,255,0), (self.hitbox[0], self.hitbox[1] - 20, 70 -(7 * (10 - self.health)), 10))
if self.health == 0:
self.health = 10
self.hitbox = (self.x + 0, self.y + 0,44,66)
def move(self):
if self.vel > 0:
if self.x + self.vel < self.path[1]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0
else:
if self.x - self.vel > self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0
def hit(self):
if self.health > 0:
self.health -= 1
else:
self.visible = False
print("you hit negr")
def drawWindow():
global animCount
win.blit(monu1, (0, 0))
goblin.draw(win)
text = font.render(" SCORE " + str(score),1, (0,0,0))
win.blit(text,(390,10))
if animCount + 1 >= 30:
animCount = 0
if left:
win.blit( walkLeft[animCount // 5], (x, y) )
animCount += 1
elif right:
win.blit( walkRight[animCount // 5], (x, y) )
animCount += 1
else:
win.blit(playerStand, (x, y))
for bullet in bullets:
bullet.draw(win)
pygame.display.update()
player = Player(50, 430, 64,64)
font = pygame.font.SysFont("comicsans", 30, True)
run = True
bullets = []
goblin = enemy(200, 300, 64, 64, 1000 - width )
while run:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for bullet in bullets:
if bullet.y - bullet.radius < goblin.hitbox[1] + goblin.hitbox[3] and bullet.y + bullet.radius > goblin.hitbox[1]:
if bullet.x + bullet.radius > goblin.hitbox[0] and bullet.x - bullet.radius < goblin.hitbox[0] + goblin.hitbox[2]:
goblin.hit()
score += 1
bullets.pop(bullets.index(bullet))
if bullet.x < 1000 and bullet.x > 0:
bullet.x += bullet.vel
else:
bullets.pop(bullets.index(bullet))
keys = pygame.key.get_pressed()
if keys[pygame.K_z]:
if lastMove == "right":
facing = 1
else:
facing = -1
if len(bullets) < 5:
bullets.append(snaryad1(round(x + width // 2 ), round(y + height // 2 + 10), 5, (255,0,0),facing))
bullets.append(snaryad1(round(x + width // 2 ), round(y + height // 2 - 10), 5, (255,0,0),facing))
if event.type == pygame.MOUSEBUTTONDOWN:
if lastMove == "right":
facing = 1
else:
facing = -1
if len(bullets) < 5:
bullets.append(snaryad(round(x + width // 2), round(y + height // 2), 5, (255, 0, 0), facing))
if keys[pygame.K_q] and x < 1000 - width - 5:
x += speed * 2.5
if keys[pygame.K_e] and x > 5:
x -= speed * 2.5
if keys[pygame.K_w] and y > 5:
y -= speed
if keys[pygame.K_s] and y < 600 - height -5:
y += speed
if keys[pygame.K_a] and x > 5:
x -= speed
left = True
right = False
lastMove = "left"
elif keys[pygame.K_d] and x < 1000 - width - 5:
x += speed
left = False
right = True
lastMove = "right"
else:
left = False
right = False
animCount = 0
if not(isJump):
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
if jumpCount < 0:
y += (jumpCount ** 2) / 2
else:
y -= (jumpCount ** 2) / 2
jumpCount -= 1
else:
isJump = False
jumpCount = 10
if keys[pygame.K_f]:
attack = True
else:
if swordCount >= -10:
if swordCount < 0:
y = y
x = x
else:
attack = False
drawWindow()
pygame.quit()```