Draw a line from bottom left to top right năm 2024
Welcome to part 2 in my pygame tutorial. This time we will build upon what you learned last time as we start drawing onto surfaces. We will continue to move very slowly so that everybody gets a chance to learn. Drawing a line You draw a line by using To draw a blue line from (0, 0) to (200, 100) (note: we are measuring in pixels) onto the surface screen you do:
You could also do this:
This is will do exactly the same thing as the previous example, but is (possibly) more readable. Let’s try to put this together to a little program. We will draw two diagonal lines: one going from the top left-hand corner to the bottom right-hand corner and one from the top right-hand corner to the bottom left-hand corner. We will use 1 #! /usr/bin/env python 2 3 import pygame 4 5 screen = pygame.display.set_mode((640, 480)) 6 running = 1 7 8 while running: 9 event = pygame.event.poll() 10 if event.type == pygame.QUIT: 11 running = 0 12 13 screen.fill((0, 0, 0)) 14 pygame.draw.line(screen, (0, 0, 255), (0, 0), (639, 479)) 15 pygame.draw.aaline(screen, (0, 0, 255), (639, 0), (0, 479)) 16 pygame.display.flip() I hope you recognize most of the code from the first tutorial. If you run the program you should see a big blue ‘x’ across the window. Exercises
Moving things around Now that we know how to draw lines on the screen with pygame, let’s start moving them around. Moving lines around is simple. we store the line coordinates in variables. Inside the event loop we modify the values of those variables. Let’s draw a line that jumps up and down. 1 #! /usr/bin/env python 2 3 import pygame 4 5 y = 0 6 dir = 1 7 running = 1 8 width = 800 9 height = 600 10 screen = pygame.display.set_mode((width, height)) 11 linecolor = 255, 0, 0 12 bgcolor = 0, 0, 0 13 14 while running: 15 event = pygame.event.poll() 16 if event.type == pygame.QUIT: 17 running = 0 18 19 screen.fill(bgcolor) 20 pygame.draw.line(screen, linecolor, (0, y), (width-1, y)) 21 22 y += dir 23 if y == 0 or y == height-1: dir *= -1 24 25 pygame.display.flip() There we go. The y-position of the line is determined by the variable 1 #! /usr/bin/env python 2 3 import pygame 4 5 y = 0 6 dir = 1 7 running = 1 8 width = 800 9 height = 600 10 screen = pygame.display.set_mode((width, height)) 11 linecolor = 255, 0, 0 12 bgcolor = 0, 0, 0 13 14 while running: 15 event = pygame.event.poll() 16 if event.type == pygame.QUIT: 17 running = 0 18 19 screen.fill(bgcolor) 20 pygame.draw.line(screen, linecolor, (0, y), (width-1, y)) 21 22 y += dir 23 if y == 0 or y == height-1: dir *= -1 24 25 pygame.display.flip() 3 which increased by 1 #! /usr/bin/env python 2 3 import pygame 4 5 y = 0 6 dir = 1 7 running = 1 8 width = 800 9 height = 600 10 screen = pygame.display.set_mode((width, height)) 11 linecolor = 255, 0, 0 12 bgcolor = 0, 0, 0 13 14 while running: 15 event = pygame.event.poll() 16 if event.type == pygame.QUIT: 17 running = 0 18 19 screen.fill(bgcolor) 20 pygame.draw.line(screen, linecolor, (0, y), (width-1, y)) 21 22 y += dir 23 if y == 0 or y == height-1: dir *= -1 24 25 pygame.display.flip() 4 in each iteration of the event loop. The value of 1 #! /usr/bin/env python 2 3 import pygame 4 5 y = 0 6 dir = 1 7 running = 1 8 width = 800 9 height = 600 10 screen = pygame.display.set_mode((width, height)) 11 linecolor = 255, 0, 0 12 bgcolor = 0, 0, 0 13 14 while running: 15 event = pygame.event.poll() 16 if event.type == pygame.QUIT: 17 running = 0 18 19 screen.fill(bgcolor) 20 pygame.draw.line(screen, linecolor, (0, y), (width-1, y)) 21 22 y += dir 23 if y == 0 or y == height-1: dir *= -1 24 25 pygame.display.flip() 4 is 1 when the line is moving downwards, or -1 when the line is moving upwards. Simple, isn’t it? Exercises
Drawing a color bar Our final example for today will draw a single color bar (“copper bar” if you’ve ever owned an Amiga). Just like our last example, this one will also jump. 1 #! /usr/bin/env python 2 3 import pygame 4 5 y = 0 6 dir = 1 7 running = 1 8 barheight = 124 9 screen = pygame.display.set_mode((800, 600)); 10 11 barcolor = [] 12 for i in range(1, 63): 13 barcolor.append((0, 0, i*4)) 14 for i in range(1, 63): 15 barcolor.append((0, 0, 255 - i*4)) 16 17 while running: 18 event = pygame.event.poll() 19 if event.type == pygame.QUIT: 20 running = 0 21 22 screen.fill((0, 0, 0)) 23 for i in range(0, barheight): 24 pygame.draw.line(screen, barcolor[i], (0, y+i), (799, y+i)) 25 26 y += dir 27 if y + barheight > 599 or y < 0: 28 dir *= -1 29 30 pygame.display.flip() In this example I create an array called colorbar (for the lack of a better name). This will hold values for the colorbar as it shifts from black to bright blue and back to black. Keep in mind that a color is composed of red, green and blue. Each one of these can be a value between 0 and 255. If I change the blue value by one for each new line, I would get a really smooth gradient bar. But, the height would be 256 pixels from black to blue and another 256 pixels from blue back to black = 512 pixels, which is too high. There would hardly be enough space in our window to see the bar moving up and down. So I have to decide on a bar height that I find acceptable. I settled on 124, which means I have 62 pixels from the black to blue gradient and another 62 for the blue to black gradient. This also means that I have to move from a blue value of 1 to a blue value of 255 in 62 pixels. The change in blue per line must be 4 per line. I use two for loops to populate the barcolor array. The first loop pushes increasing values of blue to the array and the second one decreasing values of blue. It is important to notice that this array is nothing other than a lookup. It doesn’t contain the bar as you see it on the screen. It just contains color values. Run it and see what it looks like. Once you’ve seen it run, analyze the code and make sure you understand exactly how it works. Then start experimenting with it. Exercises
Conclusion In this tutorial you have learned to draw lines. We will be staying with methods of drawing to the screen a little while longer, before we move on to other interesting things like loading and displaying images. In the next tutorial you will also learn a bit about mouse events. 2007.12.11:Added explanation of what the colorbar array does in the last example. Thanks to reader Sergy for pointing out that it needed some explanation. How to draw line in CSS?You can create a straight and horizontal line within CSS by using the tag or the border-bottom property. How do you add a line in HTML CSS?To make a horizontal line in HTML, use the element. You can place the element — which stands for “horizontal rule” — wherever you want to create a horizontal line across a web page. Like the img element, the element is an “empty element” because it does not have a closing tag. |