Pygame

#级联编程(开火车式的编程) 让方法返回对象本身 f().add().mul()   return self
# pygame 去https://www.pygame.org/ 了解pygame
#faststone capture 下载这个工具可以录屏,量屏,屏幕拾色器
#下面演示一下不用面向对象的方式,进行游戏编程,代码如下:
import pygame

EMPTY = 0
BLACK = 1
WHITE = 2


def main():
    board = [[EMPTY] * 15 for _ in range(15)]
    pygame.init()
    screen = pygame.display.set_mode([640, 640]) #创建窗口
    screen.fill([255, 255, 0]) #添加背景颜色
    black_color = [0, 0, 0]
    white_color = [255, 255, 255]
    for index in range(1, 16):
        pygame.draw.line(screen,black_color, [40, 40 * index], [600, 40 * index], 1)
        pygame.draw.line(screen, black_color, [40 * index, 40], [ 40 * index, 600], 1)
    pygame.draw.rect(screen, black_color, [36, 36, 568, 568], 4) #36,36表示起点的横纵坐标,568,568表示矩形的长宽
    pygame.draw.circle(screen, black_color, [320, 320], 5, 0)#5表示半径,0表示线条是实心
    pygame.draw.circle(screen, black_color, [160, 160], 5, 0)
    pygame.draw.circle(screen, black_color, [480, 480], 5, 0)
    pygame.draw.circle(screen, black_color, [480, 160], 5, 0)
    pygame.draw.circle(screen, black_color, [160, 480], 5, 0)

    for row in range(len(board)):
        for col in range(len(board[row])):
            if board[row][col] != EMPTY:
                ccolor = black_color \
                    if board[row][col] == BLACK else white_color
                pos = [40 * (col + 1), 40 * (row + 1)]
                pygame.draw.circle(screen, ccolor, pos, 20, 0)

    pygame.display.flip() #刷新窗口
    pygame.display.set_caption('五子棋') #创建标题
    running = True
    is_black_turn = True
    while running:
        for event in pygame.event.get(): #接受事件
            if event.type == pygame.QUIT: #事件类型是关闭事件
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1: #表示鼠标左键
                    x, y = event.pos #获取鼠标的坐标
                    if 40 <= x <= 600 and 40 <= y <= 600:
                        col = round((x - 40) / 40)
                        row = round((y - 40) / 40)
                        if board[row][col] == EMPTY:
                            board[row][col] = BLACK if is_black_turn else WHITE
                            is_black_turn = not is_black_turn
                            for row in range(len(board)):
                                for col in range(len(board[row])):
                                    if board[row][col] != EMPTY:
                                        ccolor = black_color \
                                            if board[row][col] == BLACK else white_color
                                        pos = [40 * (col + 1), 40 * (row + 1)]
                                        pygame.draw.circle(screen, ccolor, pos, 20, 0)
                            pygame.display.flip()

    pygame.quit()


if __name__ == '__main__':
    main()
pycharm中选择想要创建函数的代码,右键,refactor 下excract下,method,可以直接生成函数
# 下面一段代码用面向对象的思想实现了相同的功能,但是代码整体感觉更加清晰
import pygame
EMPTY = 0
BLACK = 1
WHITE = 2
black_color = [0, 0, 0]
white_color = [255, 255, 255]


class RenjuBoard(object):

    def __init__(self):
        self._board = [[]] * 15
        self.reset()

    def reset(self):
        for row in range(len(self._board)):
            self._board[row] = [EMPTY] * 15

    def draw(self, screen):
        for index in range(1, 16):
            pygame.draw.line(screen, black_color, [40, 40 * index], [600, 40 * index], 1)
            pygame.draw.line(screen, black_color, [40 * index, 40], [40 * index, 600], 1)
        pygame.draw.rect(screen, black_color, [36, 36, 568, 568], 4)  # 36,36表示起点的横纵坐标,568,568表示矩形的长宽
        pygame.draw.circle(screen, black_color, [320, 320], 5, 0)  # 5表示半径,0表示线条是实心
        pygame.draw.circle(screen, black_color, [160, 160], 5, 0)
        pygame.draw.circle(screen, black_color, [480, 480], 5, 0)
        pygame.draw.circle(screen, black_color, [480, 160], 5, 0)
        pygame.draw.circle(screen, black_color, [160, 480], 5, 0)
        for row in range(len(self._board)):
            for col in range(len(self._board[row])):
                if self._board[row][col] != EMPTY:
                    ccolor = black_color \
                        if self._board[row][col] == BLACK else white_color
                    pos = [40 * (col + 1), 40 * (row + 1)]
                    pygame.draw.circle(screen, ccolor, pos, 20, 0)

    def move(self,row,col,is_black):
        if self._board[row][col] == EMPTY:
            self._board[row][col] = BLACK if is_black else WHITE
            return True
        return False


