Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

135 řádky
3.7 KiB

před 2 roky
před 2 roky
před 2 roky
před 2 roky
před 2 roky
před 2 roky
před 2 roky
před 2 roky
před 2 roky
před 1 rokem
před 2 roky
před 2 roky
před 2 roky
před 1 rokem
před 1 rokem
před 2 roky
  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. explosion_frames = []
  63. for i in range(64):
  64. explosion_frames.append(pygame.image.load("explosion/exp_frame_"+str(i)+".png"))
  65. explosion_frames_nr = 0
  66. explosion_on = 0
  67. while True:
  68. if random.randint(1, 100) == 50:
  69. for pumpkin in list_pumpkins:
  70. pumpkin.liv = 1
  71. for event in pygame.event.get():
  72. if event.type == QUIT:
  73. pygame.quit()
  74. sys.exit()
  75. P1.update()
  76. #E1.move(P1)
  77. #DISPLAYSURF.fill(WHITE)
  78. DISPLAYSURF.blit(bg, (0, 0))
  79. P1.draw(DISPLAYSURF)
  80. for pumpkin in list_pumpkins:
  81. if pumpkin.liv == 1:
  82. pumpkin.draw(DISPLAYSURF)
  83. DISPLAYSURF.blit(text_surface, (0,0))
  84. if explosion_on == 1:
  85. DISPLAYSURF.blit(explosion_frames[explosion_frames_nr], (200,200))
  86. explosion_frames_nr = (explosion_frames_nr +1)
  87. if explosion_frames_nr == 64:
  88. explosion_on = 0
  89. explosion_frames_nr = 0
  90. if event.type == MOUSEBUTTONDOWN:
  91. mouse_pos = event.pos # Now it will have the coordinates of click point.
  92. print('Click')
  93. for pumpkin in list_pumpkins:
  94. if pumpkin.rect.collidepoint(mouse_pos):
  95. if pumpkin.liv == 1:
  96. pumpkin.liv = 0
  97. if mus_nede == 0:
  98. mus_nede = 1
  99. print('Touched')
  100. clicks = clicks + 1
  101. explosion_on = 1
  102. text_surface = my_font.render('Clicks: ' + str(clicks), False, (255, 255, 255))
  103. elif event.type == MOUSEBUTTONUP:
  104. mus_nede = 0
  105. pygame.display.update()
  106. FramePerSec.tick(FPS)