You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.2 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. import pygame, sys
  2. from pygame.locals import *
  3. import random
  4. pygame.init()
  5. SCREEN_WIDTH = 400
  6. FPS = 60
  7. FramePerSec = pygame.time.Clock()
  8. BLUE = (0, 0, 255)
  9. RED = (255, 0, 0)
  10. GREEN = (0, 255, 0)
  11. BLACK = (0, 0, 0)
  12. WHITE = (255, 255, 255)
  13. DISPLAYSURF = pygame.display.set_mode((600,600))
  14. DISPLAYSURF.fill(WHITE)
  15. pygame.display.set_caption("Game")
  16. class Pumpkin(pygame.sprite.Sprite):
  17. def __init__(self, x, y):
  18. super().__init__()
  19. self.image = pygame.image.load("pumpkin.png")
  20. self.rect = self.image.get_rect()
  21. self.rect.center=(x,y)
  22. self.liv = 1
  23. def move(self, player):
  24. self.rect.move_ip(0,10)
  25. if (self.rect.bottom > 600):
  26. self.rect.top = 0
  27. self.rect.center = (random.randint(30, 370), 0)
  28. if self.rect.colliderect(player.rect):
  29. print("HIT")
  30. def draw(self, surface):
  31. surface.blit(self.image, self.rect)
  32. class Player(pygame.sprite.Sprite):
  33. def __init__(self, x, y):
  34. super().__init__()
  35. self.image = pygame.image.load("ghost.png")
  36. self.rect = self.image.get_rect()
  37. self.rect.center = (x, y)
  38. def update(self):
  39. pressed_keys = pygame.key.get_pressed()
  40. if pressed_keys[K_UP]:
  41. self.rect.move_ip(0, -5)
  42. if pressed_keys[K_DOWN]:
  43. self.rect.move_ip(0,5)
  44. if self.rect.left > 0:
  45. if pressed_keys[K_LEFT]:
  46. self.rect.move_ip(-5, 0)
  47. if self.rect.right < SCREEN_WIDTH:
  48. if pressed_keys[K_RIGHT]:
  49. self.rect.move_ip(5, 0)
  50. def draw(self, surface):
  51. surface.blit(self.image, self.rect)
  52. def get_pos(self):
  53. return self.rect.pos
  54. P1 = Player(185, 290)
  55. list_pumpkins = [Pumpkin(185, 290), Pumpkin(330, 290)]
  56. clicks = 0
  57. mus_nede = 0
  58. pygame.font.init()
  59. my_font = pygame.font.SysFont('Comic Sans MS', 30)
  60. text_surface = my_font.render('Clicks: ' + str(clicks), False, (255, 255, 255))
  61. bg = pygame.image.load("bg.png")
  62. while True:
  63. if random.randint(1, 100) == 50:
  64. for pumpkin in list_pumpkins:
  65. pumpkin.liv = 1
  66. for event in pygame.event.get():
  67. if event.type == QUIT:
  68. pygame.quit()
  69. sys.exit()
  70. P1.update()
  71. #E1.move(P1)
  72. #DISPLAYSURF.fill(WHITE)
  73. DISPLAYSURF.blit(bg, (0, 0))
  74. P1.draw(DISPLAYSURF)
  75. for pumpkin in list_pumpkins:
  76. if pumpkin.liv == 1:
  77. pumpkin.draw(DISPLAYSURF)
  78. DISPLAYSURF.blit(text_surface, (0,0))
  79. if event.type == MOUSEBUTTONDOWN:
  80. mouse_pos = event.pos # Now it will have the coordinates of click point.
  81. print('Click')
  82. for pumpkin in list_pumpkins:
  83. if pumpkin.rect.collidepoint(mouse_pos):
  84. if pumpkin.liv == 1:
  85. pumpkin.liv = 0
  86. if mus_nede == 0:
  87. mus_nede = 1
  88. print('Touched')
  89. clicks = clicks + 1
  90. text_surface = my_font.render('Clicks: ' + str(clicks), False, (255, 255, 255))
  91. elif event.type == MOUSEBUTTONUP:
  92. mus_nede = 0
  93. pygame.display.update()
  94. FramePerSec.tick(FPS)