def main():
    pygame.init()
    is_black = True
    pygame.display.set_caption('五子棋')  # 创建标题
    screen = pygame.display.set_mode([640, 640]) #创建窗口
    screen.fill([255, 255, 0]) #添加背景颜色
    r1 = RenjuBoard()
    r1.draw(screen)
    pygame.display.flip()  # 刷新窗口
    running = True
    while running:
        for event in pygame.event.get(): #接受事件
            if event.type == pygame.QUIT: #事件类型是关闭事件
                running = False
            elif event.type == pygame.KEYDOWN:
                pass
            elif event.type == pygame.MOUSEBUTTONDOWN \
                    and event.button == 1:
                x, y = event.pos
                row = round((y - 40) / 40)
                col = round((x - 40) / 40)
                if r1.move(row, col, is_black):
                    is_black = not is_black
                    screen.fill([255, 255, 0])
                    r1.draw(screen)
                    pygame.display.flip()

    pygame.quit()


if __name__ == '__main__':
    main()
"""
大球吃小球
"""
from random import randint
from math import sqrt, pi
import pygame


class Ball(object):

    def __init__(self, center, color, radius, sx, sy):
        """

        :param x: 横坐标
        :param y: 纵坐标
        :param color: 颜色
        :param radius: 半径
        :param sx: 横向移动速度
        :param sy: 纵向移动速度
        """
        self._center = center
        self._color = color
        self._radius = radius
        self._sx = sx
        self._sy = sy

    @property
    def center(self):
        return self._center

    @property
    def radius(self):
        return self._radius

    @radius.setter
    def radius(self, radius):
        self._radius = radius

    def area(self):
        return pi * self._radius ** 2

    def move(self):
        x, y = self._center[0], self._center[1]
        x += self._sx
        y += self._sy
        self._center = (x, y)
        if x + self._radius > 800:
            x = 790 - self._radius
            self._sx = -self._sx
            self._center = (x, y)
        elif x - self._radius < 0:
            x = 10 + self._radius
            self._sx = -self._sx
            self._center = (x, y)
        elif x + self._radius == 800 or x - self._radius == 0:
            self._sx = -self._sx
        if y + self._radius > 600:
            y = 590 - self._radius
            self._sy = -self._sy
            self._center = (x, y)
        elif y - self._radius < 0:
            y = 10 + self._radius
            self._sy = -self._sy
            self._center = (x, y)
        elif y + self._radius == 600 or y - self._radius == 0:
            self._sy = -self._sy

    def draw(self, screen):
        pygame.draw.circle(screen, self._color, self._center,
                           self._radius, 0)

    def eat(self, other):
        for ball in other:
            if (self._radius > ball.radius) and (distance(self._center, ball.center)
                                                 < (self._radius + ball.radius)):
                temp_area = self.area() + ball.area()
                self._radius = int(sqrt(temp_area / pi))
                # del ball
                other.remove(ball)
            elif self._radius < ball._radius and distance(self._center, ball.center) \
                    < (self._radius + ball.radius):
                temp_area = self.area() + ball.area()
                ball.radius = int(sqrt(temp_area / pi))
                other.remove(self)


def main():
    balls = []
    pygame.init()
    screen = pygame.display.set_mode([800, 600])
    pygame.display.set_caption('大球吃小球')
    clock = pygame.time.Clock()
    running = True

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN and \
                    event.button == 1:
                color = random_color()
                radius = randint(10, 30)
                sx, sy = randint(-10, 10), randint(-10, 10)
                ball = Ball(event.pos, color, radius, sx, sy)
                balls.append(ball)

        refresh(screen, balls)
        clock.tick(50)
        for ball in balls:
            ball.move()
            ball.eat(balls)

    pygame.quit()


def refresh(screen, balls):
    bg_color = (242, 242, 242)
    screen.fill(bg_color)
    for ball in balls:
        ball.draw(screen)
    pygame.display.flip()


def random_color():
    red = randint(0, 255)
    green = randint(0, 255)
    blue = randint(0, 255)
    return red, green, blue


def distance(one, other):
    x1 = one[0]
    y1 = one[1]
    x2 = other[0]
    y2 = other[1]
    return sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)


if __name__ == '__main__':
    main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值