Larger worlds added. Also release scripts.

This commit is contained in:
pjht 2018-09-03 20:16:19 -05:00
parent ee5c7ae9c5
commit e903446454
13 changed files with 152 additions and 78 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
__pycache__
map_*.pkl
serv
client

View File

@ -7,14 +7,13 @@ import select
import blocks
import socket
import pickle
import math
import lib.constants as constants
from lib.gameregistry import GameRegistry
from lib.map import Map
from lib.character import Character
from lib.block import Block
from player import *
from player_img import *
from lib.player import Player
from lib.player_img import PlayerImg
from time import sleep
def recv_str(sock,print_str=True):
@ -70,16 +69,12 @@ data=recvall(sock)
map=pickle.loads(data)
# The server's generated map does not have a screen. We fix that here.
map.screen=screen
for s in map.sprites():
s.screen=screen
send_str(sock,"GET_POS_FOR_UID")
send_str(sock,str(my_uid))
x=int(recv_str(sock))*constants.TILESIZE
y=int(recv_str(sock))*constants.TILESIZE
fac=recv_str(sock)
player=Player(x,y,map,screen,UNAME,"player_local")
for row in map.tiles:
for tile in row:
tile.screen=screen
player=Player(0,0,map,screen,UNAME,"player_local")
player.inv.addTile("workbench",1)
player.dir=fac
others={}
running=True
move=False
@ -100,8 +95,8 @@ while running:
if uname!=UNAME:
send_str(sock,"GET_POS_FOR_UID")
send_str(sock,uid)
x=int(recv_str(sock))*constants.TILESIZE
y=int(recv_str(sock))*constants.TILESIZE
x=int(recv_str(sock))
y=int(recv_str(sock))
fac=recv_str(sock)
if uname in others:
others[uname].x=x
@ -142,15 +137,16 @@ while running:
block_data=change["block_data"]
tile=map.tileAt(x,y)
tile.loadData(block_data)
for s in map.sprites():
if s.mp_upd:
data=s.interactData()
send_str(sock,"INTERACT_BLOCK_AT")
send_str(sock,str(my_uid))
send_str(sock,str(math.floor(s.x)))
send_str(sock,str(math.floor(s.y)))
data_string=pickle.dumps(data)
sock.send(data_string)
for row in map.tiles:
for s in row:
if s.mp_upd:
data=s.interactData()
send_str(sock,"INTERACT_BLOCK_AT")
send_str(sock,str(my_uid))
send_str(sock,str(s.x))
send_str(sock,str(s.y))
data_string=pickle.dumps(data)
sock.send(data_string)
if select.select([sys.stdin],[],[],0.0)[0]:
cmd=input()
cmd=cmd.split()
@ -178,8 +174,8 @@ while running:
if to_place!="":
send_str(sock,"PLACE_BLOCK_AT")
send_str(sock,str(my_uid))
send_str(sock,str(math.floor(coords[0])))
send_str(sock,str(math.floor(coords[1])))
send_str(sock,str(coords[0]))
send_str(sock,str(coords[1]))
send_str(sock,str(to_place))
player.interact()
else:
@ -189,8 +185,8 @@ while running:
if data!=None:
send_str(sock,"INTERACT_BLOCK_AT")
send_str(sock,str(my_uid))
send_str(sock,str(math.floor(coords[0])))
send_str(sock,str(math.floor(coords[1])))
send_str(sock,str(coords[0]))
send_str(sock,str(coords[1]))
data_string=pickle.dumps(data)
sock.send(data_string)
if event.key==pygame.K_SLASH:
@ -201,8 +197,8 @@ while running:
y=facing[1]
send_str(sock,"BREAK_BLOCK_AT")
send_str(sock,str(my_uid))
send_str(sock,str(math.floor(x)))
send_str(sock,str(math.floor(y)))
send_str(sock,str(x))
send_str(sock,str(y))
if event.key==pygame.K_j:
player.inv.selPrev()
if event.key==pygame.K_k:
@ -227,8 +223,8 @@ while running:
player.move(dir)
send_str(sock,"SET_POS_FOR_UID")
send_str(sock,str(my_uid))
send_str(sock,str(math.floor(player.x/constants.TILESIZE)))
send_str(sock,str(math.floor(player.y/constants.TILESIZE)))
send_str(sock,str(player.x))
send_str(sock,str(player.y))
send_str(sock,player.dir)
if inv:
screen.fill([255,255,255])
@ -243,14 +239,30 @@ while running:
pygame.display.flip()
else:
screen.fill([0,0,0])
map.draw()
map.draw(player.x,player.y)
player.draw()
selected=player.inv.selected
if selected!="":
texture=Block.textures[selected]
screen.blit(texture,(0*constants.TILESIZE,32*constants.TILESIZE))
for uname,other in others.items():
other.draw()
screen.blit(texture,(0*constants.TILESIZE,constants.PORTHEIGHT*constants.TILESIZE))
topleftx=player.x-16
toplefty=player.y-16
x=topleftx
y=toplefty
while True:
other=None
for uname,o in others.items():
if o.x==x and o.y==y:
other=o
break
if other:
other.draw(x-topleftx,y-toplefty)
x+=1
if x==topleftx+constants.PORTWIDTH:
x=topleftx
y+=1
if y==toplefty+constants.PORTHEIGHT:
break
pygame.display.flip()
sleep(0.1)
send_str(sock,"CLOSE")

