import pygame, sys from pygame.locals import * import random pygame.init() SCREEN_WIDTH = 400 FPS = 60 FramePerSec = pygame.time.Clock() BLUE = (0, 0, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLACK = (0, 0, 0) WHITE = (255, 255, 255) DISPLAYSURF = pygame.display.set_mode((600,600)) DISPLAYSURF.fill(WHITE) pygame.display.set_caption("Game") class Pumpkin(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.image.load("pumpkin.png") self.rect = self.image.get_rect() self.rect.center=(x,y) self.liv = 1 def move(self, player): self.rect.move_ip(0,10) if (self.rect.bottom > 600): self.rect.top = 0 self.rect.center = (random.randint(30, 370), 0) if self.rect.colliderect(player.rect): print("HIT") def draw(self, surface): surface.blit(self.image, self.rect) class Player(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.image.load("ghost.png") self.rect = self.image.get_rect() self.rect.center = (x, y) def update(self): pressed_keys = pygame.key.get_pressed() if pressed_keys[K_UP]: self.rect.move_ip(0, -5) if pressed_keys[K_DOWN]: self.rect.move_ip(0,5) if self.rect.left > 0: if pressed_keys[K_LEFT]: self.rect.move_ip(-5, 0) if self.rect.right < SCREEN_WIDTH: if pressed_keys[K_RIGHT]: self.rect.move_ip(5, 0) def draw(self, surface): surface.blit(self.image, self.rect) def get_pos(self): return self.rect.pos P1 = Player(185, 290) list_pumpkins = [Pumpkin(185, 290), Pumpkin(330, 290)] clicks = 0 mus_nede = 0 pygame.font.init() my_font = pygame.font.SysFont('Comic Sans MS', 30) text_surface = my_font.render('Clicks: ' + str(clicks), False, (255, 255, 255)) bg = pygame.image.load("bg.png") while True: if random.randint(1, 100) == 50: for pumpkin in list_pumpkins: pumpkin.liv = 1 for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() P1.update() #E1.move(P1) #DISPLAYSURF.fill(WHITE) DISPLAYSURF.blit(bg, (0, 0)) P1.draw(DISPLAYSURF) for pumpkin in list_pumpkins: if pumpkin.liv == 1: pumpkin.draw(DISPLAYSURF) DISPLAYSURF.blit(text_surface, (0,0)) if event.type == MOUSEBUTTONDOWN: mouse_pos = event.pos # Now it will have the coordinates of click point. print('Click') for pumpkin in list_pumpkins: if pumpkin.rect.collidepoint(mouse_pos): if pumpkin.liv == 1: pumpkin.liv = 0 if mus_nede == 0: mus_nede = 1 print('Touched') clicks = clicks + 1 text_surface = my_font.render('Clicks: ' + str(clicks), False, (255, 255, 255)) elif event.type == MOUSEBUTTONUP: mus_nede = 0 pygame.display.update() FramePerSec.tick(FPS)