pygame2D我的世界简易源代码

python 2d minecraft simple version我的世界2Dpygame
python 2d minecraft simple version我的世界2Dpygame

本人用pygame做的2D简单版本。我的世界,你懂的。这个版本用石头、碳、水可以合成钥匙。以下是代码预览:

import random
import pygame
from pygame.locals import *

TILE_SIZE = 30
STEVE_SPEED = 30
SIZE = SCREEN_WIDTH,SCREEN_HEIGHT = 900,750
TRAY_HEIGHT = 120
ROWS = SCREEN_HEIGHT // TILE_SIZE
COLS = SCREEN_WIDTH // TILE_SIZE

# 爆炸图像表
explosion_images = [pygame.image.load(f'explosion/{i}.gif') for i in range(6)]

class Block(pygame.sprite.Sprite):
    """方块类"""
    cors = []                             # 所有方块坐标表
    def __init__(self,master,group):
        pygame.sprite.Sprite.__init__(self)
        self.image_index = random.choice(list(block_images.keys()))
        self.image = block_images[self.image_index]                 
        self.rect = self.image.get_rect()        
        
class Spide(pygame.sprite.Sprite):
    """蜘蛛类,它一生成会自动攻击史蒂夫,给蜘蛛加上碰到边缘就反弹等AI行为"""
    def __init__(self,position,image,group):
        """image:蜘蛛的surface"""
        pygame.sprite.Sprite.__init__(self)
        self.image = image
        self.rect = self.image.get_rect()
         
class Explosion(pygame.sprite.Sprite):
    """爆炸类"""
    def __init__(self,position,screen,group):

 
class Bullet(pygame.sprite.Sprite):
    """子弹类,史蒂夫按空格会发射子弹"""
    def __init__(self,master):
        """master:史蒂夫"""

        

def make_background():
    """画格子,做为背景,返回一个surface"""
    color = (25,95,12)
    surface = pygame.Surface(SIZE)
    surface.fill((25,105,0))
        
def main():
    
    house_images = ["house/house_open.png","house/house_close.png"] 
    steve_images = ["player/player_up.png","player/player_down.png","player/player_left.png","player/player_right.png"]
    pygame.init()
    screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT+TRAY_HEIGHT))
    pygame.display.set_caption("pygame我的世界简单版")

    block_group = pygame.sprite.Group()    # 方块组
    spide_group = pygame.sprite.Group()    # 蜘蛛组
    explosion_group = pygame.sprite.Group()# 爆炸组
    
    background = make_background()         # 产生背景图
    tip_tray = pygame.image.load("tray.png")
    keyimage = pygame.image.load("key.png")

    # 新建房子,有两个造型
    house_images = [pygame.image.load(image) for image in house_images]    
    house = House(house_images)

    steve_position = 2 * TILE_SIZE, SCREEN_HEIGHT - 2 * TILE_SIZE
    steve_images = [pygame.image.load(image).convert_alpha() for image in steve_images]    
    steve = Player(steve_images,steve_position,house,screen,block_group,spide_group,explosion_group)
    
    [Block(steve,block_group)for i in range(100)] # 生成一百个方块

    clock = pygame.time.Clock()
    running = True
    
    while running:                          # 尽量缩短while循环体长度
        event = pygame.event.poll()         # 从队列取一个事件
        if event.type == QUIT:running = False
        if event.type == KEYDOWN:
            steve.keypress_check(event)      # 按键检测会调用update更新坐标等            
            steve.house_detect()             # 是否在房子里外检测         

        spide_group.update()
        
        screen.blit(background,(0,0))        # 画背景
        screen.blit(tip_tray,(0,SCREEN_HEIGHT))
        block_group.draw(screen)             # 画所有方块 
        screen.blit(house.image,house.rect)  # 画房子
        spide_group.draw(screen)             # 画所有蜘蛛
        screen.blit(steve.image,steve.rect)  # 画史蒂夫
        
        [bomb.draw()  for bomb in  explosion_group] # 这里不用group的draw方法,因为它要切换造型             
        
        if steve.key ==  3 :  screen.blit(keyimage,(SCREEN_WIDTH-100,SCREEN_HEIGHT+30))
        
        pygame.display.update()
        clock.tick(60)
        
    pygame.quit()        

if __name__ == "__main__":

    main()  
    

下载完整源代码与素材,请扫码付款。

VIP免费查看

已有0人支付

本站所有作品,教程等皆为原创,版权所有。只供个人及单位内部研究使用,对外展示或传播必需经本站同意,且注明来自本站。培训机构等用本站资源培训学生,需经本站授权。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。亦可直接向微信号scratch8付款购买。入住QQ群:225792826 和爱好者共同交流,并且能下载免费提供的Python资源(需提供真实姓名才可入群)
李兴球的博客_Python创意编程技术前沿_pygame » pygame2D我的世界简易源代码
scratch

学本领,探索更大的世界!

李兴球博客 风火轮编程主页