欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python+pygame簡(jiǎn)單畫板實(shí)現(xiàn)代碼實(shí)例

 更新時(shí)間:2017年12月13日 10:08:57   作者:legion  
這篇文章主要介紹了python+pygame簡(jiǎn)單畫板實(shí)現(xiàn)代碼實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下。

疑問(wèn):pygame已經(jīng)過(guò)時(shí)了嗎?

過(guò)沒(méi)過(guò)時(shí)不知道,反正這玩意官方已經(jīng)快四年沒(méi)有更新了。用的人還是蠻多的(相對(duì)于其他同類項(xiàng)目),不過(guò)大家都是用來(lái)寫寫小東西玩一玩,沒(méi)有人用這個(gè)做商業(yè)項(xiàng)目。pygame其實(shí)就是SDL的python綁定,SDL又是基于OpenGL,所以也有人用pygame+pyOpenGL做3D演示什么的。真的要寫游戲的話pygame的封裝比較底層,不太夠用,很多東西都要自己實(shí)現(xiàn)(當(dāng)然自由度也高)。文檔也不太好,好在前人留下了很多文章。拿來(lái)練手倒是很不錯(cuò)的選擇,可以用來(lái)實(shí)踐很多2D游戲中常用的思想和算法。如果是想要直接以上來(lái)拿來(lái)寫2D游戲的話還可以選擇cocos2D(注意不是iOS那個(gè),是Python的)這個(gè)的API設(shè)計(jì)的非常好,簡(jiǎn)單易用。還有場(chǎng)景管理、內(nèi)置的控制臺(tái)等等??上б灿幸荒隂](méi)更新……雖然作者說(shuō)會(huì)更新啦,估計(jì)他主攻Objective-C那個(gè)版本的cocos了,畢竟用的人多……幀動(dòng)畫之類的特性沒(méi)有真是很可惜(Objective-C的版本就有T_T)如果是想寫引擎的話可以試試pyglet。想寫3D試試panda3D或者python-orge,這倆我都沒(méi)用過(guò),不過(guò)大家都這么說(shuō),應(yīng)該錯(cuò)不了??偟膩?lái)說(shuō)拿python寫游戲的人少之又少,你寫完了別人玩還要裝環(huán)境,打包又各種bug,拿來(lái)試驗(yàn)游戲中的某種算法做原型還可以。真正寫還是算了。當(dāng)然了,題主要是根本就沒(méi)打算用pygame寫游戲就當(dāng)我什么都沒(méi)說(shuō)吧……

(以上來(lái)自知乎的回答,感謝!)

下面是畫板截圖

# -*- coding: utf-8 -*-
import pygame
from pygame.locals import *
import math
class Brush:
  def __init__(self, screen):
    self.screen = screen
    self.color = (0, 0, 0)
    self.size = 1
    self.drawing = False
    self.last_pos = None
    self.style = True
    self.brush = pygame.image.load("images/brush.png").convert_alpha()
    self.brush_now = self.brush.subsurface((0, 0), (1, 1)) 
  def start_draw(self, pos):
    self.drawing = True
    self.last_pos = pos 
  def end_draw(self):
    self.drawing = False 
  def set_brush_style(self, style):
    print("* set brush style to", style)
    self.style = style 
  def get_brush_style(self):
    return self.style 
  def get_current_brush(self):
    return self.brush_now 
  def set_size(self, size):
    if size < 1:
      size = 1
    elif size > 32:
      size = 32
    print("* set brush size to", size)
    self.size = size
    self.brush_now = self.brush.subsurface((0, 0), (size*2, size*2)) 
  def get_size(self):
    return self.size 
  def set_color(self, color):
    self.color = color
    for i in xrange(self.brush.get_width()):
      for j in xrange(self.brush.get_height()):
        self.brush.set_at((i, j),
                 color + (self.brush.get_at((i, j)).a,)) 
  def get_color(self):
    return self.color 
  def draw(self, pos):
    if self.drawing:
      for p in self._get_points(pos):
        if self.style:
          self.screen.blit(self.brush_now, p)
        else:
          pygame.draw.circle(self.screen, self.color, p, self.size)
      self.last_pos = pos
 
  def _get_points(self, pos):
    points = [(self.last_pos[0], self.last_pos[1])]
    len_x = pos[0] - self.last_pos[0]
    len_y = pos[1] - self.last_pos[1]
    length = math.sqrt(len_x**2 + len_y**2)
    step_x = len_x / length
    step_y = len_y / length
    for i in xrange(int(length)):
      points.append((points[-1][0] + step_x, points[-1][1] + step_y))
    points = map(lambda x: (int(0.5 + x[0]), int(0.5 + x[1])), points)
    return list(set(points)) 
