{"id":204341,"date":"2025-03-21T18:45:05","date_gmt":"2025-03-21T18:45:05","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=204341"},"modified":"2025-03-21T18:45:08","modified_gmt":"2025-03-21T18:45:08","slug":"write-a-program-that-draws-a-stoplight","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/03\/21\/write-a-program-that-draws-a-stoplight\/","title":{"rendered":"Write a program that draws a stoplight"},"content":{"rendered":"\n<p>Write a program that draws a stoplight. You should have a gray rectangle, and then three circles in the rectangle. The circles should be red, then yellow, then green.<\/p>\n\n\n\n<p>The rectangle should be centered on the screen. The yellow light should be centered on the screen, and the red and green light should be offset by BUFFER amount. BUFFER amount represents the distance from the center of one circle to the center of another circle.<\/p>\n\n\n\n<p>Implement the function draw_circle that draws a single circle. Use it to draw the red, yellow, and green lights. Since all of the lights are the same size and aligned vertically, the function only needs to take the y position and color as arguments.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/03\/image-1143-808x1024.png\" alt=\"\" class=\"wp-image-204342\"\/><\/figure>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-6-color\">The correct answer and explanation is:<\/mark><\/strong><\/p>\n\n\n\n<p>Here is a Python program using the Pygame library to draw a stoplight:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pygame\n\n# Constants\nWIDTH, HEIGHT = 400, 600\nRECT_WIDTH, RECT_HEIGHT = 150, 400\nCIRCLE_RADIUS = 40\nBUFFER = 100  # Distance between the centers of the circles\nRECT_COLOR = (100, 100, 100)  # Gray color\nRED = (255, 0, 0)\nYELLOW = (255, 255, 0)\nGREEN = (0, 255, 0)\n\n# Initialize Pygame\npygame.init()\nscreen = pygame.display.set_mode((WIDTH, HEIGHT))\npygame.display.set_caption(\"Stoplight\")\n\ndef draw_circle(y, color):\n    \"\"\"Draws a single circle at the given y-position with the specified color.\"\"\"\n    pygame.draw.circle(screen, color, (WIDTH \/\/ 2, y), CIRCLE_RADIUS)\n\ndef draw_stoplight():\n    \"\"\"Draws the stoplight with three lights and a gray rectangle.\"\"\"\n    rect_x = (WIDTH - RECT_WIDTH) \/\/ 2\n    rect_y = (HEIGHT - RECT_HEIGHT) \/\/ 2\n    pygame.draw.rect(screen, RECT_COLOR, (rect_x, rect_y, RECT_WIDTH, RECT_HEIGHT))\n\n    # Calculate the y-positions of the lights\n    center_y = HEIGHT \/\/ 2\n    red_y = center_y - BUFFER\n    yellow_y = center_y\n    green_y = center_y + BUFFER\n\n    # Draw the lights\n    draw_circle(red_y, RED)\n    draw_circle(yellow_y, YELLOW)\n    draw_circle(green_y, GREEN)\n\n# Main loop\nrunning = True\nwhile running:\n    screen.fill((255, 255, 255))  # White background\n    draw_stoplight()\n    pygame.display.flip()  # Update display\n\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            running = False\n\npygame.quit()\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation (300 words):<\/h3>\n\n\n\n<p>This program creates a stoplight using Pygame, a popular Python library for graphics. The window size is set to 400&#215;600 pixels, and the stoplight consists of a gray rectangle with three circles (red, yellow, and green) arranged vertically.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Constants and Initialization:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The window dimensions are defined as <code>WIDTH = 400<\/code> and <code>HEIGHT = 600<\/code>.<\/li>\n\n\n\n<li>The stoplight rectangle has a fixed size (<code>RECT_WIDTH = 150<\/code>, <code>RECT_HEIGHT = 400<\/code>).<\/li>\n\n\n\n<li><code>CIRCLE_RADIUS = 40<\/code> ensures the lights are of uniform size.<\/li>\n\n\n\n<li><code>BUFFER = 100<\/code> controls the vertical spacing between the circle centers.<\/li>\n\n\n\n<li>Colors for the rectangle and circles are defined using RGB values.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>draw_circle(y, color)<\/code> function:<\/strong>\n<ul class=\"wp-block-list\">\n<li>This function draws a circle at the center of the screen (<code>WIDTH \/\/ 2<\/code>) and a specified <code>y<\/code> position.<\/li>\n\n\n\n<li>It takes the color as an argument to differentiate between the three lights.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>draw_stoplight()<\/code> function:<\/strong>\n<ul class=\"wp-block-list\">\n<li>This function first calculates the coordinates for the rectangle so that it is centered.<\/li>\n\n\n\n<li>It then calculates the <code>y<\/code> positions of the circles based on <code>BUFFER<\/code>.<\/li>\n\n\n\n<li>The red, yellow, and green lights are drawn at the correct positions using <code>draw_circle()<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Loop:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The screen is continuously updated within a loop.<\/li>\n\n\n\n<li><code>pygame.display.flip()<\/code> refreshes the display to show the stoplight.<\/li>\n\n\n\n<li>The program listens for a quit event (<code>pygame.QUIT<\/code>) to close the window.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>This approach modularizes the drawing process, making it easy to adjust and extend.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write a program that draws a stoplight. You should have a gray rectangle, and then three circles in the rectangle. The circles should be red, then yellow, then green. The rectangle should be centered on the screen. The yellow light should be centered on the screen, and the red and green light should be offset [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-204341","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/204341","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=204341"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/204341\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=204341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=204341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=204341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}