pygame2D我的世界简单版

pygame2D我的世界简单版

import random
import pygame
from pygame.locals import *
form player import Player    # 导入Player玩家类

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

# 图片加载字典
block_images = {
             'DIRT':pygame.image.load('blocks/dirt.png'),
             'GRASS':pygame.image.load('blocks/grass.png'),
             'COAL':pygame.image.load('blocks/coal.png'),
             'WOOD' : pygame.image.load('blocks/wood.png'),
             'SAND' : pygame.image.load('blocks/sand.png'),
             'ROCK' : pygame.image.load('blocks/rock.png'),
             'HEAD' : pygame.image.load('blocks/head.png'),
             'FACE' : pygame.image.load('blocks/face.png'),
             'BODY' : pygame.image.load('blocks/body.png'),
             'LEG' : pygame.image.load('blocks/leg.png'),
             'FOOT' : pygame.image.load('blocks/foot.png'),
             'WATER' : pygame.image.load('blocks/water.png')
            }

animal_images = {  
             'spide':pygame.image.load('spide.png')
             
             }
# 爆炸图像表
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()        
        # 产生不同的坐标点,防止产生相同的,所以用了类变量
        # 如果有相同的则再次产生
        while True:
            x = TILE_SIZE * random.randint(0,COLS)
            y = TILE_SIZE * random.randint(0,ROWS-1)
            if (x,y) in Block.cors:continue
            Block.cors.append((x,y))
            self.rect.left = x
            self.rect.top = y
            break
        self.group = group
        self.group.add(self)               # 把自己加入到组中
        
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()
        self.rect.topleft = position
        self.dx = random.randint(-5,5)
        self.dy = random.randint(-5,5)
        group.add(self)                  # 添加到蜘蛛组
        
    def update(self):
        if random.randint(1,30):
            self.dx = random.randint(-5,5)
            self.dy = random.randint(-5,5)
            self.rect.move_ip(self.dx,self.dy)
        

class Explosion(pygame.sprite.Sprite):
    """爆炸类"""
    def __init__(self,position,screen,group):
       """参数说明:
       position:爆炸的位置
       images:surface对象列表
       group:所在的组
       screen:在这个面上渲染
       """
       pygame.sprite.Sprite.__init__(self)
       self.screen = screen                     # 能访问屏幕
       self.group = group
       group.add(self)                          # 把自己加到组里面去
       self.images = explosion_images           # 造型,已经用pygame.image.load转换成surface有
       self.index = 0
       self.amounts = len(self.images)              
       self.rects = [image.get_rect() for image in self.images]  # 矩形对象列表        
       for rect in self.rects: rect.center = position
       self.interval = 2                         # 切换造型延时
       self.counter = 0                          # 切换造型延时计数器
      
    def draw(self):
        """在screen上渲染""" 
        if self.index < self.amounts:
           self.screen.blit(self.images[self.index],self.rects[self.index]) # 在screen上渲染自己
           if self.counter < self.interval:      # 起到时间间隔作用                                      
              self.counter += 1
           else:            
              self.counter = 0
              self.index = self.index + 1        # 超过则索引加1,下一帧图则会显示       
        else:
           
           self.group.remove(self)

You must log in to read the rest of this article. Please log in or register as a user.