class Menu:
  def __init__(self, screen):
    self.screen = screen
    self.brush = None
    self.colors = [
      (0xff, 0x00, 0xff), (0x80, 0x00, 0x80),
      (0x00, 0x00, 0xff), (0x00, 0x00, 0x80),
      (0x00, 0xff, 0xff), (0x00, 0x80, 0x80),
      (0x00, 0xff, 0x00), (0x00, 0x80, 0x00),
      (0xff, 0xff, 0x00), (0x80, 0x80, 0x00),
      (0xff, 0x00, 0x00), (0x80, 0x00, 0x00),
      (0xc0, 0xc0, 0xc0), (0xff, 0xff, 0xff),
      (0x00, 0x00, 0x00), (0x80, 0x80, 0x80),
    ]
    self.colors_rect = []
    for (i, rgb) in enumerate(self.colors):
      rect = pygame.Rect(10 + i % 2 * 32, 254 + i / 2 * 32, 32, 32)
      self.colors_rect.append(rect)
    self.pens = [
      pygame.image.load("images/pen1.png").convert_alpha(),
      pygame.image.load("images/pen2.png").convert_alpha(),
    ]
    self.pens_rect = []
    for (i, img) in enumerate(self.pens):
      rect = pygame.Rect(10, 10 + i * 64, 64, 64)
      self.pens_rect.append(rect)
    self.sizes = [
      pygame.image.load("images/big.png").convert_alpha(),
      pygame.image.load("images/small.png").convert_alpha()
    ]
    self.sizes_rect = []
    for (i, img) in enumerate(self.sizes):
      rect = pygame.Rect(10 + i * 32, 138, 32, 32)
      self.sizes_rect.append(rect)
 
  def set_brush(self, brush):
    self.brush = brush
 
  def draw(self):
    for (i, img) in enumerate(self.pens):
      self.screen.blit(img, self.pens_rect[i].topleft)
    for (i, img) in enumerate(self.sizes):
      self.screen.blit(img, self.sizes_rect[i].topleft)
    self.screen.fill((255, 255, 255), (10, 180, 64, 64))
    pygame.draw.rect(self.screen, (0, 0, 0), (10, 180, 64, 64), 1)
    size = self.brush.get_size()
    x = 10 + 32
    y = 180 + 32
    if self.brush.get_brush_style():
      x = x - size
      y = y - size
      self.screen.blit(self.brush.get_current_brush(), (x, y))
    else:
      pygame.draw.circle(self.screen,
                self.brush.get_color(), (x, y), size)
    for (i, rgb) in enumerate(self.colors):
      pygame.draw.rect(self.screen, rgb, self.colors_rect[i])
  def click_button(self, pos):
    for (i, rect) in enumerate(self.pens_rect):
      if rect.collidepoint(pos):
        self.brush.set_brush_style(bool(i))
        return True
    for (i, rect) in enumerate(self.sizes_rect):
      if rect.collidepoint(pos):
        if i:
          self.brush.set_size(self.brush.get_size() - 1)
        else:
          self.brush.set_size(self.brush.get_size() + 1)
        return True
    for (i, rect) in enumerate(self.colors_rect):
      if rect.collidepoint(pos):
        self.brush.set_color(self.colors[i])
        return True
    return False
class Painter:
  def __init__(self):
    self.screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption("Painter")
    self.clock = pygame.time.Clock()
    self.brush = Brush(self.screen)
    self.menu = Menu(self.screen)
    self.menu.set_brush(self.brush)
  def run(self):
    self.screen.fill((255, 255, 255))
    while True:
      self.clock.tick(30)
      for event in pygame.event.get():
        if event.type == QUIT:
          return
        elif event.type == KEYDOWN:
          if event.key == K_ESCAPE:
            self.screen.fill((255, 255, 255))
        elif event.type == MOUSEBUTTONDOWN:
          if event.pos[0] <= 74 and self.menu.click_button(event.pos):
            pass
          else:
            self.brush.start_draw(event.pos)
        elif event.type == MOUSEMOTION:
          self.brush.draw(event.pos)
        elif event.type == MOUSEBUTTONUP:
          self.brush.end_draw()
      self.menu.draw()
      pygame.display.update()
def main():
  app = Painter()
  app.run()
 
if __name__ == '__main__':
  main()

總結(jié)

以上就是本文關(guān)于python+pygame簡(jiǎn)單畫板實(shí)現(xiàn)代碼實(shí)例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

  • mac系統(tǒng)裝python后pip命令不能用的解決方案

    mac系統(tǒng)裝python后pip命令不能用的解決方案

    這篇文章主要介紹了mac系統(tǒng)裝python后pip命令不能用的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 動(dòng)態(tài)規(guī)劃之矩陣連乘問(wèn)題Python實(shí)現(xiàn)方法

    動(dòng)態(tài)規(guī)劃之矩陣連乘問(wèn)題Python實(shí)現(xiàn)方法

    這篇文章主要介紹了動(dòng)態(tài)規(guī)劃之矩陣連乘問(wèn)題Python實(shí)現(xiàn)方法,較為詳細(xì)的分析了矩陣連乘問(wèn)題的概念、原理并結(jié)合實(shí)例形式分析了Python相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-11-11
  • python3實(shí)現(xiàn)斐波那契數(shù)列(4種方法)

    python3實(shí)現(xiàn)斐波那契數(shù)列(4種方法)

    這篇文章主要介紹了python3實(shí)現(xiàn)斐波那契數(shù)列(4種方法),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python在for循環(huán)中更改list值的方法【推薦】

    Python在for循環(huán)中更改list值的方法【推薦】

    這篇文章主要介紹了Python在for循環(huán)中更改list值的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-08-08
  • python命令行傳遞參數(shù)的兩種方式

    python命令行傳遞參數(shù)的兩種方式

    python在命令行運(yùn)行.py文件時(shí),如何在命令行傳遞參數(shù)給運(yùn)行程序,python默認(rèn)提供了sys模塊的系統(tǒng)參數(shù)屬性實(shí)現(xiàn)接收命令行中的外部參數(shù),本文給大家介紹了python命令行傳遞參數(shù)的兩種方式,需要的朋友可以參考下
    2024-05-05
  • opencv鎖定鼠標(biāo)定位的實(shí)現(xiàn)

    opencv鎖定鼠標(biāo)定位的實(shí)現(xiàn)

    本文主要介紹了opencv鎖定鼠標(biāo)定位的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • python起點(diǎn)網(wǎng)月票榜字體反爬案例

    python起點(diǎn)網(wǎng)月票榜字體反爬案例

    大家好,本篇文章主要講的是python起點(diǎn)網(wǎng)月票榜字體反爬案例,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • python中使用多線程改進(jìn)flask案例

    python中使用多線程改進(jìn)flask案例

    這篇文章主要介紹了使用多線程改進(jìn)flask案例,線程是指進(jìn)程內(nèi)的一個(gè)執(zhí)行單元,也是進(jìn)程內(nèi)的可調(diào)度實(shí)體.線程的劃分尺度小于進(jìn)程,使得多線程程序的并發(fā)性高,更多具體內(nèi)容,需要的小伙伴可以參考下面文章相關(guān)資料,希望對(duì)你有所幫助
    2022-03-03
  • Python中OpenCV圖像特征和harris角點(diǎn)檢測(cè)

    Python中OpenCV圖像特征和harris角點(diǎn)檢測(cè)

    Harris角點(diǎn)檢測(cè)算子是于1988年由CHris Harris & Mike Stephens提出來(lái)的。在具體展開(kāi)之前,不得不提一下Moravec早在1981就提出來(lái)的Moravec角點(diǎn)檢測(cè)算子。本文重點(diǎn)給大家介紹OpenCV圖像特征harris角點(diǎn)檢測(cè)知識(shí),一起看看吧
    2021-09-09
  • Pycharm連接遠(yuǎn)程mysql報(bào)錯(cuò)的實(shí)現(xiàn)

    Pycharm連接遠(yuǎn)程mysql報(bào)錯(cuò)的實(shí)現(xiàn)

    本文主要介紹了Pycharm連接遠(yuǎn)程mysql報(bào)錯(cuò)的實(shí)現(xiàn),文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08

最新評(píng)論