CoolFace
Apppublic

awacke1/PyGame2D

sourceHugging Faceapache-2.0updated 4y agoView on Hugging Face
1likes
app.py181 linesDownload Raw Back to root
1import pygame2import os3pygame.font.init()4pygame.mixer.init()5 6WIDTH, HEIGHT = 900, 5007WIN = pygame.display.set_mode((WIDTH, HEIGHT))8pygame.display.set_caption("First Game!")9 10WHITE = (255, 255, 255)11BLACK = (0, 0, 0)12RED = (255, 0, 0)13YELLOW = (255, 255, 0)14 15BORDER = pygame.Rect(WIDTH//2 - 5, 0, 10, HEIGHT)16 17#BULLET_HIT_SOUND = pygame.mixer.Sound('Assets/Grenade+1.mp3')18#BULLET_FIRE_SOUND = pygame.mixer.Sound('Assets/Gun+Silencer.mp3')19 20HEALTH_FONT = pygame.font.SysFont('comicsans', 40)21WINNER_FONT = pygame.font.SysFont('comicsans', 100)22 23FPS = 6024VEL = 525BULLET_VEL = 726MAX_BULLETS = 327SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 4028 29YELLOW_HIT = pygame.USEREVENT + 130RED_HIT = pygame.USEREVENT + 231 32YELLOW_SPACESHIP_IMAGE = pygame.image.load(33    os.path.join('Assets', 'spaceship_yellow.png'))34YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(35    YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90)36 37RED_SPACESHIP_IMAGE = pygame.image.load(38    os.path.join('Assets', 'spaceship_red.png'))39RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(40    RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270)41 42SPACE = pygame.transform.scale(pygame.image.load(43    os.path.join('Assets', 'space.png')), (WIDTH, HEIGHT))44 45 46def draw_window(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health):47    WIN.blit(SPACE, (0, 0))48    pygame.draw.rect(WIN, BLACK, BORDER)49 50    red_health_text = HEALTH_FONT.render(51        "Health: " + str(red_health), 1, WHITE)52    yellow_health_text = HEALTH_FONT.render(53        "Health: " + str(yellow_health), 1, WHITE)54    WIN.blit(red_health_text, (WIDTH - red_health_text.get_width() - 10, 10))55    WIN.blit(yellow_health_text, (10, 10))56 57    WIN.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))58    WIN.blit(RED_SPACESHIP, (red.x, red.y))59 60    for bullet in red_bullets:61        pygame.draw.rect(WIN, RED, bullet)62 63    for bullet in yellow_bullets:64        pygame.draw.rect(WIN, YELLOW, bullet)65 66    pygame.display.update()67 68 69def yellow_handle_movement(keys_pressed, yellow):70    if keys_pressed[pygame.K_a] and yellow.x - VEL > 0:  # LEFT71        yellow.x -= VEL72    if keys_pressed[pygame.K_d] and yellow.x + VEL + yellow.width < BORDER.x:  # RIGHT73        yellow.x += VEL74    if keys_pressed[pygame.K_w] and yellow.y - VEL > 0:  # UP75        yellow.y -= VEL76    if keys_pressed[pygame.K_s] and yellow.y + VEL + yellow.height < HEIGHT - 15:  # DOWN77        yellow.y += VEL78 79 80def red_handle_movement(keys_pressed, red):81    if keys_pressed[pygame.K_LEFT] and red.x - VEL > BORDER.x + BORDER.width:  # LEFT82        red.x -= VEL83    if keys_pressed[pygame.K_RIGHT] and red.x + VEL + red.width < WIDTH:  # RIGHT84        red.x += VEL85    if keys_pressed[pygame.K_UP] and red.y - VEL > 0:  # UP86        red.y -= VEL87    if keys_pressed[pygame.K_DOWN] and red.y + VEL + red.height < HEIGHT - 15:  # DOWN88        red.y += VEL89 90 91def handle_bullets(yellow_bullets, red_bullets, yellow, red):92    for bullet in yellow_bullets:93        bullet.x += BULLET_VEL94        if red.colliderect(bullet):95            pygame.event.post(pygame.event.Event(RED_HIT))96            yellow_bullets.remove(bullet)97        elif bullet.x > WIDTH:98            yellow_bullets.remove(bullet)99 100    for bullet in red_bullets:101        bullet.x -= BULLET_VEL102        if yellow.colliderect(bullet):103            pygame.event.post(pygame.event.Event(YELLOW_HIT))104            red_bullets.remove(bullet)105        elif bullet.x < 0:106            red_bullets.remove(bullet)107 108 109def draw_winner(text):110    draw_text = WINNER_FONT.render(text, 1, WHITE)111    WIN.blit(draw_text, (WIDTH/2 - draw_text.get_width() /112                         2, HEIGHT/2 - draw_text.get_height()/2))113    pygame.display.update()114    pygame.time.delay(5000)115 116 117def main():118    red = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)119    yellow = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)120 121    red_bullets = []122    yellow_bullets = []123 124    red_health = 10125    yellow_health = 10126 127    clock = pygame.time.Clock()128    run = True129    while run:130        clock.tick(FPS)131        for event in pygame.event.get():132            if event.type == pygame.QUIT:133                run = False134                pygame.quit()135 136            if event.type == pygame.KEYDOWN:137                if event.key == pygame.K_LCTRL and len(yellow_bullets) < MAX_BULLETS:138                    bullet = pygame.Rect(139                        yellow.x + yellow.width, yellow.y + yellow.height//2 - 2, 10, 5)140                    yellow_bullets.append(bullet)141                    #BULLET_FIRE_SOUND.play()142 143                if event.key == pygame.K_RCTRL and len(red_bullets) < MAX_BULLETS:144                    bullet = pygame.Rect(145                        red.x, red.y + red.height//2 - 2, 10, 5)146                    red_bullets.append(bullet)147                    #BULLET_FIRE_SOUND.play()148 149            if event.type == RED_HIT:150                red_health -= 1151                #BULLET_HIT_SOUND.play()152 153            if event.type == YELLOW_HIT:154                yellow_health -= 1155                #BULLET_HIT_SOUND.play()156 157        winner_text = ""158        if red_health <= 0:159            winner_text = "Yellow Wins!"160 161        if yellow_health <= 0:162            winner_text = "Red Wins!"163 164        if winner_text != "":165            draw_winner(winner_text)166            break167 168        keys_pressed = pygame.key.get_pressed()169        yellow_handle_movement(keys_pressed, yellow)170        red_handle_movement(keys_pressed, red)171 172        handle_bullets(yellow_bullets, red_bullets, yellow, red)173 174        draw_window(red, yellow, red_bullets, yellow_bullets,175                    red_health, yellow_health)176 177    main()178 179 180if __name__ == "__main__":181    main()