• Home
  • Textbooks
  • Mathematics for Game Programming and Computer Graphics: Explore the essential mathematics for creating, rendering, and manipulating 3D virtual environments
  • Hello Graphics Window: You’re On Your Way

Mathematics for Game Programming and Computer Graphics: Explore the essential mathematics for creating, rendering, and manipulating 3D virtual environments

Penny de Byl

Chapter 1

Hello Graphics Window: You’re On Your Way - all with Video Answers

Educators


Section 1

Working with Window and Cartesian Coordinates

Problem 1

Create a new Python script called PlotPixel. py and add the following code (the differences between Hellowindow. py and this script are shown in bold):
import pygame
pygame.init ()
screen_width $=1000$
screen_height $=800$
screen $=$ pygame.display.set_mode ((screen_width,
screen_height))
done $=\mathrm{False}$
white $=$ pygame. Color $(255,255,255)$
while not done:
for event in pygame. event.get ():
if event.type $==$ pygame.QUIT:
done = True
screen.set_at((100, 100),white)
pygame.display.update()
pygame.quit()

Check back soon!

Problem 2

The set_at () method will plot a single pixel point in the color of your choosing; in this case, the pixel will be white at the coordinates $\mathrm{x}=100$, and $\mathrm{y}=100$. To see this in action, run the script. Remember that you will need to right-click on the filename in the Project window and select Run before you'll be able to run it from the little green icon at the top-right of the window. Now, try plotting another point at $(200,200)$. You can do this with a second set_at () call, for example, as follows:
screen.set_at $((200,200)$, white $)$

Check back soon!

Problem 3

If you would prefer the origin of the coordinate system to be shown in the lower left-hand corner, then you can modify the coordinates with a simple method as follows:
import pygame
pygame.init ()
screen_width $=1000$
screen_height $=800$
screen $=$ pygame.display.set_mode ((screen_width,
screen_height))
done $=\mathrm{False}$
white $=$ pygame. $\operatorname{Color}(255,255,255)$
def to_pygame_coordinates (display, x, y) :
return $x$, display.get_height() - y
while not done:
for event in pygame. event.get ():
if event.type $==$ pygame.QUIT:
done $=$ True
screen.set_at (to_pygame_coordinates
(screen, 100,100),
white)
screen.set_at (to_pygame_coordinates
(screen, 200,200),
white)
pygame.display .update ()
pygame.quit ()

Check back soon!