VirseCraft自制游戏,Python语言(仿Minecraft)

大家好,我是尘风,今给大家带来VirseCraft自制游戏,Python语言(仿Minecraft)

由于疏忽,后期会讲故事引擎库从Pygame换成Ursina,所以这是一个3D游戏,之后我会放全部源码

本项目使用了Pygame、Ursin、Pyyaml和Flask开源库

这是启动器

import os
import sys
import json
import socket
import random
import threading

import src.net.getNetwork
import src.version_check
import src.virsecraft.VirseCraft

world = None

game_main_thread = {}
along_play = True
right_PORT = False
__port = 0

file_path = os.path.abspath(__file__)
inOSpath = file_path.split("/VirseCraft/runFile.py")[0]

afford_PORT = []

try:
    with open(inOSpath+"/VirseCraft/resourse/info/port_info.json","r") as stream:
        afford_PORT = json.load(stream)
except:
    pass

if __name__ == '__main__':
    if not along_play:
        while not right_PORT:
            __port = random.randint(0, 65535)
            if __port not in afford_PORT:
                right_PORT = True
        src.net.getNetwork.app.run(host='0.0.0.0',port=80,debug=True)
    game_main_thread["error check"] = threading.Thread()
    src.version_check.check()
    world = src.virsecraft.VirseCraft.Game()
    world.run()

这是主程序入口

"""

本项目在Github上开源遵循CC协议
开源在Github,Gitee

作者:孤雪落尘风
项目启动时间:

"""

import io
import os
import os.path
import gc
import sys
import time
import json
import yaml
import random
import datetime
import threading
import webbrowser

import numpy
import pygame

import src.virsecraft.PlayerPlayingState
import src.logsets.logWritten
import src.virsecraft.RenderedSettings.RenderedDragon
import src.virsecraft.DEFINE
import src.virsecraft.listener
import src.virsecraft.FoodSystem.getFoodLevel
import src.virsecraft.FoodSystem.getFoodNums
import src.virsecraft.getAllBlokSetting
import src.virsecraft.getExpSettings
import src.virsecraft.GenerateMonster.BasicMonsterGenerate
import src.virsecraft.GenerateMonster.BossGenerate

delta_xy = (
            (-1,0),
            (0,0),
            (1,0),
            (0,-1),
            (0,1),(1,1)
            )

def getDxDy(x,y,dx=1,dy=1):
    return [
        [x,y],
        [x+dx,y],
        [x,y+dy],
        [x+dx,y+dy]
    ]

def getDxDyDz(x,y,z,dx=1,dy=1,dz=1):
    return [
        [x,y,z],
        [x+dx,y,z],
        [x,y+dy,z],
        [x,y,z+dz],
        [x+dx,y+dy,z],
        [x,y+dy,z+dz],
        [x+dx,y,z+dz],
        [x+dx,y+dy,z+dz]
    ]

def correctPostion(x,y,z,chunk):
    k = chunk*16
    return [
        int(x)&15+k,
        int(y)&15+k,
        int(z)&15+k
    ]

"""The gegin of the games all data"""

renderGameScreenAll = src.virsecraft.RenderedSettings.RenderedDragon.RenderedGameScreen(1)

getGlobalListenerGame = src.virsecraft.listener.GlobalGameListener()
getNonglobalListenerGame = src.virsecraft.listener.NonglobalGameListener()

getFoodSystemOptions = src.virsecraft.FoodSystem.getFoodLevel.getAboutFoodOpinitionSettings()
getFoodNumsLevelOptions = src.virsecraft.FoodSystem.getFoodLevel.getAboutFoodOpinitionSettings()

MonsterGenerateOptions = src.virsecraft.GenerateMonster.BasicMonsterGenerate.MonsterGenerate()
MonsterGenerateOptions.Tick_end_attack = 1200

getBOSSGenerateOptions = src.virsecraft.GenerateMonster.BossGenerate.GenerateBoss(0,0,0,0)

getAllBlockOptions = src.virsecraft.getAllBlokSetting.getBlock()

getExpOptions = src.virsecraft.getExpSettings.aboutExpSettngs()

