Advertisement
IMustRemainUnknown

Zaki

Feb 11th, 2024
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.36 KB | None | 0 0
  1. import pygame
  2. import sys
  3. import random
  4.  
  5. def battle_capacitor():
  6. # Initialize Pygame
  7. pygame.init()
  8.  
  9. # Set up the window
  10. WINDOW_WIDTH, WINDOW_HEIGHT = 1280, 720
  11. window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
  12. pygame.display.set_caption("Leonel Gods")
  13. clock = pygame.time.Clock()
  14.  
  15. # Load and scale background image
  16. background_image = pygame.image.load("assets/sprites/background.png").convert_alpha()
  17. background_image = pygame.transform.scale(background_image, (WINDOW_WIDTH, WINDOW_HEIGHT))
  18.  
  19.  
  20. # Load and scale heart image
  21. heart_image = pygame.image.load("assets/sprites/heart.png").convert_alpha()
  22. heart_image = pygame.transform.scale(heart_image, (40, 40)) # Adjust size as needed
  23.  
  24. # Load and scale sprite images
  25. new_cursor = pygame.image.load("assets/sprites/01.png").convert_alpha()
  26. pygame.mouse.set_visible(False)
  27. dialogue_image = pygame.image.load("assets/sprites/dialogue.png").convert_alpha()
  28. dialogue_image = pygame.transform.scale(dialogue_image, (1475, 280))
  29.  
  30. # Load and scale sprite images
  31. player_sprite = pygame.image.load("assets/sprites/player_sprite.png").convert_alpha()
  32. enemy_sprite = pygame.image.load("assets/sprites/enemy_sprite_frog1.png").convert_alpha()
  33.  
  34. # Scale sprite images
  35. sprite_width, sprite_height = 250, 250
  36. player_sprite = pygame.transform.scale(player_sprite, (sprite_width, sprite_height))
  37. # = pygame.transform.scale(enemy_sprite, (sprite_width + 50, sprite_height + 50))
  38. # Load and play music
  39. pygame.mixer.music.load("assets/music/background_music.mp3")
  40. pygame.mixer.music.set_volume(0.5)
  41. pygame.mixer.music.play(-1) # Play in a loop
  42.  
  43. # Load sound effects
  44. button_click_sound = pygame.mixer.Sound("assets/sounds/button_click.mp3")
  45. attack_sound = pygame.mixer.Sound("assets/sounds/attack.mp3")
  46. damage_sound = pygame.mixer.Sound("assets/sounds/damage.mp3")
  47. notif_sound = pygame.mixer.Sound("assets/sounds/notification.wav")
  48. over_sound = pygame.mixer.Sound("assets/sounds/gameover.wav")
  49. click_sound = pygame.mixer.Sound("assets/sounds/click.wav")
  50. pygame.mixer.Sound.set_volume(click_sound, 0.5)
  51. pygame.mixer.Sound.set_volume(damage_sound, 0.5)
  52. pygame.mixer.Sound.set_volume(attack_sound, 5)
  53. # Reward Sprite
  54. reward = pygame.image.load('assets/sprites/Capacitor.png')
  55.  
  56. # Define colors and fonts
  57. colors = {'white': (255, 255, 255), 'black': (0, 0, 0), 'gray': (200, 200, 200),
  58. 'red': (255, 0, 0), 'green': '#228b22', 'blue': (0, 0, 255), 'orange': '#fe6f5e'}
  59. font = pygame.font.Font('assets/ngger.ttf', 28)
  60.  
  61.  
  62. # Game variables
  63. player_hp, enemy_hp = 3, 3
  64. dialogues = ["A wild monster appeared!", "The monster has seized the Capacitor! ", "Beware, for it now wields the power of storing against you."]
  65. current_dialogue = 0
  66. question_answered = False
  67. game_ended = False
  68. game_lost = False # New variable to track if the game is lost
  69.  
  70. # Define paths for button images
  71. button_image_normal_path = "assets/sprites/Button_Blue_3Slides.png"
  72. button_image_pressed_path = "assets/sprites/Button_Blue_3Slides_Pressed.png"
  73.  
  74. # Load button images
  75. button_image_normal = pygame.image.load(button_image_normal_path).convert_alpha()
  76. button_image_pressed = pygame.image.load(button_image_pressed_path).convert_alpha()
  77.  
  78. # Scale button images
  79. button_width, button_height = 200, 80
  80. button_x, button_y, button_spacing = 100, 180, 40
  81. button_image_normal = pygame.transform.scale(button_image_normal, (button_width, button_height))
  82. button_image_pressed = pygame.transform.scale(button_image_pressed, (button_width, button_height))
  83.  
  84. questions = [
  85. {
  86. "question": "What stores electric charge in a circuit?",
  87. "choices": ["Capacitor", "Transistor", "Diode"],
  88. "correct_answer": "Capacitor"
  89. },
  90. {
  91. "question": "What type of capacitor can’t hold a charge?",
  92. "choices": ['Electrolytic', 'Ceramic', 'Mylar'],
  93. "correct_answer": "Electrolytic"
  94. },
  95. {
  96. "question": "What unit measures capacitance?",
  97. "choices": ["Ampere", "Ohms", "Farad"],
  98. "correct_answer": "Farad"
  99. },
  100. {
  101. "question": "What decreases the capacitance of a parallel plate capacitor?",
  102. "choices": ["Distance", "Volume", "None"],
  103. "correct_answer": "Distance"
  104. },
  105. {
  106. "question": "What type of capacitor has a specific positive and negative terminal?",
  107. "choices": ["Negatron", "Polarized", "None"],
  108. "correct_answer": "Polarized"
  109. }
  110. ]
  111.  
  112. current_question_index = 0
  113.  
  114.  
  115.  
  116. class Enemy(pygame.sprite.Sprite):
  117. def __init__(self, pos_x, pos_y):
  118. super().__init__()
  119. self.attack_animation = False
  120. self.sprites = []
  121. sprite_paths = ['assets/sprites/frog1_1.png', 'assets/sprites/frog1_2.png', 'assets/sprites/frog1_3.png', 'assets/sprites/frog1_4.png']
  122. for path in sprite_paths:
  123. sprite = pygame.image.load(path)
  124. scaled_sprite = pygame.transform.scale(sprite, (320, 320)) # Resize each frame of the sprite animation
  125. self.sprites.append(scaled_sprite)
  126.  
  127. self.current_sprite = 0
  128. self.image = self.sprites[self.current_sprite]
  129. self.rect = self.image.get_rect(topleft=(pos_x, pos_y))
  130. self.animation_timer = pygame.time.get_ticks() # Timer for animation
  131.  
  132. def update(self):
  133. # Set animation speed (milliseconds per frame)
  134. animation_speed = 200 # Adjust as needed for desired animation speed
  135. # Check if it's time to update the sprite image
  136. if pygame.time.get_ticks() - self.animation_timer >= animation_speed:
  137. self.animation_timer = pygame.time.get_ticks() # Reset timer
  138. self.current_sprite = (self.current_sprite + 1) % len(self.sprites) # Loop through sprite list
  139. self.image = self.sprites[self.current_sprite]
  140.  
  141.  
  142. moving_sprites = pygame.sprite.Group()
  143. enemy = Enemy(550, 205)
  144. moving_sprites.add(enemy)
  145.  
  146.  
  147.  
  148. class Button:
  149. def __init__(self, text, x, y, width, height, image_normal, image_pressed, font, font_size,
  150. clickable=True): # Add font properties
  151. self.text = text
  152. self.rect = pygame.Rect(x, y, width, height)
  153. self.image_normal = image_normal
  154. self.image_pressed = image_pressed
  155. self.font = pygame.font.Font(font, font_size) # Load font
  156. self.text_y = y + height // 2.4 # Adjust the vertical position of the text
  157. self.clickable = clickable # Store clickable state
  158.  
  159. def draw(self, surface):
  160. mouse = pygame.mouse.get_pos()
  161. click = pygame.mouse.get_pressed()
  162.  
  163. if self.rect.collidepoint(mouse):
  164. surface.blit(self.image_pressed, self.rect)
  165. if click[0] == 1 and self.clickable: # Check if clickable
  166. return True, self.text
  167. else:
  168. surface.blit(self.image_normal, self.rect)
  169.  
  170. text_surface = self.font.render(self.text, True, colors['black']) # Use custom font
  171. text_rect = text_surface.get_rect(center=(self.rect.centerx, self.text_y))
  172. surface.blit(text_surface, text_rect)
  173.  
  174. return False, None
  175.  
  176. def display_text(text, x, y, color='black'):
  177. text_surface = font.render(text, True, colors[color])
  178. text_rect = text_surface.get_rect(center=(x, y))
  179. window.blit(text_surface, text_rect)
  180.  
  181.  
  182. # Initialize variables to track sound playing
  183. notification_sound_played = False
  184. over_sound_played = False
  185.  
  186. running = True
  187. while running:
  188. window.blit(background_image, (0, 0))
  189. window.blit(player_sprite,(250,350))
  190.  
  191. enemy.update() # Update enemy animation continuously
  192. moving_sprites.draw(window)
  193. moving_sprites.update()
  194. # pygame.display.flip()
  195. # clock.tick(60)
  196.  
  197. if current_dialogue < len(dialogues) and not game_ended and not game_lost:
  198. # Display dialogues
  199. box_height = 175
  200. box_y = WINDOW_HEIGHT - box_height
  201. pygame.draw.rect(window, colors['gray'], (0, box_y, WINDOW_WIDTH, box_height))
  202. window.blit(dialogue_image, (-100, WINDOW_HEIGHT - box_height - 55))
  203. display_text(dialogues[current_dialogue], WINDOW_WIDTH // 2, WINDOW_HEIGHT - box_height // 2, 'black')
  204.  
  205. if pygame.mouse.get_pressed()[0]:
  206. click_sound.play(0)
  207. current_dialogue += 1
  208. pygame.time.wait(300)
  209. elif current_question_index < len(questions) and not question_answered and not game_ended and not game_lost:
  210. # Display questions and choices
  211. question = questions[current_question_index]
  212. box_height = 175
  213. box_y = WINDOW_HEIGHT - box_height
  214. pygame.draw.rect(window, colors['gray'], (0, box_y, WINDOW_WIDTH, box_height))
  215. window.blit(dialogue_image, (-100, WINDOW_HEIGHT - box_height - 55))
  216. display_text(question["question"], WINDOW_WIDTH // 2, WINDOW_HEIGHT - box_height // 2, 'black')
  217.  
  218. # Create buttons for choices
  219. buttons = []
  220. for i, choice in enumerate(question["choices"]):
  221. button = Button(choice, button_x, button_y + i * (button_height + button_spacing),
  222. button_width, button_height, button_image_normal, button_image_pressed,
  223. 'assets/ngger.ttf', 28) # Use font properties directly
  224. buttons.append(button)
  225.  
  226. # Check for button clicks
  227. for button in buttons:
  228. clicked, choice = button.draw(window)
  229. if clicked:
  230. button_click_sound.play()
  231. pygame.time.wait(300)
  232. question_answered = True
  233. if choice == question["correct_answer"]:
  234. enemy_hp -= 1
  235. attack_sound.play(0)
  236.  
  237. else:
  238. player_hp -= 1
  239. damage_sound.play(0)
  240.  
  241. pygame.time.wait(300)
  242.  
  243. elif question_answered and not game_ended and not game_lost:
  244. current_question_index += 1
  245. question_answered = False
  246.  
  247. if current_question_index >= len(questions) or player_hp <= 0 or enemy_hp <= 0:
  248. # All questions answered or game ended
  249. if enemy_hp <= 0:
  250. dialogues.append("You win! Congratulations.")
  251. game_ended = True
  252. elif player_hp <= 0:
  253. dialogues.append("You lose ! Game over.")
  254. game_lost = True # Set game_lost to True if player loses
  255. game_ended = True
  256. sys.exit()
  257. else:
  258. dialogues.append("Game over.")
  259. game_ended = True
  260.  
  261. elif game_ended:
  262. # Display the last dialogue
  263. if current_dialogue < len(dialogues):
  264. box_height = 175
  265. box_y = WINDOW_HEIGHT - box_height
  266. pygame.draw.rect(window, colors['gray'], (0, box_y, WINDOW_WIDTH, box_height))
  267. window.blit(dialogue_image, (-100, WINDOW_HEIGHT - box_height - 55))
  268. display_text(dialogues[current_dialogue], WINDOW_WIDTH // 2, WINDOW_HEIGHT - box_height // 2, 'black')
  269.  
  270. if pygame.mouse.get_pressed()[0]:
  271. if not game_lost:
  272. if not notification_sound_played:
  273. notif_sound.play(0)
  274. notification_sound_played = True
  275. else:
  276. if not over_sound_played:
  277. over_sound.play(0)
  278. over_sound_played = True
  279. current_dialogue += 1
  280. pygame.time.wait(300)
  281. else:
  282. # Display the reward sprite and wait for user to exit (if game is won)
  283. if not game_lost:
  284. window.blit(reward, (525, 200))
  285. window.blit(dialogue_image, (-100, WINDOW_HEIGHT - box_height - 55))
  286. display_text(f"Behold your triumph: a Capacitor, now yours!", WINDOW_WIDTH // 2, WINDOW_HEIGHT // 1.15,
  287. 'green')
  288. for event in pygame.event.get():
  289. if event.type == pygame.QUIT:
  290. running = False
  291. elif event.type == pygame.MOUSEBUTTONDOWN:
  292. running = False
  293.  
  294. pos = pygame.mouse.get_pos()
  295. window.blit(new_cursor, pos)
  296.  
  297. # Display HP
  298. for i in range(player_hp):
  299. window.blit(heart_image, (100 + i * 50, 50))
  300.  
  301. for i in range(enemy_hp):
  302. window.blit(heart_image, (WINDOW_WIDTH - (150 + i * 50), 50))
  303.  
  304. # Event handling
  305. for event in pygame.event.get():
  306. if event.type == pygame.QUIT:
  307. running = False
  308.  
  309.  
  310.  
  311. pygame.display.update()
  312. pygame.quit()
  313.  
  314. def battle_voltmeter():
  315. # Initialize Pygame
  316. pygame.init()
  317.  
  318. # Set up the window
  319. WINDOW_WIDTH, WINDOW_HEIGHT = 1280, 720
  320. window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
  321. pygame.display.set_caption("Leonel Gods")
  322. clock = pygame.time.Clock()
  323.  
  324. # Load and scale background image
  325. background_image = pygame.image.load("assets/sprites/background.png").convert_alpha()
  326. background_image = pygame.transform.scale(background_image, (WINDOW_WIDTH, WINDOW_HEIGHT))
  327.  
  328.  
  329. # Load and scale heart image
  330. heart_image = pygame.image.load("assets/sprites/heart.png").convert_alpha()
  331. heart_image = pygame.transform.scale(heart_image, (40, 40)) # Adjust size as needed
  332.  
  333. # Load and scale sprite images
  334. new_cursor = pygame.image.load("assets/sprites/01.png").convert_alpha()
  335. pygame.mouse.set_visible(False)
  336. dialogue_image = pygame.image.load("assets/sprites/dialogue.png").convert_alpha()
  337. dialogue_image = pygame.transform.scale(dialogue_image, (1475, 280))
  338.  
  339. # Load and scale sprite images
  340. player_sprite = pygame.image.load("assets/sprites/player_sprite.png").convert_alpha()
  341. enemy_sprite = pygame.image.load("assets/sprites/enemy_sprite_fire.png").convert_alpha()
  342.  
  343. # Scale sprite images
  344. sprite_width, sprite_height = 250, 250
  345. player_sprite = pygame.transform.scale(player_sprite, (sprite_width, sprite_height))
  346. # = pygame.transform.scale(enemy_sprite, (sprite_width + 50, sprite_height + 50))
  347. # Load and play music
  348. pygame.mixer.music.load("assets/music/background_music.mp3")
  349. pygame.mixer.music.set_volume(0.5)
  350. pygame.mixer.music.play(-1) # Play in a loop
  351.  
  352. # Load sound effects
  353. button_click_sound = pygame.mixer.Sound("assets/sounds/button_click.mp3")
  354. attack_sound = pygame.mixer.Sound("assets/sounds/attack.mp3")
  355. damage_sound = pygame.mixer.Sound("assets/sounds/damage.mp3")
  356. notif_sound = pygame.mixer.Sound("assets/sounds/notification.wav")
  357. over_sound = pygame.mixer.Sound("assets/sounds/gameover.wav")
  358. click_sound = pygame.mixer.Sound("assets/sounds/click.wav")
  359. pygame.mixer.Sound.set_volume(click_sound, 0.5)
  360. pygame.mixer.Sound.set_volume(damage_sound, 0.5)
  361. pygame.mixer.Sound.set_volume(attack_sound, 5)
  362. # Reward Sprite
  363. reward = pygame.image.load('assets/sprites/voltmeter.png')
  364.  
  365. # Define colors and fonts
  366. colors = {'white': (255, 255, 255), 'black': (0, 0, 0), 'gray': (200, 200, 200),
  367. 'red': (255, 0, 0), 'green': '#228b22', 'blue': (0, 0, 255), 'orange': '#fe6f5e'}
  368. font = pygame.font.Font('assets/ngger.ttf', 28)
  369.  
  370.  
  371. # Game variables
  372. player_hp, enemy_hp = 3, 3
  373. dialogues = ["A wild monster appeared!", "The monster has seized the voltmeter! ", "Beware, for it now wields the power of volts against you."]
  374. current_dialogue = 0
  375. question_answered = False
  376. game_ended = False
  377. game_lost = False # New variable to track if the game is lost
  378.  
  379. # Define paths for button images
  380. button_image_normal_path = "assets/sprites/Button_Blue_3Slides.png"
  381. button_image_pressed_path = "assets/sprites/Button_Blue_3Slides_Pressed.png"
  382.  
  383. # Load button images
  384. button_image_normal = pygame.image.load(button_image_normal_path).convert_alpha()
  385. button_image_pressed = pygame.image.load(button_image_pressed_path).convert_alpha()
  386.  
  387. # Scale button images
  388. button_width, button_height = 200, 80
  389. button_x, button_y, button_spacing = 100, 180, 40
  390. button_image_normal = pygame.transform.scale(button_image_normal, (button_width, button_height))
  391. button_image_pressed = pygame.transform.scale(button_image_pressed, (button_width, button_height))
  392.  
  393. questions = [
  394. {
  395. "question": "What electrical quantity does a voltmeter measure?",
  396. "choices": ["Voltage", "Ohms", "Ampere"],
  397. "correct_answer": "Voltage"
  398. },
  399. {
  400. "question": "What are the typical units used to express voltmeter readings?",
  401. "choices": ['Ampere', 'Ohms', 'Volts'],
  402. "correct_answer": "Volts"
  403. },
  404. {
  405. "question": "What type of circuit element are most voltmeters?",
  406. "choices": ["Active", "Dynamic", "Passive"],
  407. "correct_answer": "Passive"
  408. },
  409. {
  410. "question": "What type of current do most voltmeters measure?",
  411. "choices": ["DC only", "AC only", "DC and AC"],
  412. "correct_answer": "DC and AC"
  413. },
  414. {
  415. "question": "What is the typical connection method for a voltmeter in a circuit?",
  416. "choices": ["Series", "Parallel", "Horizontal"],
  417. "correct_answer": "Parallel"
  418. }
  419. ]
  420.  
  421. current_question_index = 0
  422.  
  423. class Enemy(pygame.sprite.Sprite):
  424. def __init__(self, pos_x, pos_y):
  425. super().__init__()
  426. self.attack_animation = False
  427. self.sprites = []
  428. sprite_paths = ['assets/sprites/fire_1.png', 'assets/sprites/fire_2.png', 'assets/sprites/fire_3.png', 'assets/sprites/fire_4.png']
  429. for path in sprite_paths:
  430. sprite = pygame.image.load(path)
  431. scaled_sprite = pygame.transform.scale(sprite, (320, 320)) # Resize each frame of the sprite animation
  432. self.sprites.append(scaled_sprite)
  433.  
  434. self.current_sprite = 0
  435. self.image = self.sprites[self.current_sprite]
  436. self.rect = self.image.get_rect(topleft=(pos_x, pos_y))
  437. self.animation_timer = pygame.time.get_ticks() # Timer for animation
  438.  
  439. def update(self):
  440. # Set animation speed (milliseconds per frame)
  441. animation_speed = 200 # Adjust as needed for desired animation speed
  442. # Check if it's time to update the sprite image
  443. if pygame.time.get_ticks() - self.animation_timer >= animation_speed:
  444. self.animation_timer = pygame.time.get_ticks() # Reset timer
  445. self.current_sprite = (self.current_sprite + 1) % len(self.sprites) # Loop through sprite list
  446. self.image = self.sprites[self.current_sprite]
  447.  
  448.  
  449. moving_sprites = pygame.sprite.Group()
  450. enemy = Enemy(550, 205)
  451. moving_sprites.add(enemy)
  452.  
  453.  
  454.  
  455. class Button:
  456. def __init__(self, text, x, y, width, height, image_normal, image_pressed, font, font_size,
  457. clickable=True): # Add font properties
  458. self.text = text
  459. self.rect = pygame.Rect(x, y, width, height)
  460. self.image_normal = image_normal
  461. self.image_pressed = image_pressed
  462. self.font = pygame.font.Font(font, font_size) # Load font
  463. self.text_y = y + height // 2.4 # Adjust the vertical position of the text
  464. self.clickable = clickable # Store clickable state
  465.  
  466. def draw(self, surface):
  467. mouse = pygame.mouse.get_pos()
  468. click = pygame.mouse.get_pressed()
  469.  
  470. if self.rect.collidepoint(mouse):
  471. surface.blit(self.image_pressed, self.rect)
  472. if click[0] == 1 and self.clickable: # Check if clickable
  473. return True, self.text
  474. else:
  475. surface.blit(self.image_normal, self.rect)
  476.  
  477. text_surface = self.font.render(self.text, True, colors['black']) # Use custom font
  478. text_rect = text_surface.get_rect(center=(self.rect.centerx, self.text_y))
  479. surface.blit(text_surface, text_rect)
  480.  
  481. return False, None
  482.  
  483. def display_text(text, x, y, color='black'):
  484. text_surface = font.render(text, True, colors[color])
  485. text_rect = text_surface.get_rect(center=(x, y))
  486. window.blit(text_surface, text_rect)
  487.  
  488.  
  489. # Initialize variables to track sound playing
  490. notification_sound_played = False
  491. over_sound_played = False
  492.  
  493. running = True
  494. while running:
  495. window.blit(background_image, (0, 0))
  496. window.blit(player_sprite,(250,350))
  497.  
  498. enemy.update() # Update enemy animation continuously
  499. moving_sprites.draw(window)
  500. moving_sprites.update()
  501. # pygame.display.flip()
  502. # clock.tick(60)
  503.  
  504. if current_dialogue < len(dialogues) and not game_ended and not game_lost:
  505. # Display dialogues
  506. box_height = 175
  507. box_y = WINDOW_HEIGHT - box_height
  508. pygame.draw.rect(window, colors['gray'], (0, box_y, WINDOW_WIDTH, box_height))
  509. window.blit(dialogue_image, (-100, WINDOW_HEIGHT - box_height - 55))
  510. display_text(dialogues[current_dialogue], WINDOW_WIDTH // 2, WINDOW_HEIGHT - box_height // 2, 'black')
  511.  
  512. if pygame.mouse.get_pressed()[0]:
  513. click_sound.play(0)
  514. current_dialogue += 1
  515. pygame.time.wait(300)
  516. elif current_question_index < len(questions) and not question_answered and not game_ended and not game_lost:
  517. # Display questions and choices
  518. question = questions[current_question_index]
  519. box_height = 175
  520. box_y = WINDOW_HEIGHT - box_height
  521. pygame.draw.rect(window, colors['gray'], (0, box_y, WINDOW_WIDTH, box_height))
  522. window.blit(dialogue_image, (-100, WINDOW_HEIGHT - box_height - 55))
  523. display_text(question["question"], WINDOW_WIDTH // 2, WINDOW_HEIGHT - box_height // 2, 'black')
  524.  
  525. # Create buttons for choices
  526. buttons = []
  527. for i, choice in enumerate(question["choices"]):
  528. button = Button(choice, button_x, button_y + i * (button_height + button_spacing),
  529. button_width, button_height, button_image_normal, button_image_pressed,
  530. 'assets/ngger.ttf', 28) # Use font properties directly
  531. buttons.append(button)
  532.  
  533. # Check for button clicks
  534. for button in buttons:
  535. clicked, choice = button.draw(window)
  536. if clicked:
  537. button_click_sound.play()
  538. pygame.time.wait(300)
  539. question_answered = True
  540. if choice == question["correct_answer"]:
  541. enemy_hp -= 1
  542. attack_sound.play(0)
  543.  
  544. else:
  545. player_hp -= 1
  546. damage_sound.play(0)
  547.  
  548. pygame.time.wait(300)
  549.  
  550. elif question_answered and not game_ended and not game_lost:
  551. current_question_index += 1
  552. question_answered = False
  553.  
  554. if current_question_index >= len(questions) or player_hp <= 0 or enemy_hp <= 0:
  555. # All questions answered or game ended
  556. if enemy_hp <= 0:
  557. dialogues.append("You win! Congratulations.")
  558. game_ended = True
  559. elif player_hp <= 0:
  560. dialogues.append("You lose ! Game over.")
  561. game_lost = True # Set game_lost to True if player loses
  562. game_ended = True
  563. sys.exit()
  564.  
  565. else:
  566. dialogues.append("Game over.")
  567. game_ended = True
  568.  
  569. elif game_ended:
  570. # Display the last dialogue
  571. if current_dialogue < len(dialogues):
  572. box_height = 175
  573. box_y = WINDOW_HEIGHT - box_height
  574. pygame.draw.rect(window, colors['gray'], (0, box_y, WINDOW_WIDTH, box_height))
  575. window.blit(dialogue_image, (-100, WINDOW_HEIGHT - box_height - 55))
  576. display_text(dialogues[current_dialogue], WINDOW_WIDTH // 2, WINDOW_HEIGHT - box_height // 2, 'black')
  577.  
  578. if pygame.mouse.get_pressed()[0]:
  579. if not game_lost:
  580. if not notification_sound_played:
  581. notif_sound.play(0)
  582. notification_sound_played = True
  583. else:
  584. if not over_sound_played:
  585. over_sound.play(0)
  586. over_sound_played = True
  587. current_dialogue += 1
  588. pygame.time.wait(300)
  589. else:
  590. # Display the reward sprite and wait for user to exit (if game is won)
  591. if not game_lost:
  592. window.blit(reward, (525, 200))
  593. window.blit(dialogue_image, (-100, WINDOW_HEIGHT - box_height - 55))
  594. display_text(f"Behold your triumph: a Voltmeter, now yours!", WINDOW_WIDTH // 2, WINDOW_HEIGHT // 1.15,
  595. 'green')
  596. for event in pygame.event.get():
  597. if event.type == pygame.QUIT:
  598. running = False
  599. elif event.type == pygame.MOUSEBUTTONDOWN:
  600. running = False
  601.  
  602. pos = pygame.mouse.get_pos()
  603. window.blit(new_cursor, pos)
  604.  
  605. # Display HP
  606. for i in range(player_hp):
  607. window.blit(heart_image, (100 + i * 50, 50))
  608.  
  609. for i in range(enemy_hp):
  610. window.blit(heart_image, (WINDOW_WIDTH - (150 + i * 50), 50))
  611.  
  612. # Event handling
  613. for event in pygame.event.get():
  614. if event.type == pygame.QUIT:
  615. running = False
  616.  
  617.  
  618.  
  619. pygame.display.update()
  620. pygame.quit()
  621.  
  622. class Game:
  623. def __init__(self):
  624. # Initialize Pygame
  625. pygame.init()
  626.  
  627. # Set up screen dimensions
  628. self.screen_width = 800
  629. self.screen_height = 600
  630. self.screen = pygame.display.set_mode((self.screen_width, self.screen_height))
  631. pygame.display.set_caption("Basic Movement")
  632.  
  633. # Load background image
  634. try:
  635. self.background_image = pygame.image.load('background_image.png') # Replace 'background_image.jpg' with your image file
  636. self.background_image = pygame.transform.scale(self.background_image, (self.screen_width*2, self.screen_height*2))
  637. self.background_rect = self.background_image.get_rect()
  638. except pygame.error as e:
  639. print("Error loading background image:", e)
  640. pygame.quit()
  641. sys.exit()
  642.  
  643. # Set up player properties
  644. try:
  645. self.player_image = pygame.image.load('character_sprite.png') # Load your character sprite image
  646. self.player_rect = self.player_image.get_rect(center=(self.screen_width // 2, self.screen_height // 2))
  647. self.player_speed = 5
  648. except pygame.error as e:
  649. print("Error loading player image:", e)
  650. pygame.quit()
  651. sys.exit()
  652.  
  653. # Set up enemy properties
  654. try:
  655. self.enemy_images = [
  656. pygame.transform.scale(pygame.image.load('enemy_sprite_1.png'), (128, 128)), # Load and resize different enemy sprite images
  657. pygame.transform.scale(pygame.image.load('enemy_sprite_2.png'), (128, 128)),
  658. pygame.transform.scale(pygame.image.load('enemy_sprite_3.png'), (128, 128)),
  659. # Add more images as needed
  660. ]
  661. self.enemies = []
  662. for _ in range(3): # Add 7 enemies (including the initial one)
  663. enemy_image = self.enemy_images[_]
  664. enemy_rect = enemy_image.get_rect(center=(random.randint(0, self.background_rect.width), random.randint(0, self.background_rect.height)))
  665. enemy_name = f"Enemy {len(self.enemies) + 1}"
  666. self.enemies.append((enemy_image, enemy_rect, enemy_name))
  667. except pygame.error as e:
  668. print("Error loading enemy image:", e)
  669. pygame.quit()
  670. sys.exit()
  671.  
  672. # Game over font
  673. self.game_over_font = pygame.font.Font(None, 64)
  674.  
  675. self.clock = pygame.time.Clock()
  676. self.game_over = False
  677.  
  678. def run(self):
  679. # Main game loop
  680. running = True
  681. while running:
  682. self.screen.fill((255, 255, 255))
  683.  
  684. # Handle events
  685. for event in pygame.event.get():
  686. if event.type == pygame.QUIT:
  687. running = False
  688.  
  689. if not self.game_over:
  690. # Handle player movement
  691. keys = pygame.key.get_pressed()
  692. if keys[pygame.K_w]:
  693. self.player_rect.y -= self.player_speed
  694. if keys[pygame.K_s]:
  695. self.player_rect.y += self.player_speed
  696. if keys[pygame.K_a]:
  697. self.player_rect.x -= self.player_speed
  698. if keys[pygame.K_d]:
  699. self.player_rect.x += self.player_speed
  700.  
  701. # Update camera position to follow player
  702. camera = pygame.Rect(0, 0, self.screen_width, self.screen_height)
  703. camera.centerx = self.player_rect.centerx
  704. camera.centery = self.player_rect.centery
  705.  
  706. # Ensure the camera stays within the bounds of the background
  707. camera.left = max(camera.left, 0)
  708. camera.top = max(camera.top, 0)
  709. camera.right = min(camera.right, self.background_rect.width)
  710. camera.bottom = min(camera.bottom, self.background_rect.height)
  711.  
  712. # Draw background
  713. self.screen.blit(self.background_image, (0, 0), camera)
  714.  
  715. # Draw player sprite in the center of the screen
  716. self.screen.blit(self.player_image, (self.screen_width // 2 - self.player_rect.width // 2, self.screen_height // 2 - self.player_rect.height // 2))
  717.  
  718. # Draw enemies
  719. updated_enemies = []
  720. for enemy_image, enemy_rect, enemy_name in self.enemies:
  721. self.screen.blit(enemy_image, (enemy_rect.x - camera.x, enemy_rect.y - camera.y))
  722. if self.player_rect.colliderect(enemy_rect):
  723. print(f"Player collided with {enemy_name}!")
  724. if enemy_name == "Enemy 1":
  725. battle_voltmeter()
  726. elif enemy_name == "Enemy 2":
  727. battle_voltmeter()
  728. else:
  729. updated_enemies.append((enemy_image, enemy_rect, enemy_name))
  730.  
  731. self.enemies = updated_enemies # Update enemies list
  732. else:
  733. # Game over text
  734. game_over_text = self.game_over_font.render("Game Over", True, (255, 0, 0))
  735. self.screen.blit(game_over_text, (self.screen_width // 2 - game_over_text.get_width() // 2, self.screen_height // 2 - game_over_text.get_height() // 2))
  736.  
  737. # Update the display
  738. pygame.display.update()
  739.  
  740. # Cap the frame rate
  741. self.clock.tick(60)
  742.  
  743. # Quit Pygame
  744. pygame.quit()
  745. sys.exit()
  746.  
  747. if __name__ == "__main__":
  748. game = Game()
  749. game.run()
  750.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement