-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
100 lines (95 loc) · 3.19 KB
/
game.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import pygame
from pygame.locals import *
from player import Player
from constants import CHUNKSIZE
from confvar import renderDistance, showGrass, fpsLimit
from main import screen,clock,cursor
from funcs import *
from paths import blocksPath
def game():
from mainMenu import mainMenu
grass = loadPathTexture(blocksPath, "grass.png", True, (64, 64))
sand = loadPathTexture(blocksPath, "sand.png", True, (64, 64))
chunks = []
for x in range(1):
for y in range(1):
chunks.append([x,y])
player = Player((8, 8))
pressedKeys = {
"up": False,
"down": False,
"left": False,
"right": False,
}
playerChunkPos = (player.x//(64*CHUNKSIZE), player.y//(64*CHUNKSIZE))
loadedChunks = []
for x in range(playerChunkPos[0]-renderDistance//2, playerChunkPos[0]+renderDistance//2):
for y in range(playerChunkPos[1]-renderDistance//2, playerChunkPos[1]+renderDistance//2):
if not [x,y] in chunks:
chunks.append([x,y])
loadedChunks.append([x,y])
cameraOffset = [0,0]
while 1:
mousePos = pygame.mouse.get_pos()
clock.tick(fpsLimit)
screen.fill((51, 153, 218))
if playerChunkPos != (player.x//(64*CHUNKSIZE), player.y//(64*CHUNKSIZE)):
playerChunkPos = (player.x//(64*CHUNKSIZE), player.y//(64*CHUNKSIZE))
loadedChunks.clear()
for x in range(playerChunkPos[0]-renderDistance//2, playerChunkPos[0]+renderDistance//2):
for y in range(playerChunkPos[1]-renderDistance//2, playerChunkPos[1]+renderDistance//2):
if [x,y] in chunks:
loadedChunks.append([x,y])
else:
chunks.append([x,y])
loadedChunks.append([x,y])
for chunk in loadedChunks:
chunkRect = pygame.Rect(chunk[0]*64*CHUNKSIZE+cameraOffset[0], chunk[1]*64*CHUNKSIZE+cameraOffset[1], 64*CHUNKSIZE, 64*CHUNKSIZE)
chunkSurface = pygame.Surface((64*CHUNKSIZE, 64*CHUNKSIZE))
chunkSurface.fill((57, 194, 114))
if showGrass:
for x in range(CHUNKSIZE):
for y in range(CHUNKSIZE):
chunkSurface.blit(grass, (x*64, y*64))
if chunkRect.collidepoint(mousePos[0], mousePos[1]):
pygame.draw.rect(chunkSurface, (255, 255, 255), ((mousePos[0]-chunkRect.x)//64*64, (mousePos[1]-chunkRect.y)//64*64,64,64),2)
screen.blit(chunkSurface, chunkRect)
pygame.draw.rect(screen, (255, 0, 0), chunkRect, 1)
if pressedKeys["up"]:
player.up()
# cameraOffset[1] += 3
if pressedKeys["down"]:
player.y += player.speed
# cameraOffset[1] -= 3
if pressedKeys["left"]:
player.left()
if pressedKeys["right"]:
player.x += player.speed
player.render(screen)
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_w:
pressedKeys["up"] = True
elif event.key == K_s:
pressedKeys["down"] = True
elif event.key == K_a:
pressedKeys["left"] = True
elif event.key == K_d:
pressedKeys["right"] = True
elif event.key == K_ESCAPE:
mainMenu()
elif event.type == KEYUP:
if event.key == K_w:
pressedKeys["up"] = False
elif event.key == K_s:
pressedKeys["down"] = False
elif event.key == K_a:
pressedKeys["left"] = False
elif event.key == K_d:
pressedKeys["right"] = False
elif event.type == MOUSEBUTTONDOWN:
pass
if event.type == QUIT:
gameExit()
screen.blit(cursor, mousePos)
pygame.display.update()