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)
pygame.draw.circle(screen, black_color, [320, 320], 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)
pygame.draw.circle(screen, black_color, [320, 320], 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))
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()