0

This program currently makes two squares appear on the screen when you press start, the one on the left is controlled using WASD whilst the one on the right is controlled using the arrow keys. I want to make it so that when you click spacebar, the square on the left shoots a red rectangle (the bullet) to the right of the screen and if it hits the other square, then the health goes down by one. If it doesn't hit the other square, the bullet hits the edge of the screen and disappears (I would also like the other square to do this too but the other way around and instead of pressing space you press right control). The health variable for both squares is on lines 9 and 10. Can somebody please tell me how to do this?

Full Code (The two key presses that I would like to make the chosen character shoot are lines 60 and 72):

import pygame
import sys
import os

pygame.init()

FPS = 60

HEALTH_L = 3
HEALTH_R = 3

start_smallfont = pygame.font.SysFont('Corbel', 45)
start_text = start_smallfont.render('Start', True, (255, 255, 255))
rect_smallfont = pygame.font.SysFont('Corbel', 33)
rect_text = rect_smallfont.render('You', True, (255, 255, 255))
x = 630
y = 325
x2 = 170
y2 = 325
vel = 0.1
startWIDTH, startHEIGHT = 170, 80
screenWIDTH, screenHEIGHT = 800, 720

WIN = pygame.display.set_mode((screenWIDTH, screenHEIGHT))
pygame.display.set_caption(":D")



def main():
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if 800/2-85 <= mouse[0] <= 800/2-85+startWIDTH and 720/2-40 <= mouse[1] <= 720/2-40+startHEIGHT:
                    clock.tick(FPS)
                    run = False
                    while True:
                        global x, y, x2, y2, movingx
                        for event in pygame.event.get():
                            if event.type == pygame.QUIT:
                                pygame.quit()

                        WIN.fill((0, 0, 0))
                        keys = pygame.key.get_pressed()

                        if keys[pygame.K_LEFT] and x > vel:
                            x -= vel
                        if keys[pygame.K_RIGHT] and x < screenWIDTH - 50 - vel:
                            x += vel
                        if keys[pygame.K_UP] and y > vel:
                            y -= vel
                        if keys[pygame.K_DOWN] and y < screenHEIGHT - 50 - vel:
                            y += vel
                        if keys[pygame.K_SPACE]:
                            #PUT BULLET FUNCTION HERE


                        if keys[pygame.K_a] and x2 > vel:
                            x2 -= vel
                        if keys[pygame.K_d] and x2 < screenWIDTH - 50 - vel:
                            x2 += vel
                        if keys[pygame.K_w] and y2 > vel:
                            y2 -= vel
                        if keys[pygame.K_s] and y2 < screenHEIGHT - 50 - vel:
                            y2 += vel
                        if keys[pygame.K_RCTRL]:
                            #PUT BULLET FUNCTION HERE

                            
                        pygame.draw.rect(WIN, (255, 0, 0), (x, y, 50, 50))
                        pygame.draw.rect(WIN, (255, 0, 0), (x2, y2, 50, 50))

                        pygame.display.update()



        mouse = pygame.mouse.get_pos()

        if 800/2-85 <= mouse[0] <= 800/2-85+startWIDTH and 720/2-40 <= mouse[1] <= 720/2-40+startHEIGHT:
            pygame.draw.rect(WIN, (255, 91, 91), (800/2-85, 720/2-40, startWIDTH, startHEIGHT))
        else:
            pygame.draw.rect(WIN, (255, 0, 0), (800/2-85, 720/2-40, startWIDTH, startHEIGHT))

        WIN.blit(start_text, (800/2-85+42,720/2-40+20))
        pygame.display.update()





if __name__ == "__main__":
    main()
Rabbid76
  • 177,135
  • 25
  • 101
  • 146
  • What have you tried so far? This question is far to broad. Please show us your attempts and tell us exactly where you struggle. – Rabbid76 Jun 19 '21 at 12:08
  • @Rabbid76 I just have no idea where to start, I've tried various things like creating a rectangle inside the square and making that go right when space is pressed but multiple errors pop up. I am relatively new to pygame and I am clueless to this concept of projectiles. – Gratah3 Jun 19 '21 at 12:22
  • See [What topics can I ask about here?](https://stackoverflow.com/help/on-topic). Actually you are searching for a tutorial. Maybe some other questions can help: [How can i shoot a bullet with space bar?](https://stackoverflow.com/questions/59687250/how-can-i-shoot-a-bullet-with-space-bar/59689297#59689297), [How to get keyboard input in pygame?](https://stackoverflow.com/questions/16044229/how-to-get-keyboard-input-in-pygame/64494842#64494842), [How do I detect collision in pygame?](https://stackoverflow.com/questions/29640685/how-do-i-detect-collision-in-pygame/65064907#65064907) – Rabbid76 Jun 19 '21 at 12:25

0 Answers0