Пишу свою первую игру и сделал так, чтобы спавнился спрайт helper, который добавляет жизни (жизни еще не доделал). Потом сделал счетчик жизней чтобы он показывал кол - во жизней, но при подборе helper - а к счетчику жизни прибавляются, а потом сразу отнимаются хотя так быть не должно, что делать?
Вот код -
import pygame
import random
cl_black = (0,0,0)
cl_white = (255,255,255)
cl_red = (255,0,0)
cl_green = (0,255,0)
cl_blue = (0,0,255)
cl_yellow = (255,255,0)
fps = 60
width = 700
height = 1000
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Моя первая игра')
clock = pygame.time.Clock()
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((20,20))
self.image.fill(cl_green)
self.rect = self.image.get_rect()
self.rect.center = (width/2, height/2)
self.rect.centerx = width/2
self.rect.centery = height/2
self.speedx = 0
self.speedy = 0
self.shoot_delay = 250
self.last_shoot = pygame.time.get_ticks()
def update(self):
self.speedx = 0
self.speedy = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_d]:
self.speedx += 5
if keystate[pygame.K_a]:
self.speedx -= 5
if keystate[pygame.K_w]:
self.speedy -= 5
if keystate[pygame.K_s]:
self.speedy += 5
if keystate[pygame.K_KP_ENTER]:
self.shoot()
self.rect.centerx += self.speedx
self.rect.centery += self.speedy
if self.rect.bottom > height:
self.rect.bottom = height
if self.rect.top < 0:
self.rect.top = 0
if self.rect.right > width:
self.rect.right = width
if self.rect.left < 0:
self.rect.left = 0
def shoot(self):
now = pygame.time.get_ticks()
if now - self.last_shoot > self.shoot_delay:
self.last_shoot = now
bullet = Bullet(self.rect.centerx, self.rect.top)
all_sprites.add(bullet)
bullets.add(bullet)
class Mob(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 50))
self.image.fill(cl_red)
self.rect = self.image.get_rect()
self.rect.x = random.randrange(width - self.rect.width)
self.rect.y = random.randrange(0,100)
self.speedy = random.randrange(1, 3)
def update(self):
self.rect.y = self.rect.y + self.speedy
if self.rect.top > height:
pygame.guit()
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((10,10))
self.image.fill(cl_blue)
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.bottom = y
self.speedy = -5
def update(self):
self.rect.y += self.speedy
if self.rect.bottom < 0:
self.kill()
class Helper(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((30,30))
self.image.fill(cl_yellow)
self.rect = self.image.get_rect()
self.rect.centerx = random.randrange(20, width - 20)
self.rect.centery = random.randrange(0, 50)
self.speedy = 1
def update(self):
self.rect.centery += self.speedy
if self.rect.top > height:
self.rect.top = 0
class Life(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((10,10))
self.image.fill(cl_white)
self.rect = self.image.get_rect()
self.rect.center = (10,32)
def newmob():
m = Mob()
all_sprites.add(m)
mobs.add(m)
def newlife():
helper = Helper()
all_sprites.add(helper)
helpers.add(helper)
font_name = pygame.font.match_font('arial')
def draw_text(surf,text,size,x,y):
font = pygame.font.Font(font_name, size)
text_surface = font.render(text, True, cl_white)
text_rect = text_surface.get_rect()
text_rect.midtop = (x,y)
surf.blit(text_surface, text_rect)
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
mobs = pygame.sprite.Group()
bullets = pygame.sprite.Group()
helpers = pygame.sprite.Group()
newlife()
lifes = pygame.sprite.Group()
life = Life()
all_sprites.add(life)
lifes.add(life)
newmob()
running = True
while running:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
all_sprites.update()
hits = pygame.sprite.groupcollide(mobs, bullets, True, True)
for hit in hits:
for i in range(2):
newmob()
count_lifes = 0
hits = pygame.sprite.spritecollide(player, helpers, True, pygame.sprite.collide_circle)
for hit in hits:
count_lifes += 1
newlife()
hits = pygame.sprite.spritecollide(player, mobs, True, pygame.sprite.collide_circle)
for hit in hits:
running = False
screen.fill(cl_black)
all_sprites.draw(screen)
draw_text(screen,"- " + str(count_lifes), 18, 35, 20)
pygame.display.flip()
pygame.quit()