pygame-会写字的瓦力walli

转载请注明:@小五义http://www.cnblogs.com/xiaowuyi

题目:用walli在屏幕上写出指定的文字
思路:利用地图,指引walli写出文字。walli要有笔运动的轨迹。
方法:利用mapmake.py完成地图制作,其中右键建立笔运动轨迹,左建为字的笔划。运行walli.py,点击左键后walli会自动写字。因为walli是按运动轨迹运动,所以在制作地图时,要注意下笔顺序。
具体代码:

制作地图:mapmake.py

复制代码
# -*- coding: cp936 -*-
#@小五义 http://www.cnblogs.com/xiaowuyi
#右键画出绿色的点,表示walli的运动轨迹,左键画出红色的点,表示字的笔划。
import pygame,sys,os
import time
from pygame.locals import * 

pygame.init()
screencaption=pygame.display.set_caption('hello world')
screen=pygame.display.set_mode((800, 600),FULLSCREEN)
screen.fill([255,255,255])
width=10#记录每个格的宽度
height=10#记录每个格的高度
left=0
top=0
oldx=0
oldy=0
#判断地图文件是否存在
if os.path.isfile('map.dat'):
    os.remove('map.dat')
#画出表格
while top<601:
    for i in range(800/10):
        pygame.draw.rect(screen,[0,0,0],[left+i*10,top,width,height],1)
    top+=10

pygame.display.flip()
while True:
    for event in pygame.event.get():
        if (event.type==pygame.QUIT or (event.type==pygame.KEYDOWN and event.key==pygame.K_ESCAPE)):
            sys.exit()
        

    # 这里获取鼠标的按键情况
    pressed_mouse = pygame.mouse.get_pressed()
    if pressed_mouse[0]:#按下左键
        x, y = pygame.mouse.get_pos() 
        f=open('map.dat','a')
        if ((x/10)*10)!=oldx or ((y/10)*10)!=oldy:
            f.write(str((x/10)*10)+','+str((y/10)*10)+',1\n') 
            oldx=((x/10)*10)
            oldy=((y/10)*10)
        f.close()
        pygame.draw.rect(screen,[255,0,0],[(x/10)*10,(y/10)*10,width,height],0) 
    if pressed_mouse[2]:#按下右键
        x, y = pygame.mouse.get_pos() 
        f=open('map.dat','a')
        if ((x/10)*10)!=oldx or ((y/10)*10)!=oldy:
            f.write(str((x/10)*10)+','+str((y/10)*10)+',0\n') 
            oldx=((x/10)*10)
            oldy=((y/10)*10)
        f.close()
        pygame.draw.rect(screen,[0,255,0],[(x/10)*10,(y/10)*10,width,height],0) 
    
    pygame.display.update()
复制代码

walli写字:walli.py

复制代码
# -*- coding: cp936 -*-
#@小五义 http://www.cnblogs.com/xiaowuyi
#按照地图画出轨迹
import pygame,sys,os
import time
from pygame.locals import * 

pygame.init()
screencaption=pygame.display.set_caption('hello world')
screen=pygame.display.set_mode((800, 600),FULLSCREEN)
screen.fill([0,0,0])
width=10
height=10
left=0
top=0
oldx=0
oldy=0
if os.path.isfile('map.dat'):
    pass
else:
    print 'no file!'
    sys.exit()
my_pic=pygame.image.load('wali2.jpg')
my_yan=pygame.image.load('yan.jpg')
my_zheban=pygame.image.load('zheban.jpg')
locationxy=[]
goldxy=[]#记录要画的点
pygame.display.update()
while True:
    for event in pygame.event.get():
        if (event.type==pygame.QUIT or (event.type==pygame.KEYDOWN and event.key==pygame.K_ESCAPE)):
            sys.exit()
        
    
    pressed_mouse = pygame.mouse.get_pressed()
    if pressed_mouse[0]:
        f=open('map.dat','r')
        lines=f.readlines()
        for i in lines:##如果这句改为for i in lines[::-1]:会发现walli在倒着写字
            locationxy=i.split(',')
            oldxy=[oldx,oldy]
            screen.blit(my_zheban,oldxy)
            if '1' in locationxy[2]:
               goldxy.append(oldxy)
               
           
                
            xy=[int(locationxy[0]),int(locationxy[1])]
            time.sleep(0.051)
            screen.blit(my_pic,xy)
            for j in goldxy:
                screen.blit(my_yan,j)
            pygame.display.update()
            oldx=int(locationxy[0])
            oldy=int(locationxy[1])
        f.close()
    
复制代码

两张效果图:

文件下载地址:http://www.kuaipan.cn/file/id_749007936687977.htm

posted @   小五义  阅读(2450)  评论(1编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示