import src.virsecraft.UpdateBlock
liquidUpdateOptions = src.virsecraft.UpdateBlock.UpdateLiquid(src.virsecraft.DEFINE.game_FPS)
airBlockUpdateOptions = src.virsecraft.UpdateBlock.UpdateAirBlock()
commonBlockUpdateOptions = src.virsecraft.UpdateBlock.UpdateCommonBlock()
electricityUpdateOptions = src.virsecraft.UpdateBlock.ElectricityUpdate(0.02,100,50)
specialBlockUpdateOptions = src.virsecraft.UpdateBlock.SpecialBlockUpdate()

gamePlayerGlobalStateOperation = src.virsecraft.PlayerPlayingState.PlayerStates()

"""The gegin of the games all data"""


class cgui(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("/Users/hc101/Desktop/VirseCraft/src/img/choose_gui.png")
        self.rect = self.image.get_rect()
        self.rect.center = [518, 670]

class bag(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("/Users/hc101/Desktop/VirseCraft/src/img/bag.png")
        self.rect = self.image.get_rect()
        self.rect.center = [src.virsecraft.DEFINE.WINDOW_WIDTH>>1,src.virsecraft.DEFINE.WINDOW_HEIGHT>>1]

class grass(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("/Users/hc101/Desktop/VirseCraft/src/img/grass.jpeg")
        self.rect = self.image.get_rect()
        self.rect.center = [src.virsecraft.DEFINE.WINDOW_WIDTH>>1,src.virsecraft.DEFINE.WINDOW_HEIGHT>>1]

cgui1 = cgui()
bag1 = bag()
grassList = []

class LoadingGameGlobalsData:
    def __init__(self):
        pass

    def FirstReadData(self,**kwargs):
        for fileName in range(kwargs):
            with open(fileName,"r") as stream:
                stream.read()

    def EndWriteData(self,**kwargs):
        for fileName in range(kwargs):
            with open(fileName,"w") as stream:
                stream.write()

    def CommonFileOptions(self,mode,**kwargs):
        if mode == "r":
            for fileName in range(kwargs):
                with open(fileName, "r") as stream:
                    stream.write()

        if mode == "w":
            for fileName in range(kwargs):
                with open(fileName, "w") as stream:
                    stream.write()



class Game:
    def __init__(self):
        pygame.init()
        pygame.mouse.set_visible(True)
        self.screen = pygame.display.set_mode((src.virsecraft.DEFINE.WINDOW_WIDTH,src.virsecraft.DEFINE.WINDOW_HEIGHT))
        pygame.display.set_caption(src.virsecraft.DEFINE.WINDOW_TITTLE)
        self.tick = 0
        self.fps = src.virsecraft.DEFINE.game_FPS
        self.game_begin = False
        self.player_info_in_game = [0, 0, 100]
        pygame.font.get_fonts()
        self.font_List = []
        self.font_List.append((pygame.font.Font("/Users/hc101/Library/Containers/com.3D4Medical.CAMac/Data/Documents/Mobile3D4MEngine/Resources/dist/7795b8913e7adb4ea26b49d95ed59e42.ttf",100)))
        self.font_List.append((pygame.font.Font("/Users/hc101/Library/Containers/com.3D4Medical.CAMac/Data/Documents/Mobile3D4MEngine/Resources/dist/7795b8913e7adb4ea26b49d95ed59e42.ttf",100)))
        self.font_List.append((pygame.font.Font("/Users/hc101/Library/Containers/com.3D4Medical.CAMac/Data/Documents/Mobile3D4MEngine/Resources/dist/7795b8913e7adb4ea26b49d95ed59e42.ttf",100)))
        self.startcd_f = self.font_List[0].render("Start Game", True, "white")
        self.choose_f = self.font_List[1].render(">", True, "white")
        self.more_f = self.font_List[2].render("More People",True,"white")
        self.mode_choose = 1
        self.choose_y = (src.virsecraft.DEFINE.WINDOW_HEIGHT>>1)-100
        self.bagState = False

        class wpl(pygame.sprite.Sprite):
            def __init__(self):
                pygame.sprite.Sprite.__init__(self)
                self.image = pygame.image.load("/Users/hc101/Desktop/VirseCraft/src/img/wpl.png")
                self.rect = self.image.get_rect()
                self.rect.center = [600, 670]

        self.wpl1 = wpl()

        self.thingDockID = 1
        self.thingDockIDName = (pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4, pygame.K_5, pygame.K_6, pygame.K_7, pygame.K_8, pygame.K_9)
        self.cgui_xy = (508, 530, 548, 568, 588, 608, 628, 648, 668)

        self.game_seed = 432423545

        if self.game_seed and isinstance(self.game_seed,(str,int,float)):
            self.game_seed = hash(self.game_seed)
        else:
            src.logsets.logWritten.write(' '.join((str(datetime.datetime.now()), "[Server]: the type of seed is wrong")))

        gc.collect()


    def run(self):
        gc.collect()
        step = 0
        sttime = False
        mst = 255
        src.logsets.logWritten.write(' '.join((str(datetime.datetime.now()),"[Server]: game start successfully")))
        while True:
            self.tick += 1

            if (self.thingDockID > 9 or self.thingDockID <= 0):
                self.thingDockID = 1

            cgui1.rect[0] = self.cgui_xy[self.thingDockID-1]
            if mst == 0:
                if not self.game_begin:
                    self.screen.fill("black")
                    self.screen.blit(self.startcd_f,((src.virsecraft.DEFINE.WINDOW_WIDTH>>1)-260,(src.virsecraft.DEFINE.WINDOW_HEIGHT>>1)-200))
                    self.screen.blit(self.choose_f, ((src.virsecraft.DEFINE.WINDOW_WIDTH>>1)-320, self.choose_y))
                    self.screen.blit(self.more_f, ((src.virsecraft.DEFINE.WINDOW_WIDTH>>1)-260, (src.virsecraft.DEFINE.WINDOW_HEIGHT>>1)-100))
                else:
                    self.screen.fill("white")
                    self.screen.blit(self.wpl1.image, self.wpl1.rect)
                    self.screen.blit(cgui1.image, cgui1.rect)
                    if self.bagState:
                        self.screen.fill("black")
                        self.screen.blit(bag1.image,bag1.rect)
                        self.screen.blit(self.wpl1.image, self.wpl1.rect)
                        self.screen.blit(cgui1.image, cgui1.rect)

            if not self.tick%60:
                pass

            if not self.tick%120:
                pass

            if not self.tick%180:
                src.logsets.logWritten.write(' '.join((str(datetime.datetime.now()),"[Server]: game run naturl")))
                print(src.logsets.logWritten.gameLog[-1])

            if not self.tick%240:
                pass

            if not self.tick%300:
                gamePlayerGlobalStateOperation.playerInfoDateDict["foodNums"] = getFoodSystemOptions.getTickAllFoodLevelNums(gamePlayerGlobalStateOperation.playerInfoDateDict["foodNums"][2])

            if not self.tick%360:
                pass

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_w:
                        pass
                    if event.key == pygame.K_s:
                        pass
                    if event.key == pygame.K_a:
                        pass
                    if event.key == pygame.K_d:
                        pass
                    if event.key == pygame.K_e:
                        self.bagState = not self.bagState
                    if event.key == pygame.K_TAB:
                        pass
                    if not self.game_begin:
                        if event.key == pygame.K_UP:
                            if self.mode_choose == 2:
                                self.mode_choose >>= 1
                                self.choose_y = (src.virsecraft.DEFINE.WINDOW_HEIGHT>>1) - 200
                        if event.key == pygame.K_DOWN:
                            if self.mode_choose == 1:
                                self.mode_choose <<= 1
                                self.choose_y = (src.virsecraft.DEFINE.WINDOW_HEIGHT>>1) - 100
                        if event.key == pygame.K_RETURN:
                            self.game_begin = True
                    if (event.key in self.thingDockIDName):
                        self.thingDockID = self.thingDockIDName.index(event.key)+1
                        self.cgui1 = self.cgui_xy[self.thingDockID-1]
            if not sttime:
                if step<900:
                    self.screen.fill("red")
                    pygame.draw.rect(self.screen, (255, 255, 255), ((src.virsecraft.DEFINE.WINDOW_WIDTH>>1)-480, 500, 900, 35))
                    pygame.draw.rect(self.screen, (0, 0, 0), ((src.virsecraft.DEFINE.WINDOW_WIDTH>>1)-480, 500,step%900, 35))
                else:
                    mst -= 1
                    if mst >= 1:
                        self.screen.fill((mst-1,0,0))
                    else:
                        sttime = True

                step += 1

            pygame.display.update()

if __name__ == '__main__':
    test_world = Game()
    test_world.run()

点个关注支持一下吧

你可能感兴趣的:(游戏,python,pygame)