3
dist_all Executable file
View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
./dist_client
./dist_serv

11
dist_client Executable file
View File

@ -0,0 +1,11 @@
#!/usr/bin/env bash
rm -rf client
mkdir client
mkdir client/code
cp -r lib sprites tiles animation blocks.py recipes.py client/code
cp client.py client/code
echo "#!/usr/bin/env bash
cd code
python3 client.py
cd .." > client/run
chmod u+x client/run

10
dist_serv Executable file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env bash
rm -rf serv
mkdir serv
mkdir serv/code
mkdir serv/worlds
cp -r lib blocks.py serv/code
cp serv.py serv/code
echo "#!/usr/bin/env bash
python3 code/serv.py" > serv/run
chmod u+x serv/run

View File

@ -1,2 +1,2 @@
from . import block,constants,gameregistry,map,character,inventory
__all__=["block","gameregistry","map","character","inventory"]
from . import block,constants,gameregistry,map,character,inventory,player,player_img
__all__=["block","gameregistry","map","character","inventory","player","player_img"]

View File

@ -21,14 +21,15 @@ class Block(Sprite):
self.unlocalisedName=""
self.drops=False
self.mp_upd=False
def draw(self):
def draw(self,x,y):
if self.tname==None:
raise Exception("No texture name for block. Did you forget to call setTextureName()?".format())
self.screen.blit(Block.background,(self.x*constants.TILESIZE,self.y*constants.TILESIZE))
self.screen.blit(Block.background,(x*constants.TILESIZE,y*constants.TILESIZE))
texture=self.getTexture()
if texture==False:
texture=Block.textures[self.tname]
self.screen.blit(texture,(self.x*constants.TILESIZE,self.y*constants.TILESIZE))
self.screen.blit(texture,(x*constants.TILESIZE,y*constants.TILESIZE))
def setTextureName(self,name):
if not name in Block.textures.keys():

View File

@ -30,7 +30,7 @@ class Character(Sprite):
def draw(self):
img=self.frames[self.dir][self.frame]
self.screen.blit(img,(self.x,self.y))
self.screen.blit(img,(self.x*constants.TILESIZE,self.y*constants.TILESIZE))
pygame.display.flip()
def move(self,dir):
@ -38,25 +38,25 @@ class Character(Sprite):
old_y=self.y
self.dir=dir
if dir=="up":
self.y-=constants.PLAYER_MOVE
self.y-=1
elif dir=="down":
self.y+=constants.PLAYER_MOVE
self.y+=1
elif dir=="left":
self.x-=constants.PLAYER_MOVE
self.x-=1
elif dir=="right":
self.x+=constants.PLAYER_MOVE
self.x+=1
self.frame+=1
if self.frame>2:
self.frame=0
if self.x>=constants.WINDWIDTH:
if self.x>=constants.MAPWIDTH:
self.x=old_x
if self.x<0:
self.x=old_x
if self.y>=(constants.WINDHEIGHT-(constants.EXTRAROWS*constants.TILESIZE)):
if self.y>=constants.MAPHEIGHT:
self.y=old_y
if self.y<0:
self.y=old_y
tile=self.map.tileAt(self.x/constants.TILESIZE,self.y/constants.TILESIZE)
tile=self.map.tileAt(self.x,self.y)
if not tile.clear:
self.x=old_x
self.y=old_y

View File

@ -1,9 +1,10 @@
TILESIZE=16
MAPWIDTH=32
MAPHEIGHT=32
MAPWIDTH=64
MAPHEIGHT=64
PORTHEIGHT=32
PORTWIDTH=32
EXTRAROWS=1
WINDWIDTH=MAPWIDTH*TILESIZE
WINDHEIGHT=(MAPHEIGHT+EXTRAROWS)*TILESIZE
PLAYER_MOVE=TILESIZE
WINDWIDTH=PORTWIDTH*TILESIZE
WINDHEIGHT=(PORTHEIGHT+EXTRAROWS)*TILESIZE
BACKGROUND="grass"
FONTSIZE=30

