zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    场景法设计测试用例
    编写边界条件测试用例原则
    CSS
    JavaScript
    HTML
    lamp安装教程
    Linux(lamp安装)
    oracle sqlplus常用命令
    利用同步器实现互斥锁
    工厂模式--简单工厂模式
  • 原文地址:https://www.cnblogs.com/xiaowuyi/p/2701760.html
Copyright © 2011-2022 走看看