

Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!


Here is the Python code to create a blue dot that bounces within the walls of a red square using the Pygame library: import pygame
pygame.init()
width = 500 height = 500 screen = pygame.display.set_mode((width, height))
blue = (0, 0, 255) red = (255, 0, 0)
x = 50 y = 50 vel_x = 5 vel_y = 5
square_size = 100 square_x = (width - square_size) / 2 square_y = (height - square_size) / 2
running = True while running:
for event in pygame.event.get(): if event.type == pygame.QUIT: running = False
x += vel_x y += vel_y
if x < square_x:
x = square_x vel_x = -vel_x elif x > square_x + square_size: x = square_x + square_size vel_x = -vel_x if y < square_y: y = square_y vel_y = -vel_y elif y > square_y + square_size: y = square_y + square_size vel_y = -vel_y
screen.fill((255, 255, 255)) pygame.draw.rect(screen, red, (square_x, square_y, square_size, square_size))
pygame.draw.circle(screen, blue, (x, y), 10)
pygame.display.flip()
pygame.quit() This program uses a main loop that runs until the user closes the game window. Within the loop, Pygame events are handled, the dot is moved, and everything is drawn on the screen. The dot bounces within the square using a series of conditional checks. Finally, the screen is updated with pygame.display.flip() and Pygame is exited at the end of the program with pygame.quit().