|
| 1 | +import pygame |
| 2 | +import utility as util |
| 3 | + |
| 4 | +# for displaying health we need text |
| 5 | +pygame.font.init() |
| 6 | + |
| 7 | +# width, height = 900, 500 |
| 8 | +WINDOW = pygame.display.set_mode((util.width, util.height)) |
| 9 | +pygame.display.set_caption("Spaceship War Game") |
| 10 | + |
| 11 | +# Creating user event so that we can came to know if the bullet collides, diff number indicates diff events |
| 12 | +YELLOW_HIT = pygame.USEREVENT + 1 |
| 13 | +RED_HIT = pygame.USEREVENT + 2 |
| 14 | + |
| 15 | + |
| 16 | +def drawWindow(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health, |
| 17 | + YELLOW_SPACESHIP, RED_SPACESHIP, SPACE, BORDER): |
| 18 | + """ |
| 19 | + This functions gives displays the graphics of the Game which includes |
| 20 | + both the spaceship, background space image, bullets, and health of players |
| 21 | + with 60 Frames per second. |
| 22 | + :param red: Bounding rectangle of Red Spaceship |
| 23 | + :param yellow: Bounding rectangle of Yellow Spaceship |
| 24 | + :param red_bullets: Bullets fired by red spaceship |
| 25 | + :param yellow_bullets: Bullets fired by Yellow spaceship |
| 26 | + :param red_health: Health of Red Player |
| 27 | + :param yellow_health: Health of Yellow Player |
| 28 | + :param YELLOW_SPACESHIP: Red Spaceship Image |
| 29 | + :param RED_SPACESHIP: Yellow Spaceship Image |
| 30 | + :param SPACE: Space Background Image |
| 31 | + :param BORDER: Center border |
| 32 | + """ |
| 33 | + # ORDER IN WHICH DRAW THINGS MATTER REMEMBER |
| 34 | + WINDOW.blit(SPACE, (0, 0)) |
| 35 | + |
| 36 | + # Adding border created above |
| 37 | + pygame.draw.rect(WINDOW, (255, 255, 255), BORDER) |
| 38 | + |
| 39 | + # to indicate health |
| 40 | + HEALTH_FONT = pygame.font.SysFont('comicsans', 40) |
| 41 | + |
| 42 | + # Displaying Health by font |
| 43 | + red_health_text = HEALTH_FONT.render("Health: " + str(red_health), True, (255, 255, 255)) |
| 44 | + yellow_health_text = HEALTH_FONT.render("Health: " + str(yellow_health), True, (255, 255, 255)) |
| 45 | + WINDOW.blit(red_health_text, (util.width - red_health_text.get_width() - 10, 10)) |
| 46 | + WINDOW.blit(yellow_health_text, (10, 10)) |
| 47 | + |
| 48 | + # to load surfaces we use blit |
| 49 | + WINDOW.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y)) |
| 50 | + |
| 51 | + WINDOW.blit(RED_SPACESHIP, (red.x, red.y)) |
| 52 | + |
| 53 | + # Drawing bullets |
| 54 | + for bullet in red_bullets: |
| 55 | + pygame.draw.rect(WINDOW, (255, 0, 0), bullet) |
| 56 | + |
| 57 | + for bullet in yellow_bullets: |
| 58 | + pygame.draw.rect(WINDOW, (255, 255, 0), bullet) |
| 59 | + |
| 60 | + pygame.display.update() |
| 61 | + |
| 62 | + |
| 63 | +def handle_bullets(yellow_bullets, red_bullets, yellow, red): |
| 64 | + """ |
| 65 | + This function moves the bullet forward with a specific Bullet velocity |
| 66 | + and at a time only 3 bullets can be fired by the user. |
| 67 | + It also checks weather the bullet has hit the spaceship by using collidirect |
| 68 | + function, so that we can decrease the health of that player. |
| 69 | + :param yellow_bullets: Bullets fired by red spaceship |
| 70 | + :param red_bullets: Bullets fired by red spaceship |
| 71 | + :param yellow: Bounding rectangle of Yellow Spaceship |
| 72 | + :param red: Bounding rectangle of Red Spaceship |
| 73 | + """ |
| 74 | + # Bullet velocity |
| 75 | + BULLET_VEL = 7 |
| 76 | + |
| 77 | + # To check weather the bullet hit red or yellow spaceship or they fly through the skin |
| 78 | + for bullet in yellow_bullets: |
| 79 | + bullet.x += BULLET_VEL |
| 80 | + |
| 81 | + # colliddirect only works if both are rectangle |
| 82 | + if red.colliderect(bullet): |
| 83 | + # Now we are going to post a event then check in the main function for the event |
| 84 | + # it will indicate us that the bullet hit the spaceship |
| 85 | + pygame.event.post(pygame.event.Event(RED_HIT)) |
| 86 | + yellow_bullets.remove(bullet) |
| 87 | + elif bullet.x > util.width: |
| 88 | + yellow_bullets.remove(bullet) |
| 89 | + |
| 90 | + for bullet in red_bullets: |
| 91 | + bullet.x -= BULLET_VEL |
| 92 | + |
| 93 | + # colliddirect only works if both are rectangle |
| 94 | + if yellow.colliderect(bullet): |
| 95 | + # Now we are going to post a event then check in the main function for the event |
| 96 | + # it will indicate us that the bullet hit the spaceship |
| 97 | + pygame.event.post(pygame.event.Event(YELLOW_HIT)) |
| 98 | + red_bullets.remove(bullet) |
| 99 | + elif bullet.x < 0: |
| 100 | + red_bullets.remove(bullet) |
| 101 | + |
| 102 | + |
| 103 | +def main(): |
| 104 | + """ |
| 105 | + Main logic of the game, This function makes everything work together. |
| 106 | + 1. Load the assets |
| 107 | + 2. Reads input from the keyboard |
| 108 | + 3. Making bullets fire |
| 109 | + 4. Handling bullet movements |
| 110 | + 5. Displaying everything together on the screen. |
| 111 | + 6. Showing the winner |
| 112 | + 7. Again restarting the game after 5 sec. |
| 113 | + """ |
| 114 | + # Loading Assets |
| 115 | + YELLOW_SPACESHIP, RED_SPACESHIP, SPACE, BORDER = util.load_assests() |
| 116 | + |
| 117 | + # Making two rectangles so that we can control where our spaceship are moving |
| 118 | + SPACESHIP_WIDTH, SPACESHIP_HEIGHT = (50, 40) |
| 119 | + red = pygame.Rect(700, 250, SPACESHIP_WIDTH, SPACESHIP_HEIGHT) |
| 120 | + yellow = pygame.Rect(100, 250, SPACESHIP_WIDTH, SPACESHIP_HEIGHT) |
| 121 | + |
| 122 | + # To making our game refresh at a constant interval |
| 123 | + clock = pygame.time.Clock() |
| 124 | + |
| 125 | + # To storing our bullet location in pixels so that we can move it |
| 126 | + yellow_bullets = [] |
| 127 | + red_bullets = [] |
| 128 | + |
| 129 | + # Healths of our spaceships |
| 130 | + red_health = 10 |
| 131 | + yellow_health = 10 |
| 132 | + |
| 133 | + run = True |
| 134 | + |
| 135 | + while run: |
| 136 | + # Capped frame rate so it remains consistent on diff computers |
| 137 | + clock.tick(60) |
| 138 | + |
| 139 | + for event in pygame.event.get(): |
| 140 | + if event.type == pygame.QUIT: |
| 141 | + run = False |
| 142 | + pygame.quit() |
| 143 | + |
| 144 | + # checking if key pressed for firing bullet |
| 145 | + if event.type == pygame.KEYDOWN: |
| 146 | + |
| 147 | + # maximum amount of bullets a spaceship can shoot at a time |
| 148 | + MAX_BULLETS = 3 |
| 149 | + # CHECKING if we press LCTRL and we have 3 bullets at a time on a screen |
| 150 | + if event.key == pygame.K_LCTRL and len(yellow_bullets) < MAX_BULLETS: |
| 151 | + # 10, 5 width, height of bullet and others are location |
| 152 | + bullet = pygame.Rect(yellow.x + yellow.width, yellow.y + yellow.height // 2 - 2, 10, 5) |
| 153 | + yellow_bullets.append(bullet) |
| 154 | + |
| 155 | + if event.key == pygame.K_RCTRL and len(red_bullets) < MAX_BULLETS: |
| 156 | + bullet = pygame.Rect(red.x, red.y + red.height // 2 - 2, 10, 5) |
| 157 | + red_bullets.append(bullet) |
| 158 | + |
| 159 | + # If bullets hit red spaceship then decrease health |
| 160 | + if event.type == RED_HIT: |
| 161 | + red_health -= 1 |
| 162 | + # If bullets hit yellow spaceship then decrease health |
| 163 | + if event.type == YELLOW_HIT: |
| 164 | + yellow_health -= 1 |
| 165 | + |
| 166 | + winner_text = "" |
| 167 | + if red_health <= 0: |
| 168 | + winner_text = "Yellow Wins!!" |
| 169 | + if yellow_health <= 0: |
| 170 | + winner_text = "Red Wins!!" |
| 171 | + if winner_text != "": |
| 172 | + util.winner(winner_text, WINDOW) |
| 173 | + break |
| 174 | + |
| 175 | + # Checking which keys are pressed while the game is running it also checks if the |
| 176 | + # keys are pressed and remain down |
| 177 | + keys_pressed = pygame.key.get_pressed() |
| 178 | + # Spaceship velocity |
| 179 | + VELOCITY = 5 |
| 180 | + # Function that handle key movements of yellow and red spaceship and bullets |
| 181 | + util.yellow_handle_movement(keys_pressed, yellow, VELOCITY, BORDER) |
| 182 | + util.red_handle_movement(keys_pressed, red, VELOCITY, BORDER) |
| 183 | + |
| 184 | + handle_bullets(yellow_bullets, red_bullets, yellow, red) |
| 185 | + |
| 186 | + # Displaying everything on the screen. |
| 187 | + drawWindow(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health, |
| 188 | + YELLOW_SPACESHIP, RED_SPACESHIP, SPACE, BORDER) |
| 189 | + |
| 190 | + main() |
| 191 | + |
| 192 | + |
| 193 | +if __name__ == "__main__": |
| 194 | + main() |
0 commit comments