I'm trying to make a Python rhythm game using conditional sentences, but I can't. Please solve it.

Asked 2 years ago, Updated 2 years ago, 120 views

import pygame
import random
import time

pygame.init() # Initialize pie game
dis = pygame.display.set_mode(700, 800)) #screen size

perfect_range = 30
left_animes_x = 60
left_animes_y = 0

right_animes_x = 300
right_animes_y = 0

up_animes_x = 520
up_animes_y = 0

# Loading Images
left_arrow_off = pygame.image.load("left_arrow_off.png")
left_arrow_on = pygame.image.load("left_arrow_on.png")
left_img = left_arrow_off

# Get Size Width
left_arrow_size = left_img.get_rect().size
left_arrow_width = left_arrow_size[0]
left_arrow_height = left_arrow_size[1]

# Loading Images
right_arrow_off = pygame.image.load("right_a    rrow_off.png")
right_arrow_on = pygame.image.load("right_arrow_on.png")
right_img = right_arrow_off

# Get Size Width
right_arrow_size = right_img.get_rect().size
right_arrow_width = right_arrow_size[0]
right_arrow_height = right_arrow_size[1]

# Loading Images
up_arrow_off = pygame.image.load("up_arrow_off.png")
up_arrow_on = pygame.image.load("up_arrow_on.png")
up_img = up_arrow_off

up_arrow_size = up_img.get_rect().size
up_arrow_width = up_arrow_size[0]
up_arrow_height = up_arrow_size[1]


black = (0, 0, 0)
white = (255, 255, 255)
blue = (0, 0, 255)

# Select Title
pygame.display.set_caption('RYTHEM GAME')

game_over = False

# If the game isn't over #
   for event in pygame.event.get():
       # # print(event)

       if event.type == pygame.QUIT:
           game_over = True
       If event.type in [pygame.KEYDOWN]: Press #
           If event.key == pygame.K_a: If you pressed #a,
               left_img = left_arrow_on

           **
# If the arrow hits the enemy object,
               if 650 - perfect_range <= left_animes_y <= 650 + left_arrow_height + perfect_range and 650 - perfect_range <= left_animes_y + left_arrow_height <= 650 + left_arrow_height + perfect_range:
                   print("Perfect")
                   left_animes_y = 0
               # If the arrow hits the enemy object upward,
               elif left_animes_y <= 650 <= left_animes_y + left_arrow_height:
                   print("Up Good")
                   left_animes_y = 0
               # If the arrow hits the enemy object downward,
               elif left_animes_y <= 650 + left_arrow_height <= left_animes_y + left_arrow_height:
                   print("Down Good")
                   left_animes_y = 0
           If event.key == pygame.K_d: # Press d
               right_img = right_arrow_on # Replace image with right arrow on
               # If the arrow touched the enemy object,
               if 650 - perfect_range <= right_animes_y <= 650 + right_arrow_height + perfect_range and 650 - perfect_range <= right_animes_y
+ + right_arrow_height <= 650 + right_arrow_height + perfect_range:
                   print(right_animes_y, "Perfect")
                   # If the arrow hits the enemy object upward,
               elif right_animes_y <= 650 <= right_animes_y + right_arrow_height:
                   print(right_animes_y, "Up Good")
                   # If the arrow hits the enemy object downward,
               elif right_animes_y <= 650 + right_arrow_height <= right_animes_y + right_arrow_height:
                   print(right_animes_y, "Down Good")
           If event.key == pygame.K_s: # Press the key
               up_img = up_arrow_on # Replace image with up_arrow on
               # If the arrow touched the enemy object,
               if 650 - perfect_range <= up_animes_y <= 650
+ + up_arrow_height + perfect_range and 650 - perfect_range <= up_animes_y + up_arrow_height <= 650
+ + up_arrow_height + perfect_range:
                   print("Perfect")
                   right_animes_y = 0
               # If the arrow hits the enemy object upward,
               elif up_animes_y <= 650 <= up_animes_y + up_arrow_height:
                   print("Up Good")
                   right_animes_y = 0
               # If the arrow hits the enemy object downward,
               elif up_animes_y <= 650 + up_arrow_height <= up_animes_y
+ + up_arrow_height:
                   print("Down Good")
                   right_animes_y=0**

**

 if event.type in [pygame.KEYUP]: # If you hit the key,
           If event.key == pygame.K_a: # Key a
               left_img = left_arrow_off # Replace image with left arrow off

           If event.key == pygame.K_d: # key
               right_img = right_arrow_off # Replace image with right arrow off

           If event.key == pygame.K_s: # key
               up_img = up_arrow_off # Replace image with up_arrow_off


   dis.fill(black) # Screen color
   dis.blit(left_img, (60, 650)) # Draw the left arrow
   dis.blit(up_img, (300, 650)) #Draw the up arrow
   dis.blit(right_img, (520, 650)) # Draw right arrow

   dis.blit(left_arrow_off, (left_animes_x, left_animes_y)) # Draw an object with a left enemy
   dis.blit(up_arrow_off, (right_animes_x, right_animes_y)) # Draw an object that is the upper enemy
   dis.blit(right_arrow_off, (up_animes_x, up_animes_y)) # Draw right enemy object

   left_animes_y += 0.5 #speed
   right_animes_y += 0.5 #speed
   up_animes_y += 0.5 #Speed

   #If you cross the wall below, go up again
   if left_animes_y>800:
       left_animes_y=0

   if right_animes_y>800:
       right_animes_y=0

   if up_animes_y>800:
       up_animes_y=0

   pygame.display.update() # screen update

pygame.quit() #End pygame
Quit() # Finish

python conditional-statement

2022-09-19 23:33

1 Answers

The comments have become too long, so I inevitably write them as answers.

if 650 - perfect_range <= left_animes_y <= 650 + left_arrow_height + perfect_range and 650 - perfect_range <= left_animes_y + left_arrow_height <= 650 + left_arrow_height + perfect_range:

Let me give you an example of a conditional statement.

in conclusion if 650 - perfect_range <= left_animes_y <= 650 + left_arrow_height + perfect_range and 650 - perfect_range <= left_animes_y + left_arrow_height <= 650 + left_arrow_height + perfect_range: I think we can reduce the condition to if 650 - perfect_range <= left_animes_y <= 650 + perfect_range:.


2022-09-19 23:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.