1. 程式人生 > 實用技巧 >帶你用python利用小豬佩奇製作四個小遊戲

帶你用python利用小豬佩奇製作四個小遊戲

本文的文字及圖片來源於網路,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯絡我們以作處理

以下文章來源於騰訊雲 作者:Python進階者

( 想要學習Python?Python學習交流群:1039649593,滿足你的需求,資料都已經上傳群檔案流,可以自行下載!還有海量最新2020python學習資料。 )

本文說明

為什麼要學習python?是因為不僅社會上很多工作需要用到python,同時我們可以利用python做很多好玩兒的事兒,

PS:我幫女朋友曾經做了個人的動態二維碼和圖片切分為九宮格,她就很開心呀!畢竟她是文科生,就會覺得你很牛逼!所以有趣的技術還是需要學習。

當然python還有很多用途,等待著我們去發現,相信它會給你的學習生活帶來樂趣。

本文同樣是利用小豬佩奇帶著大家學習python的幾個好玩兒的操作(大綱如下),俗話說:“興趣是最好的老師”,我們只有喜歡上一門語言,才會有學好它的慾望。

  • 1.利用python繪製一個小豬佩奇;
  • 2.利用python給小豬佩奇換背景色;
  • 3.利用python將小豬佩奇切分為九宮格;
  • 4.利用python製作小豬佩奇動態二維碼;

1.利用python繪製一個小豬佩奇

turtle庫是一個很好的python圖形繪製庫,利用它我們可以繪製各種各樣的圖形、小動物。這個庫其實並不難,實際你怎麼繪製這個圖形,對應的程式碼,就跟著你的實際繪製圖形的方向走下去,即可。PS:當你有孩子了,是不是既可以利用turtle教他們繪製某些圖案,還可以培養他們的程式設計興趣,好處很多哦!

from turtle import *

# 繪製鼻子
def nose(x,y):
    penup()
    goto(x,y)
    pendown()
    setheading(-30)
    begin_fill()
    a=0.4
    for i in range(120):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            left(3) 
            forward(a) 
        else:
            a=a-0.08
            left(
3) forward(a) end_fill() penup() setheading(90) forward(25) setheading(0) forward(10) pendown() pencolor(255,155,192) setheading(10) begin_fill() circle(5) color(160,82,45) end_fill() penup() setheading(0) forward(20) pendown() pencolor(255,155,192) setheading(10) begin_fill() circle(5) color(160,82,45) end_fill() # 繪製頭部 def head(x,y): color((255,155,192),"pink") penup() goto(x,y) setheading(0) pendown() begin_fill() setheading(180) circle(300,-30) circle(100,-60) circle(80,-100) circle(150,-20) circle(60,-95) setheading(161) circle(-300,15) penup() goto(-100,100) pendown() setheading(-30) a=0.4 for i in range(60): if 0<=i<30 or 60<=i<90: a=a+0.08 left(3) forward(a) else: a=a-0.08 left(3) forward(a) end_fill() # 繪製耳朵 def ears(x,y): color((255,155,192),"pink") penup() goto(x,y) pendown() begin_fill() setheading(100) circle(-50,50) circle(-10,120) circle(-50,54) end_fill() penup() setheading(90) forward(-12) setheading(0) forward(30) pendown() begin_fill() setheading(100) circle(-50,50) circle(-10,120) circle(-50,56) end_fill() # 繪製眼睛 def eyes(x,y): color((255,155,192),"white") penup() setheading(90) forward(-20) setheading(0) forward(-95) pendown() begin_fill() circle(15) end_fill() color("black") penup() setheading(90) forward(12) setheading(0) forward(-3) pendown() begin_fill() circle(3) end_fill() color((255,155,192),"white") penup() setheading(90) forward(-25) setheading(0) forward(40) pendown() begin_fill() circle(15) end_fill() color("black") penup() setheading(90) forward(12) setheading(0) forward(-3) pendown() begin_fill() circle(3) end_fill() # 繪製腮幫 def cheek(x,y): color((255,155,192)) penup() goto(x,y) pendown() setheading(0) begin_fill() circle(30) end_fill() # 繪製嘴巴 def mouth(x,y): color(239,69,19) penup() goto(x,y) pendown() setheading(-80) circle(30,40) circle(40,80) # 繪製身體 def body(x,y): color("red",(255,99,71)) penup() goto(x,y) pendown() begin_fill() setheading(-130) circle(100,10) circle(300,30) setheading(0) forward(230) setheading(90) circle(300,30) circle(100,3) color((255,155,192),(255,100,100)) setheading(-135) circle(-80,63) circle(-150,24) end_fill() # 繪製手 def hands(x,y): color((255,155,192)) penup() goto(x,y) pendown() setheading(-160) circle(300,15) penup() setheading(90) forward(15) setheading(0) forward(0) pendown() setheading(-10) circle(-20,90) penup() setheading(90) forward(30) setheading(0) forward(237) pendown() setheading(-20) circle(-300,15) penup() setheading(90) forward(20) setheading(0) forward(0) pendown() setheading(-170) circle(20,90) # 繪製腳 def foot(x,y): pensize(10) color((240,128,128)) penup() goto(x,y) pendown() setheading(-90) forward(40) setheading(-180) color("black") pensize(15) forward(20) pensize(10) color((240,128,128)) penup() setheading(90) forward(40) setheading(0) forward(90) pendown() setheading(-90) forward(40) setheading(-180) color("black") pensize(15) forward(20) # 繪製尾巴 def tail(x,y): pensize(4) color((255,155,192)) penup() goto(x,y) pendown() setheading(0) circle(70,20) circle(10,330) circle(70,30) # 設定畫布和畫筆 def setting(): pensize(4) hideturtle() colormode(255) color((255,155,192),"pink") setup(840,500) speed(10) def main(): setting() # 畫布和畫筆設定 nose(-100,100) # 鼻子 head(-69,167) # ears(0,160) # 耳朵 eyes(0,140) # 眼睛 cheek(80,10) # 腮幫 mouth(-20,30) # 嘴巴 body(-32,-8) # 身體 hands(-56,-45) # foot(2,-177) # tail(148,-155) # 尾巴 done() # 結束 if __name__ == '__main__': main()

結果如下:

2.利用python給小豬佩奇換背景色

我們在昨天已經講述過怎麼使用python庫給證件照換底色,大家可以參考那篇文章學習一下。換背景色的原理:每一個影象都是由畫素點構成的,我們想要替換他們的顏色,就是找到每個畫素點對應的位置,然後用指定顏色,去替換它!一般證件照背景色並不是同一種藍色畫素點,無法完成畫素點的定位,這就需要我們對影象進行【腐蝕】或【膨脹】,完成圖片黑白話,這樣白色的畫素點是255,就可以很好的定位了。

import cv2
import numpy as np
# 讀取照片
img=cv2.imread('zhu.jpg')

# 影象縮放
img = cv2.resize(img,None,fx=0.5,fy=0.5)
rows,cols,channels = img.shape
print(rows,cols,channels)
cv2.imshow('img',img)

# 圖片轉換為灰度圖
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow('hsv',hsv)

# 圖片的二值化處理
lower_blue=np.array([90,70,70])
upper_blue=np.array([110,255,255])
mask = cv2.inRange(hsv, lower_blue, upper_blue)


#腐蝕膨脹
erode=cv2.erode(mask,None,iterations=1)
cv2.imshow('erode',erode)

dilate=cv2.dilate(erode,None,iterations=1)
cv2.imshow('dilate',dilate)

#遍歷替換
for i in range(rows):
  for j in range(cols):
    if erode[i,j]==255: # 畫素點為255表示的是白色,我們就是要將白色處的畫素點,替換為紅色
      img[i,j]=(0,0,255)#此處替換顏色,為BGR通道
cv2.imshow('res',img)

# 視窗等待的命令,0表示無限等待
cv2.waitKey(0)

結果如下:

3.利用python將小豬佩奇切分為九宮格

將圖片切分為九宮格的原理就是:找到圖片對應位置的座標,然後進行切割。由於是九宮格,我們切分的是3*3,然後利用雙層迴圈遍歷對應位置的座標後,進行圖片切割。

from PIL import Image
import sys
#將圖片填充為正方形
def fill_image(image):
    width, height = image.size
    #選取長和寬中較大值作為新圖片的,小的地方,用圖片填充為等寬等高
    new_image_length = width if width > height else height
    #生成新圖片[白底]
    new_image = Image.new(image.mode, (new_image_length, new_image_length), color='white')
    #將之前的圖貼上在新圖上,居中
    if width > height:#原圖寬大於高,則填充圖片的豎直維度
        #(x,y)二元組表示貼上上圖相對下圖的起始位置
        new_image.paste(image, (0, int((new_image_length - height) / 2)))
    else:
        new_image.paste(image, (int((new_image_length - width) / 2),0))
    return new_image
#切圖
def cut_image(image):
    width, height = image.size
    item_width = int(width / 3)
    item_height = int(height / 3)
    box_list = []
    #雙重迴圈,生成9張圖片基於原圖的位置
    # 注意:圖片左上角是(0,0),右下角是(width,height)
    for i in range(0,3):
        for j in range(0,3):
            print((j*item_width,i*item_height,(j+1)*item_width,(i+1)*item_height))
            box = (j*item_width,i*item_height,(j+1)*item_width,(i+1)*item_height)
            box_list.append(box)
 
    image_list = [image.crop(box) for box in box_list]
    return image_list
#儲存
def save_images(image_list):
    index = 1
    for image in image_list:
        image.save(str(index) + '.jpg')
        index += 1
 
file_path = "zhuzhu.jpg"
image = Image.open(file_path)
image = fill_image(image)
image_list = cut_image(image)
save_images(image_list)

結果如下:

4.利用python製作小豬佩奇動態二維碼

程式碼說明:如果我們利用的背景圖是gif動態圖,生成的就是動態二維碼。如果利用的背景是靜態圖,生成的是靜態二維碼。

from MyQR import myqr
import matplotlib.pyplot as plt
# 生成的二維碼最終在你電腦的儲存位置
# 當你使用了動態圖作為背景,這裡可以寫成".gif",儲存出來的就是gif動態二維碼!
save_name = '傻逼1.gif'
myqr.run(
    # 該連結表示你想要生成二維碼的連結。
    words='https://blog.csdn.net/weixin_41261833',
    version=10,  # 容錯率
    level='H',  # 糾錯水平,範圍是L、M、Q、H,從左到右依次升高
    colorized=True, # False為黑白
    contrast=1.5,  # 用以調節圖片的對比度,1.0 表示原始圖片。
    brightness=1.0,  # 用來調節圖片的亮度。
    save_name=save_name,#儲存的檔名
    # 背景圖片的路徑,你如果給的是".png/.jpg"等靜態圖片,最終生成的就是靜態二維碼!
    # 背景圖片的路徑,你如果給的是".gif"等動態圖片,最終只需要儲存為".gif",生成的就是動態二維碼!
    picture=r"G:\1Pycharm_Project\csdn分享系列\小豬佩奇的幾個操作\動態.gif"
    )
# 檢視生成的二維碼圖片
img = Image.open(save_name) # 讀取所儲存的圖片展示二維碼
plt.figure("Image") # 影象視窗名稱
plt.imshow(img)
plt.axis('off') # 關掉座標軸為 off
plt.show()

結果如下: