python怎么建立画板_Python>>>使用Python和Pygame创建画板

下面是画板截图

# -*- 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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python使用Pygame创建游戏是一个很好的入门项目,因为它提供了一个简单易用的接口来处理图形、音频和输入事件。以下是一些基本步骤: 1. 安装Pygame:首先确保你已经安装了Python,然后通过命令行或pip安装Pygame。在命令行输入: ``` pip install pygame ``` 2. 导入模块:在你的Python脚本中,导入Pygame的基本模块: ```python import pygame from pygame.locals import * ``` 3. 初始化Pygame:在开始游戏之前,需要初始化pygame,这将设置窗口大小和其他配置: ```python pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("My Game") ``` 4. 游戏循环:这是游戏的核心部分,包括事件处理、更新游戏状态和绘制: ```python while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() # 渲染帧(更新游戏画面) screen.fill((0, 0, 0)) # 全屏黑色背景 # 在这里添加游戏逻辑和绘制 pygame.display.flip() # 刷新屏幕 ``` 5. 绘制和游戏逻辑:在`while`循环中,你可以创建游戏对象,处理它们的位置、颜色等,并在屏幕上画出它们。例如: ```python player = Player((100, 100)) # 假设Player是自定义类 player.update() screen.blit(player.image, player.rect) ``` 6. 结束游戏:在适当的时候退出游戏,如按下特定键或达到游戏目标: ```python if game_over: # 游戏结束条件 pygame.quit() quit() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值