View File

@ -5,16 +5,25 @@ from . import block,gameregistry
from . import constants
from .gameregistry import GameRegistry
from .block import Block
class Map(Group):
class Map:
def __init__(self,screen):
super().__init__()
self.tiles=[]
self.screen=screen
i=0
while i<constants.MAPHEIGHT:
arr=[]
j=0
while j<constants.MAPWIDTH:
arr.append(None)
j+=1
self.tiles.append(arr)
i+=1
self.generate()
def addTile(self,tname,x,y):
klass=GameRegistry.block_classes[tname]
tile=klass(x,y,self.screen)
self.add(tile)
self.tiles[y][x]=tile
def generate(self):
y=0
@ -33,15 +42,27 @@ class Map(Group):
x+=1
y+=1
def draw(self):
for s in self.sprites():
s.draw()
def draw(self,centerx,centery):
topleftx=centerx-16
toplefty=centery-16
x=topleftx
y=toplefty
while True:
tile=self.tileAt(x,y)
if tile:
tile.draw(x-topleftx,y-toplefty)
x+=1
if x==topleftx+constants.PORTWIDTH:
x=topleftx
y+=1
if y==toplefty+constants.PORTHEIGHT:
break
pygame.display.flip()
def tileAt(self,x,y):
tile=None
for s in self.sprites():
if s.x==x and s.y==y:
tile=s
break
return tile
if x<0 or y<0:
return None
try:
return self.tiles[y][x]
except IndexError as e:
return None

View File

@ -8,8 +8,8 @@ class Player(Character):
self.uname=uname
def facingTile(self):
x=self.x/constants.TILESIZE
y=self.y/constants.TILESIZE
x=self.x
y=self.y
if self.dir=="up":
y-=1
elif self.dir=="down":
@ -27,7 +27,7 @@ class Player(Character):
if y<0:
return False
return (x,y)
def interact(self):
coords=self.facingTile()
if coords==False:
@ -61,3 +61,12 @@ class Player(Character):
drop=tile.drops[0]
amount=tile.drops[1]
self.inv.addTile(drop,amount)
def draw(self):
oldx=self.x
oldy=self.y
self.x=16
self.y=16
super().draw()
self.x=oldx
self.y=oldy

View File

@ -28,7 +28,7 @@ class PlayerImg(Sprite):
self.dir="right"
self.map=map
def draw(self):
def draw(self,x,y):
img=self.frames[self.dir][self.frame]
self.screen.blit(img,(self.x,self.y))
self.screen.blit(img,(x*constants.TILESIZE,y*constants.TILESIZE))
pygame.display.flip()

20
serv.py
View File

@ -1,10 +1,14 @@
import pygame
import sys
def my_load(path):
return None
mod=sys.modules["pygame.image"]
mod.load=my_load
sys.modules["pygame.image"]=mod
import os
import random
import recipes
import sys
import select
import blocks
import socket
import _thread
import pickle
@ -16,13 +20,13 @@ from lib.gameregistry import GameRegistry
from lib.map import Map
from lib.character import Character
from lib.block import Block
from player import *
from lib.player import Player
from time import sleep
uid_map={}
pos_map={}
map_changes={}
next_uid=0
import blocks
def recv_str(sock):
str=""
ch=""
@ -165,19 +169,19 @@ def handle_cmds():
if len(cmd)>0:
if cmd[0]=="stop":
s.close()
f=open("map_{}.pkl".format(map_name),"wb")
f=open("worlds/map_{}.pkl".format(map_name),"wb")
pickle.dump(map,f)
f.close()
exit(1)
def exit_cleanup(signal,frame):
s.close()
f=open("map_{}.pkl".format(map_name),"wb")
f=open("worlds/map_{}.pkl".format(map_name),"wb")
pickle.dump(map,f)
f.close()
Block.init()
files=glob.glob("map_*.pkl")
files=glob.glob("worlds/map_*.pkl")
if len(files)==0:
new="y"
else:
@ -194,7 +198,7 @@ if new=="n" or new=="no":
map_map.append(name)
i+=1
map_name=map_map[int(input("Which world?"))-1]
f=open("map_{}.pkl".format(map_name),"rb")
f=open("worlds/map_{}.pkl".format(map_name),"rb")
map=pickle.load(f)
f.close()
else: