Pygame 整活五子棋

很早之前写了一个类似的五子棋,没有做到pygame里面,闲着没事给整过来了,主要就是加了一个鼠标映射坐标。

表情被锤会变脸。

设置的0积分不知道能不能下载

https://download.csdn.net/download/ChillingKangaroo/82109145

代码不多,主要是由几个图片

import pygame as pg
import math
import time
import randomclass Tile(): def __init__(self,grid_size,screen_size,x,y): #一个坐标单位self.x,self.y = x,yself.grid_size = grid_sizeself.rectangle = (self.x*tile_size[0]+50,self.y*tile_size[1]+50,tile_size[0],tile_size[1])self.points = [ [(0.5+self.x)*tile_size[0]+50,self.y*tile_size[1]+50],    #upper middle[(0.5+self.x)*tile_size[0]+50,(self.y+1)*tile_size[1]+50],    #lower middle[(self.x)*tile_size[0]+50,(self.y+0.5)*tile_size[1]+50],    #middle left right[(self.x+1)*tile_size[0]+50,(self.y+0.5)*tile_size[1]+50],    #middle right] self.chess = Nonedef draw(self,color = (255,253,150),line_color = (150,175,255)): #x,y represents the tile coordinates  pg.draw.rect(screen,color,self.rectangle)pg.draw.line(screen,line_color,(self.points[0]),(self.points[1]),3)pg.draw.line(screen,line_color,(self.points[2]),(self.points[3]),3)if self.chess != None:screen.blit(chess[self.chess],((self.x+0.3)*tile_size[0] + 50, (self.y+0.3)*tile_size[1] + 50))pg.display.update()def draw_chessboard():screen.fill((255,255,255))for y in range(grid_size[1]):for x in range(grid_size[0]):matrix[y][x].draw()def get_clicked_tile():x_,y_ = pg.mouse.get_pos() #pixel coordinatesx = int((x_-50)/tile_size[0]) if int((x_-50)) >= 0 else -1y = int((y_-50)/tile_size[1]) if int((y_-50)) >= 0 else -1return x,ydef check_win(x,y):directions = [[(-1,0),(1,0)],[(0,-1),(0,1)],[(-1,1),(1,-1)],[(-1,-1),(1,1)]] #x,yfor line in directions:depth=0 for dx,dy in line:tempx = dxtempy = dyif x+dx in range(grid_size[0]) and y+dy in range(grid_size[1]):while matrix[y+dy][x+dx].chess == matrix[y][x].chess and depth <= 5:depth += 1if depth == 4:return matrix[y][x].chessdx += tempxdy += tempyif x+dx not in range(grid_size[0]) or y+dy not in range(grid_size[1]):breakdef display_score():global turnplayer1_won_text = font.render(f': {player_won[0]}', True, (0,0,0), (255,255,255))player2_won_text = font.render(f': {player_won[1]}', True, (0,0,0), (255,255,255))turn_text = font.render(f'Turns: {turn}                                      ', True, (0,0,0), (255,255,255))screen.blit(player1_won_text,(1000,120))screen.blit(player2_won_text,(1000,220))screen.blit(chess_scoreboard[0],(900,100))screen.blit(chess_scoreboard[1],(900,200))screen.blit(turn_text,(900,300))pg.display.update()def game_over_animation():for x in range(grid_size[0]+1):for y in range(grid_size[1]):if x +1 < grid_size[0]:matrix[y][x+1].chess = 2 if matrix[y][x+1].chess != None else Nonematrix[y][x+1].draw()if x < grid_size[0]:matrix[y][x].chess = 3matrix[y][x].draw()if x > 0:matrix[y][x-1].chess = Nonematrix[y][x-1].draw()time.sleep(0.05)#================================initialize parameter===================================
chess_color = [(0,0,0),(255,255,255)]
screen_size = [1200,800]
chess_size = [800,800]
grid_size = [15,15]
tile_size = [(chess_size[0]-100)/grid_size[0],(chess_size[1]-100)/grid_size[1]]run = True
game_ended = False
player_won = [0,0]
turn = 0
chess = [   pg.transform.smoothscale(pg.image.load('smiley.png'), (int(tile_size[0]/2),int(tile_size[0]/2))),   #player1pg.transform.smoothscale(pg.image.load('angry.png'), (int(tile_size[0]/2),int(tile_size[0]/2))),    #player2pg.transform.smoothscale(pg.image.load('sad.png'), (int(tile_size[0]/2),int(tile_size[0]/2))),    #sad facepg.transform.smoothscale(pg.image.load('fist.png'), (int(tile_size[0]/2),int(tile_size[0]/2)))]     #fist
chess_scoreboard = [   pg.transform.smoothscale(pg.image.load('smiley.png'), (80,80)),pg.transform.smoothscale(pg.image.load('angry.png'), (80,80)),]matrix = []
for y in range(grid_size[1]):temp = []for x in range(grid_size[0]):tile = Tile(grid_size,screen_size,x,y)temp.append(tile)matrix.append(temp)screen = pg.display.set_mode(screen_size)
pg.init()
#================================draw board==============================================
draw_chessboard()
font = pg.font.Font('freesansbold.ttf', 32)
display_score()
#================================game loop==============================================
while run:for event in pg.event.get():if event.type == pg.QUIT:run = Falsepg.quit()if event.type == pg.KEYDOWN:if event.key == pg.K_g:winner = (turn+1)%2print(f'player{(turn%2)+1} gave up')player_won[winner] += 1game_ended = Truedisplay_score()if event.type == pg.MOUSEBUTTONUP:if not game_ended:x,y = get_clicked_tile()if x in range(grid_size[0]) and y in range(grid_size[1]) and matrix[y][x].chess == None:matrix[y][x].chess = turn%2matrix[y][x].draw()winner = check_win(x,y)turn += 1if winner != None:print(f'the winner is: player{winner+1}')player_won[winner] += 1game_ended = Truedisplay_score()else:game_over_animation()game_ended = Falseturn = 0display_score()for y in range(grid_size[1]):for x in range(grid_size[0]):matrix[y][x].chess = Nonematrix[y][x].draw()time.sleep(1/30)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/244196.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

读进程和写进程同步设计_浅谈unix进程进程间通信IPC原理

什么是进程进程间通信进程间通信即为不同进程之间通信&#xff0c;进程同步是进程间通信的一种unix进程间通信的分类有哪些System V进程间通信方式包含&#xff1a;System V消息队列System V信号量System V共享内存UNIX进程间通信方式包含&#xff1a;匿名管道命名管道信号POSI…

(Ipython)Matplotlib 中将二叉树可视化

&#xff08;注意之前代码有错误目前已更新&#xff09; 最近学习黑红二叉树&#xff0c;我想如果把二叉树可视化在操作的时候如果出错会比较容易发现。 在网上搜了一圈只有比较简单的ascii 的代码。 自己用Ipython写了一个&#xff0c;比较适合学生。 PS&#xff1a;算法没…

其中一个页签慢_VBA实战技巧15:创建索引页

学习Excel技术&#xff0c;关注微信公众号&#xff1a;excelperfect在工作簿中有许多工作表时&#xff0c;我们可以创建一个单独的工作表当作索引页&#xff0c;在其中创建到每个工作表的链接&#xff0c;就像目录一样&#xff0c;不仅方便查看工作簿中的工作表名称&#xff0c…

Python使用OpenCV 卷积核 实现康威生命游戏

"Mozart, Beethoven, and Chopin never died. They simply became music." 康威生命游戏规则十分简单&#xff0c;简化后如下&#xff1a; 一个“细胞”&#xff08;或者说单元&#xff09;分为生或死两种状态&#xff0c; 如果活相邻细胞有2或3个活细胞 该细胞活…

verilog赋多位值_verilog赋值

我现在要用且只能用八位的拨片开关对两个四位变量t1l,t1h赋值&#xff0c;且这两个变量t1l,t1h是要输出的&#xff0c;所以我编了一下程序&#xff0c;先通过拨片开关对输入变量d0,d1赋值&#xff0c;然后将d0,d1的值赋给t1l,t1...我现在要用且只能用八位的拨片开关对两个四位变…

Python/OpenCV 使用傅里叶变换与高斯平滑分析轮廓轨迹

该方法基本思想是通过分析高低频信息检测出轮廓碰伤、运动轨迹突变等信息&#xff0c;在工业上应用可能比较广泛&#xff0c; 对各种不规则形状都能分析&#xff0c;不过对高频信息多的复杂形状可能不好区分形状与噪音。 在这个例子中讲使用一个有鼓包的鸡蛋 import numpy as…

实现mvcc_一文读懂 etcd 的 mvcc 实现

提到事务必谈 ACID 特性, 基于悲观锁的实现会有读写冲突问题&#xff0c;性能很低&#xff0c;为了解决这个问题&#xff0c;主流数据库大多采用版本控制 mvcc[1] 技术&#xff0c;比如 oracle, mysql, postgresql 等等。读可以不加锁&#xff0c;只需要读历史版本即可 (写写还…

Pygame 粒子物理:Numba实现同时渲染十万+像素

图中同时渲染了十万个像素&#xff0c;没有明显掉帧 我对Pygame的印象一直是慢的扣脚的&#xff0c;直到前段时间看到了一段MandelBrot代码&#xff08;源地址弄丢了&#xff09;其中使用了这个功能:pygame.surfarray.make_surface() 这里可以直接把numpy阵列转换为pygame.su…

“vector”: 不是“std”的成员_libcxx 的 std::function 源码分析

链接&#xff1a;functional。其中 std::function 的主体内容在 2100 多行。先来看 function 的头部。template<class _Rp, class ..._ArgTypes> class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>: public __function::__maybe_derive_from_unary_function…

python验证身份证号码大全_身份证号码处理技巧大全

身份证号码处理技巧大全&#xff0c;汇总了常用的身份证号码处理六大技巧&#xff1a;不需要复杂的公式&#xff0c;点点鼠标即可完成&#xff0c;简单快捷&#xff0c;下面将详细介绍六大功能的具体用法。(文章最后有工具和演示文件的下载地址&#xff0c;可以下载下来同步操作…

语言print如何实现连续输出_【每日一题】如何实现一个高效的单向链表逆序输出?...

今后&#xff0c;动力节点Java学院将每天为大家带来一道大厂面试真题&#xff0c;这些面试题都是大厂技术专家们结合多年的工作、面试经验总结提炼而成的面试真题。通过这些面试题&#xff0c;还可以间接地了解技术大牛们出题思路与考察要点。建议大家收藏并分享给更多需要的人…

恩尼格玛模拟器_用C语言编的恩格尼码模拟器

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼void enumio(char pie1[],char pie2[],char pie3[],char pier[],char ch0[],char chz[],char ip[],char k[],int cou){ int check(char *a);int excheck(char *a);int compare(char *le,char *unle);int factorial(int n);void cyc…

louvain算法python_复杂网络任务6:Louvain社区发现算法的原理、细节和实现,作业,六,以及...

ΔQ[∑in2∗mki,in2∗m−(∑tot2∗m)2−(2∗∑tot∗ki4∗m2)−(ki2∗m)2]−[∑in2∗m−(∑tot2∗m)2−(ki2∗m)2]ki,in2∗m−2∗∑tot∗ki4∗m212∗m∗(ki,in−∑tot∗kim)\Delta{Q} [\frac{\sum_{in}} {2*m} \frac{k_{i,in}}{2*m} - (\frac{\sum_{tot}}{2*m})^2 - (\frac{2*…

系统相机裁剪比例_从单反到手机,三种黄金比例构图方法,让你的照片与众不同...

古埃及金字塔和达芬奇蒙娜丽莎有什么共同之处&#xff1f;它们都是使用黄金比例进行设计的。不管是建筑设计还是绘画&#xff0c;它们都是属于艺术的一种&#xff0c;所以黄金比例也同样适用于摄影构图中。很多优秀的摄影作品都会使用黄金比例的构图方法进行拍摄&#xff0c;因…

mysql安装图解_MySQL安装图解

目录一、安装详细过程MySQL默认安装在“C:\Program Files”目录下。普通使用只安装MySQL Server就足够了&#xff0c;大小为416M。如果不想装在C盘&#xff0c;也可以安装完成之后再将其移动到其他盘。1.接受许可&#xff0c;点击Next2.选择安装功能&#xff0c;推荐选择Server…

mysql字符集设置_mysql字符集设置

配置文件路径&#xff1a; /full/path/mysql/bin/my.cnf (默认为/etc/my.cnf )[client]default-character-setutf8[mysql]default-character-setutf8[mysqld]init_connectSET collation_connection utf8_unicode_ciinit_connectSET NAMES utf8character-set-serverutf8collati…

mysql 索引 原理_MySQL——索引实现原理

在MySQL中&#xff0c;索引属于存储引擎级别的概念&#xff0c;不同存储引擎对索引的实现方式是不同的&#xff0c;本文主要讨论MyISAM和InnoDB两个存储引擎的索引实现方式。MyISAM索引实现MyISAM引擎使用BTree作为索引结构。MyISAM会按照数据插入的顺序分配行号&#xff0c;从…

mysql 字段 中文_如何配置mysql支持中文字段名与中文字段

匿名用户1级2018-11-18 回答中文字段名都可以了 但是中文记录不行 奇怪啊mysql>; create table a (a char(20));Query OK, 0 rows affected (0.05 sec)mysql>; insert into a values(^_^);Query OK, 1 row affected (0.05 sec)mysql>; insert into a values(中guo);Qu…

mysql中如何删除多个表格_mysql怎么批量删除多个表?

mysql批量删除多个表的方法&#xff1a;使用“DROP TABLE”语句&#xff0c;只要将表名依次写在后面&#xff0c;相互之间用逗号隔开即可&#xff1b;语法格式“DROP TABLE [IF EXISTS] 表名1 [ ,表名2, 表名3 ...]”。mysql批量删除多个表使用 DROP TABLE 语句可以删除一个或多…

mysql 图片 格式_mysql存储图片 用什么格式

{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],"search_count":[{"count_phone":4,"count":4}]},"card":[{"des":"阿里云数据库专家保驾护航&#xff0c;为用户…