2017-2018/ISN/pygame/draw.py

67 lines
1.9 KiB
Python

import pygame, sys
pygame.init()
windowSurface = pygame.display.set_mode((500, 400))
pygame.display.set_caption('Hello world!')
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
color_text = WHITE
color_background = WHITE
color_polygon = GREEN
color_border = RED
running = True
while running:
windowSurface.fill(color_background)
pygame.draw.polygon(windowSurface, color_polygon,
((146, 0), (291, 106), (236, 277), (56, 277), (0, 106))
)
pygame.draw.line(windowSurface, BLUE, (60, 60), (120, 60), 4)
pygame.draw.line(windowSurface, BLUE, (120, 60), (60, 120))
pygame.draw.line(windowSurface, BLUE, (60, 120), (120, 120), 4)
pygame.draw.circle(windowSurface, BLUE, (300, 50), 20, 0)
pygame.draw.ellipse(windowSurface, RED, (300, 250, 40,80), 1)
basic_font = pygame.font.SysFont(None, 48)
text = basic_font.render('Hello world!', True, color_text ,BLUE)
text_rect = text.get_rect()
text_rect.centerx = windowSurface.get_rect().centerx
text_rect.centery = windowSurface.get_rect().centery
pygame.draw.rect(windowSurface, color_border,
(text_rect.left - 20, text_rect.top - 20,
text_rect.width + 40, text_rect.height + 40)
)
windowSurface.blit(text, text_rect)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONUP:
color_background = WHITE
color_border = (255, (color_border[1] + 10) % 256, 0)
elif event.type == pygame.MOUSEBUTTONDOWN:
color_background = RED
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
color_polygon = BLACK
elif event.key == pygame.K_DOWN:
color_polygon = GREEN
pygame.quit()