import pygame,random,sys,time,math
from pygame.locals import *#定义窗口变量
WORLDWIDTH =2500 #世界宽度
WORLDHEIGHT =2500  #世界高度
HALFWWIDTH = int(WORLDWIDTH//2)
HALFWHEIGHT = int(WORLDHEIGHT//2)
WIDTH = 500  #窗口宽度
HEIGHT =500  #窗口高度
CENTERWIDTH = int(WIDTH//2)
CENTERHEIGHT = int(HEIGHT//2)
FPS = 30  #帧率
SPLITBASE = 0.5 #分裂基数
pi = math.pi
INITIALWEIGHT = 300  #初始重量#定义颜色变量
LIGHTBLACK = (10,51,71)
LIGHTBLUE = (51,102,205)
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)#定义方向变量
UP = 'up'
DOWN = 'down'
LEFT = 'left'
RIGHT = 'right'#定义球球类
class Ball():def __init__(self,xpos,ypos,weight,color): #定义球的x,y,重量,颜色self.xpos = xposself.ypos = yposself.radius = weightToRadius(weight)self.weight = weightself.speed = weightToSpeed(weight)self.color = colordef move(self,direction):  #小球移动rec = pygame.Rect(-HALFWWIDTH, -HALFWHEIGHT, WORLDWIDTH, WORLDHEIGHT)if direction == UP:if rec.top < self.ypos - self.radius: #限制在上边界以下self.ypos -= int(self.speed//20)elif direction == DOWN:if rec.bottom > self.ypos +self.radius:self.ypos += int(self.speed//20)elif direction == RIGHT:if rec.right > self.xpos + self.radius:self.xpos += int(self.speed//20)elif direction == LEFT:if rec.left < self.xpos - self.radius:self.xpos -= int(self.speed//20)def split(self,direction): #分裂小球函数newweight = math.floor((self.weight // 2) * SPLITBASE)newball = Ball(self.xpos, self.ypos, newweight, self.color)if direction == UP:#分裂流畅动画for i in range(10):newball.ypos -= round(0.2*self.radius)drawBall(newball)pygame.display.update()elif direction == DOWN:for i in range(10):newball.ypos += round(0.2*self.radius)drawBall(newball)pygame.display.update()elif direction == LEFT:for i in range(10):newball.xpos -= round(0.2*self.radius)drawBall(newball)pygame.display.update()elif direction == RIGHT:for i in range(10):newball.xpos += round(0.2*self.radius)drawBall(newball)pygame.display.update()self.setWeight(newweight) #分裂完后设置球的重量selfBalls.append(newball)def setWeight(self,newweight):self.weight = newweightself.speed = weightToSpeed(newweight)self.radius = weightToRadius(newweight)def eatFood(self): #吃食物global foodlistselfworldx = self.xposselfworldy = self.yposfor food in foodlist:distance = math.sqrt((selfworldx-food.xpos)*(selfworldx-food.xpos)+(selfworldy-food.ypos)*(selfworldy-food.ypos))if distance < self.radius:self.setWeight(self.weight+food.weight)foodlist.remove(food)#食物类
class Food():def __init__(self,xpos,ypos,weight,color,radius):self.xpos = xposself.ypos = yposself.weight = weightself.color = colorself.radius = radius#其他球类
class OtherBall():def __init__(self,xpos,ypos,weight,color):self.xpos = xposself.ypos = yposself.weight = weightself.radius = weightToRadius(weight)self.speed = weightToSpeed(weight)self.color = colorself.direction = random.uniform(0,2*pi) #方向角度def eatFood(self):global foodlistfor food in foodlist:distance = math.sqrt((self.xpos-food.xpos)**2+(self.ypos-food.ypos)**2)if distance < self.radius:self.setWeight(self.weight+food.weight)foodlist.remove(food)def setWeight(self,newweight):self.weight = newweightself.speed = weightToSpeed(newweight)self.radius = weightToRadius(newweight)def move(self):#使小球能在方框内移动rec = pygame.Rect(-HALFWWIDTH,-HALFWHEIGHT,WORLDWIDTH,WORLDHEIGHT)if rec.left>self.xpos:self.direction = pi - self.directionself.xpos += self.speed//10 #之前没有这句,小球在碰撞几次墙壁之后就会跳动着出界if rec.right<self.xpos:self.direction = pi - self.directionself.xpos -= self.speed//10if rec.top >self.ypos:self.direction = 2*pi-self.directionself.ypos += self.speed//10if rec.bottom < self.ypos:self.direction = 2*pi-self.directionself.ypos -= self.speed//10self.xpos += math.floor(math.cos(self.direction)*self.speed//40)self.ypos -= math.floor(math.sin(self.direction)*self.speed//40)def main():global FPSCLOCK,DISPLAYSURF,cameray,camerax,selfBalls,otherBalls,foodlist,rec #设置全局变量pygame.init()FPSCLOCK = pygame.time.Clock()DISPLAYSURF = pygame.display.set_mode((WIDTH,HEIGHT))camerax = -CENTERWIDTHcameray = -CENTERHEIGHTdirction = ''#定义小球列表selfBalls = []otherBalls = []foodlist = []for i in range(100): #创建其他小球xpos = random.randint(-HALFWWIDTH, HALFWWIDTH)ypos = random.randint(-HALFWHEIGHT, HALFWHEIGHT)otherb = OtherBall(xpos,ypos,INITIALWEIGHT*1.1,randomColor())otherBalls.append(otherb)for i in range(1000): #初始化创建100个食物createFood(foodlist)ball = Ball(0, 0, INITIALWEIGHT, randomColor()) #建立第一个小球selfBalls.append(ball)fontObj = pygame.font.Font('C:/Windows/Fonts/msyh.ttc',20)  #字体对象,我用的是系统字体winfont = pygame.font.Font('C:/Windows/Fonts/msyh.ttc',36)allweight = 0while True:for event in pygame.event.get():if event.type == QUIT:time.sleep(3)exit()if event.type == KEYUP: #响应键盘if event.key == K_w:dirction = UPelif event.key == K_s:dirction = DOWNelif event.key == K_d:dirction = RIGHTelif event.key == K_a:dirction = LEFTelif event.key == K_j:  #分裂count = len(selfBalls)if count < 16:for i in range(count):if selfBalls[i].weight > 20:selfBalls[i].split(dirction)DISPLAYSURF.fill(LIGHTBLACK) #背景填充rec = pygame.Rect(-(camerax + HALFWHEIGHT), -(cameray + HALFWHEIGHT), WORLDWIDTH, WORLDHEIGHT) #边界矩形drawBorder(rec)text = '重量为:'+str(allweight)+'   敌人还剩:'+str(len(otherBalls))displayText(text,fontObj,WHITE,200,20)if len(foodlist)<500:  #当食物数量小于500时,增加食物createFood(foodlist)drawFoods(foodlist)if len(otherBalls)>0: #当其他球还有的时候进行吃球的操作balleatBall()if len(otherBalls)==0 or allweight>=1000000:  #胜利条件displayText('恭喜你!最终你胖到了'+str(allweight)+'斤',winfont,RED,CENTERWIDTH,CENTERHEIGHT)pygame.display.update()time.sleep(3)pygame.quit()if len(selfBalls)==0:displayText('你被吃了~继续努力吧~',winfont,RED,CENTERWIDTH,CENTERHEIGHT)pygame.display.update()time.sleep(3)pygame.quit()allweight = 0for b in selfBalls:  #得到所有重量和移动所有球allweight += b.weightb.move(dirction)for b in otherBalls: #移动其他的球b.move()b.eatFood()drawBalls(selfBalls)camerax = selfBalls[0].xpos - CENTERWIDTHcameray = selfBalls[0].ypos - CENTERHEIGHTfor ball in selfBalls:ball.eatFood()drawBalls(otherBalls)if len(selfBalls)>=2:unite()pygame.display.update()FPSCLOCK.tick(FPS)def displayText(text,fontObj,textcolor,xpos,ypos):  #显示字函数textsurf = fontObj.render(text, True, textcolor)textRect = textsurf.get_rect()textRect.center = (xpos, ypos)DISPLAYSURF.blit(textsurf, textRect)def balleatBall(): #我方球吃其他球,本游戏不设置其他球互吃global selfBalls,otherBallsfor ball1 in selfBalls:for ball2 in otherBalls:distance = math.sqrt((ball1.xpos - ball2.xpos) ** 2 + (ball1.ypos - ball2.ypos) ** 2)if distance < (ball1.radius + ball2.radius) / 2:if ball1.radius > ball2.radius + 3:ball1.setWeight(ball1.weight + ball2.weight)otherBalls.remove(ball2)elif ball1.radius+3 < ball2.radius :ball2.setWeight(ball1.weight + ball2.weight)selfBalls.remove(ball1)def unite(): #联合两个小球global selfBallsfor ball1 in selfBalls:for ball2 in selfBalls:if ball1!=ball2:distance = math.sqrt((ball1.xpos-ball2.xpos)**2+(ball1.ypos-ball2.ypos)**2)if distance<(ball1.radius+ball2.radius)/2:ball1.setWeight(ball1.weight+ball2.weight)selfBalls.remove(ball2)def createFood(foodlist):xpos = random.randint(-HALFWWIDTH,HALFWWIDTH)ypos = random.randint(-HALFWHEIGHT,HALFWHEIGHT)weight = 10  #每个食物的重量radius = 3  #每个食物的半径newfood = Food(xpos,ypos,weight,randomColor(),radius)foodlist.append(newfood)def drawFoods(foodlist):global camerax,camerayfor food in foodlist:pygame.draw.circle(DISPLAYSURF, food.color, ((food.xpos - camerax), (food.ypos - cameray)), food.radius)def drawBalls(balls): #画所有球global camerax,camerayfor ball in balls:pos = (ball.xpos-camerax,ball.ypos-cameray)radius = ball.radiuscolor = ball.colorpygame.draw.circle(DISPLAYSURF,color,pos,radius)def weightToSpeed(weight):#重量转换为速度if weight < 8000:return math.floor(-0.02*weight+200)elif weight >=8000:return 40  #最低速度为40def weightToRadius(weight):  #将小球的重量转化为半径if weight < 100:return math.floor(0.1*weight + 10)elif weight>=100:return math.floor(2*math.sqrt(weight))def drawBorder(rec): #画边界borderthick = 5pygame.draw.rect(DISPLAYSURF,WHITE,rec,borderthick)recleft = (rec[0]-CENTERWIDTH,rec[1]-CENTERHEIGHT,CENTERWIDTH,WORLDHEIGHT+HEIGHT)recright = (rec[0]+WORLDWIDTH,rec[1]-CENTERHEIGHT,CENTERWIDTH,WORLDHEIGHT+HEIGHT)rectop = (rec[0],rec[1]-CENTERHEIGHT,WORLDWIDTH,CENTERHEIGHT)recbottom = (rec[0],rec[1]+WORLDHEIGHT,WORLDWIDTH,CENTERHEIGHT)pygame.draw.rect(DISPLAYSURF,BLACK,recleft,0)pygame.draw.rect(DISPLAYSURF, BLACK, rectop, 0)pygame.draw.rect(DISPLAYSURF, BLACK, recright, 0)pygame.draw.rect(DISPLAYSURF, BLACK, recbottom, 0)def drawBall(Obj):pygame.draw.circle(DISPLAYSURF,Obj.color,(Obj.xpos,Obj.ypos),Obj.radius)def randomColor(): #随机获取颜色return (random.randint(1,255),random.randint(1,255),random.randint(1,255))if __name__ =="__main__":main()

球球大作战(Python)相关推荐

  1. Python版见缝插针小游戏源代码,球球旋转大作战源程序

    见缝插针游戏是一款非常考验玩家手眼协调能力的休闲益智虐心虐脑小游戏,玩法很简单,但要过关却很有挑战性哟! 主要是将一系列的小球,插入到旋转的摩天轮转盘当中,插入过程中不能碰到旋转的摩天轮上的其他小球, ...

  2. 用Python制作一个简单的球球大作战

    大家好,我是查理.今天教大家制作一个简化版球球大作战 话不不多说,上代码 # -*- coding: utf-8 -*- # @Time : 2018/7/30 16:19 # @Author : G ...

  3. python球球大作战简易版详解

    在玩很多游戏的时候,我们可以发现游戏里面的世界很大,但是整个窗口却最大不过我们屏幕大小,为了观察到整个世界,我们的视角窗口就会随着里面人物的移动不断的移动. 比如说游戏球球大作战,在玩这款游戏的时候我 ...

  4. python-pygame小游戏之球球大作战

    这是我第一次写文章,和大家分享我的python程序.请大家多多点赞,觉得我写的好的话还可以关注一下我.后期我会继续多发一些文章的哦! 今天我要来介绍介绍我自己做的游戏--球球大作战!大家来看看吧! - ...

  5. java实现简单窗体小游戏----球球大作战

    java实现简单窗体小游戏----球球大作战 需求分析 1.分析小球的属性: ​ 坐标.大小.颜色.方向.速度 2.抽象类:Ball ​ 设计类:BallMain-创建窗体 ​ BallJPanel- ...

  6. 全球首届“AI球球大作战:Go-Bigger多智能体决策智能挑战赛”开启

    <球球大作战>是一款风靡全球的休闲电子竞技游戏,以大球吃小球为目标,简单有趣却又斗智斗勇. 你不知道的是,AI世界也拥有了自己的<球球大作战>. 前不久,OpenDILab开源 ...

  7. 球球大作战为什么显示服务器神游,球球大作战不能玩怎么解决_球球大作战不能玩解决方案详细分析_好特教程...

    球球大作战不能玩怎么办?下面就是小编为大家带来的关于球球大作战不能玩的问题分析和就解决方法,希望能够帮助到小伙伴们. 1.网络问题 由于球球大作战是需要进行联网游戏,如果网络信号不好或者使用信号较强的 ...

  8. 《球球大作战》游戏优化之路(下)

    演讲内容 大家好,我叫徐宇峰,负责<球球大作战>的性能优化. <球球大作战>现在拥有五亿多的玩家,为了吸引如此庞大的玩家群体,我们提供给玩家更炫更酷的皮肤,这些美轮美奂的皮肤, ...

  9. 《球球大作战》游戏优化之路(上)

    自从2015年<球球大作战>发布以来,现已拥有五亿多的玩家.如此庞大的玩家群体,对游戏的画面,性能要求是非常高的.在Unite Shanghai 2019大会中,<球球大作战> ...

  10. 球球大作战体验服找不到团战服务器6,球球大作战常见问题汇总 新版本问题解决方法...

    下面波比为大家带来球球大作战新版本的问题汇总,大版本更新以来,球球大作战的玩家们遇到了各种各样的小问题,下面就来看看要如何解决吧. 1.为什么我的账号没有签到功能,如何开启签到功能? 答:开启宝箱功能 ...

最新文章

  1. KDD 2019 | 结合属性随机游走的图递归网络
  2. c# uri 取文件名_asp.net获取当前网址url的各种属性(文件名、参数、域名 等)的代码...
  3. java连接各数据库的语句
  4. php显示网卡信息,netwox显示网络配置信息
  5. mysql insert replace_mysql 操作总结 INSERT和REPLACE
  6. 作业调度框架 Quartz.NET 2.0 StepByStep
  7. java jdbc实验,实验八 Java-JDBC编程
  8. java dispose null_Java Map释放内存置null以及调用clear()的区别
  9. Xfire的aegis绑定方式配置小结
  10. /proc/cpuinfo文件分析(查看CPU信息)
  11. Android自带硬解码解码类型说明MediaCodec使用必看
  12. 现代分类方法在医学诊断中的应用——基于R的实现
  13. DataSnap 2009 系列之三 (生命周期篇)
  14. FR两个相同字符如何提取第二个字符后内容
  15. kettle入门教程
  16. 【机器学习入门】(6) 随机森林算法:原理、实例应用(沉船幸存者预测)附python完整代码和数据集
  17. 科罗拉多大学波尔得分校计算机科学,科罗拉多大学波尔得分校相当于中国什么等级的大学?...
  18. HttpClient请求https类型的网站接口碰到ssl证书不受信任问题处理
  19. windows下Intel核显应用ffmpeg的qsv插件编解码
  20. 单目标跟踪——【数据集基准】RGB数据集OTB / NFS / TrackingNet / LaSOT / GOT-10k / UAV123 / VOT 简介

热门文章

  1. 使用PMML部署机器学习模型
  2. 创建Oracle数据库和表
  3. 微信Mac版 v3.0.0正式版上线!mac电脑上也能在朋友圈点赞和互动!
  4. 程序员最值得听的歌曲TOP10
  5. PPPoE,在安全的边缘徘徊
  6. 传奇版本添加npc修改增加npc方法以及配置参数教程
  7. 惊涛怪浪(double dam-break) -- position based fluids
  8. Keil MDK526 相同变量 突出显示
  9. 初识设计模式之简单工厂模式、工厂方法模式、抽象工厂模式
  10. y410p linux 网卡,关于Y400、Y500、Y410P、Y510P无线网卡连接在Win8下经常受限的解决方法...