python pygame编程日记01

因为看了很多视频,我想自己做一个类似于“波兰球之战”的游戏,想了一晚上,想好了。今天开始肝代码:

目录

01 获取世界地图的像素


01 获取世界地图的像素

首先,我需要一个世界地图(单色的),本来以为很难找,结果搜了一下”世界地图纯黑“就有了:

放心,这张图你可以用,因为接下来的程序会识别这些字。 

世界地图纯黑 - 搜索 图片 (bing.com)

接下来,要做的就是把每个图片的像素作为列表存到JSON文件里,程序如下:

import pygame
import json
pygame.init()
mapimg=pygame.image.load("world_map.jpeg")
rect=mapimg.get_rect()
rect.x,rect.y=0,0
width,height=rect.width,rect.height
screen=pygame.display.set_mode((width,height))
screen.blit(mapimg,rect)
pygame.display.flip()
mappix=[]
for x in range(width):
    mappix.append([])
    for y in range(height):
        pix=list(screen.get_at((x,y)))
        if (pix[0]>=200 and pix[0]<=255)and(pix[1]>=200 and pix[1]<=255)and(pix[0]>=200 and pix[0]<=255):
            mappix[x].append(pix)
        else:
            mappix[x].append(False)
with open("worldmap.map",'w')as file:
    json.dump(mappix,file)
pygame.quit()

 由于这张图不是纯色的,所以我把取色RGBAlpha定在了一个范围。

01 02 编写测试程序

import pygame
import json
pygame.init()
file=open("worldmap.map")
map_=json.load(file)
green=(0,255,0)
width=len(map_)
height=len(map_[0])
screen=pygame.display.set_mode((width,height))
while True:
    screen.fill((255,255,255))
    for x in range(0,width):
        for y in range(0,height):
            if map_[x][y]:
                pygame.draw.rect(screen,green,(x,y,1,1))
    pygame.display.flip()

这里就不多说了,相信各位都知道。 

02 编写主程序

02 01 Main初始常量

代码:

import pygame,sys
import json
class Main:
    def __init__(self):
        self.WIDTH,self.HEIGHT=self.load_worldmap()
        #              R  G  B
        self.BGCOLOR=(138,95,15)
        
    def load_worldmap(self):
        self.file=open("worldmap.map")
        self.wmap=json.load(self.file)
        return len(self.wmap),len(self.wmap[0])

load_worldmap读取了worldmap.map的地图像素,颜色我写了个棕色,还行。

02 02 编写主程序

代码如下:

import pygame,sys
import json
class Main:
    def __init__(self):
        self.WIDTH,self.HEIGHT=self.load_worldmap()
        #              R  G  B
        self.BGCOLOR=(138,95,15)
        
    def load_worldmap(self):
        self.file=open("worldmap.map")
        self.wmap=json.load(self.file)
        return len(self.wmap),len(self.wmap[0])
    
class Game():
    def __init__(self):
        pygame.init()
        self.data=Main()
        self.screen=pygame.display.set_mode((self.data.WIDTH,self.data.HEIGHT))
        self.screenrect=self.screen.get_rect()
        pygame.display.set_caption("World Imtate")
        self.run_game()
        
    def run_game(self):
        while True:
            self.screen.fill(self.data.BGCOLOR)
            self.get_event()
            self.draw_map()
            pygame.display.flip()
            
    def draw_map(self):
        for x in range(0,self.data.WIDTH):
            for y in range(0,self.data.HEIGHT):
                if self.data.wmap[x][y]:
                    pygame.draw.rect(self.screen,(self.data.wmap[x][y][0]-60\
                                                  ,self.data.wmap[x][y][1]\
                                                  ,self.data.wmap[x][y][0]-60,32)\
                                     ,(x,y,1,1))

    def get_event(self):
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                self.quit()
                
    def quit(self):
        pygame.quit()
        sys.exit()

if __name__=="__main__":
    Game()
        
            

时间不多,只是编了个基本框架, pygame.draw.rect(self.screen,(self.data.wmap[x][y][0]-60\
                                                  ,self.data.wmap[x][y][1]\
                                                  ,self.data.wmap[x][y][0]-60,32)\
                                     ,(x,y,1,1))

这部分的效果设想本来我觉得还行,但纯色就是纯色。没办法。

03 程序总览

03 01 index.py

import pygame,sys
import json
class Main:
    def __init__(self):
        self.WIDTH,self.HEIGHT=self.load_worldmap()
        #              R  G  B
        self.BGCOLOR=(138,95,15)
        
    def load_worldmap(self):
        self.file=open("worldmap.map")
        self.wmap=json.load(self.file)
        return len(self.wmap),len(self.wmap[0])
    
class Game():
    def __init__(self):
        pygame.init()
        self.data=Main()
        self.screen=pygame.display.set_mode((self.data.WIDTH,self.data.HEIGHT))
        self.screenrect=self.screen.get_rect()
        pygame.display.set_caption("World Imtate")
        self.run_game()
        
    def run_game(self):
        while True:
            self.screen.fill(self.data.BGCOLOR)
            self.get_event()
            self.draw_map()
            pygame.display.flip()
            
    def draw_map(self):
        for x in range(0,self.data.WIDTH):
            for y in range(0,self.data.HEIGHT):
                if self.data.wmap[x][y]:
                    pygame.draw.rect(self.screen,(self.data.wmap[x][y][0]-60\
                                                  ,self.data.wmap[x][y][1]\
                                                  ,self.data.wmap[x][y][0]-60,32)\
                                     ,(x,y,1,1))

    def get_event(self):
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                self.quit()
                
    def quit(self):
        pygame.quit()
        sys.exit()

if __name__=="__main__":
    Game()
        
            

03 02 load_world_map.py

import pygame
import json
pygame.init()
mapimg=pygame.image.load("world_map.jpeg")
rect=mapimg.get_rect()
rect.x,rect.y=0,0
width,height=rect.width,rect.height
screen=pygame.display.set_mode((width,height))
screen.blit(mapimg,rect)
pygame.display.flip()
mappix=[]
for x in range(width):
    mappix.append([])
    for y in range(height):
        pix=list(screen.get_at((x,y)))
        if (pix[0]>=200 and pix[0]<=255)and(pix[1]>=200 and pix[1]<=255)and(pix[0]>=200 and pix[0]<=255):
            mappix[x].append(pix)
        else:
            mappix[x].append(False)
with open("worldmap.map",'w')as file:
    json.dump(mappix,file)
pygame.quit()

03 02 test_map.py

import pygame
import json
pygame.init()
file=open("worldmap.map")
map_=json.load(file)
green=(0,255,0)
width=len(map_)
height=len(map_[0])
screen=pygame.display.set_mode((width,height))
while True:
    screen.fill((255,255,255))
    for x in range(0,width):
        for y in range(0,height):
            if map_[x][y]:
                pygame.draw.rect(screen,green,(x,y,1,1))
    pygame.display.flip()

03 04 运行效果

明天再见! 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值