# Leo Salazar # 2/15/25 # Intro to Pygame import pygame pygame.init() # basic setup screen = pygame.display.set_mode(((800, 600))) pygame.display.set_caption("intro to pygame") clock = pygame.time.Clock() running = True dt = 0 # making Rectangle Objects thing = pygame.Rect(400, 400, 100, 100) other = pygame.Rect(200, 200, 100, 100) # variables for speed and velocity speed = 300 velocity_x = 300 velocity_y = 300 # color stuff color_i = 0 colors = ['red', 'blue', 'green', 'yellow', 'orange', 'purple'] # score stuff score = 0 text = pygame.font.Font(None, 60) # game loop while running: for event in pygame.event.get(): # to quit the game if event.type == pygame.QUIT: running = False # player input (w,a,s,d) keys = pygame.key.get_pressed() if keys[pygame.K_w]: thing.y -= speed * dt if keys[pygame.K_a]: thing.x -= speed * dt if keys[pygame.K_s]: thing.y += speed * dt if keys[pygame.K_d]: thing.x += speed * dt # auto moves the object vel_x distance and vel_y distance every loop thing.x += velocity_x * dt thing.y += velocity_y * dt # bounds checker if thing.left < 0: thing.left = 0 velocity_x *= -1 # flipping velocity velocity_x += 10 # making it faster color_i = (color_i + 1) % len(colors) # changing the color score += 1 # incrementing the score # the same for the other 3 sides if thing.right > screen.get_width(): thing.right = screen.get_width() velocity_x += 10 velocity_x *= -1 color_i = (color_i + 1) % len(colors) score += 1 if thing.top < 0: thing.top = 0 velocity_y *= -1 velocity_y += 10 color_i = (color_i + 1) % len(colors) score += 1 if thing.bottom > screen.get_height(): thing.bottom = screen.get_height() velocity_y += 10 velocity_y *= -1 color_i = (color_i + 1) % len(colors) score += 1 # player collision introduction if thing.colliderect(other): print(True) else: print(False) # filling the background as black (always goes first) screen.fill("black") # show score text_box = text.render(f"Score: {score}", None, "white") screen.blit(text_box, (10, 10)) # drawing objects on the screen # pygame.draw.rect(screen, "white", thing) pygame.draw.ellipse(screen, colors[color_i], thing) # pygame.draw.ellipse(screen, "white", other) # showing our changes on the screen pygame.display.flip() # time > fps dt = clock.tick(60) / 1000 # quits the process (otherwise window wont close) pygame.quit()