-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.py
64 lines (55 loc) · 2.52 KB
/
draw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pygame
import time
import settings
class Artist:
def __init__(self, blockManager):
self.blockManager = blockManager
pygame.init()
self.screen = pygame.display.set_mode(settings.screenSize)
self.white = [255, 255, 255]
self.screen.fill(self.white)
self.digging = False
self.diggingStep = 0
self.placed = False
def getNearestCoord(self, mousePos):
if mousePos[0] % settings.imageSize >= 0.5:
x = (mousePos[0] // settings.imageSize) * settings.imageSize
elif mousePos[0] % settings.imageSize < 5:
x = (mousePos[0] // settings.imageSize - 1) * settings.imageSize
if mousePos[1] % settings.imageSize >= 0.5:
y = (mousePos[1] // settings.imageSize) * settings.imageSize
elif mousePos[1] % settings.imageSize < 5:
y = (mousePos[1] // settings.imageSize - 1) * settings.imageSize
return (x, y)
def placeImage(self, blockName, imageFile, coords):
self.blockManager.place(blockName, coords)
img = pygame.image.load(imageFile)
img = pygame.transform.scale(img, (settings.imageSize, settings.imageSize))
self.screen.blit(img, coords)
def handleEvents(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONUP:
if not pygame.mouse.get_pressed()[0] and self.digging:
self.digging = False
self.diggingStep = 0
if not pygame.mouse.get_pressed()[2] and self.placed:
self.placed = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if pygame.mouse.get_pressed()[0] and not self.digging:
self.digging = True
if pygame.mouse.get_pressed()[2] and not self.placed:
self.placed = True
blockName = "dirt"
self.placeImage(blockName, self.blockManager.registeredBlocks[blockName].imageFile, self.getNearestCoord(event.pos))
def update(self):
self.handleEvents()
if self.digging:
if self.diggingStep < 5:
self.diggingStep += 1
time.sleep(0.1)
else:
self.diggingStep = 0
self.placeImage("air", "textures/default_sky.png", self.getNearestCoord(pygame.mouse.get_pos()))
pygame.display.flip()