python pygame 复刻小游戏 躲避方块
来源:互联网 发布:ipad一直无法加入网络 编辑:程序博客网 时间:2024/05/16 10:07
新上手pygame,断断续续学习python也有一短时间了,心血来潮复刻一个小游戏(其实差的不少)。
界面就是这样:
游戏目标也很简单:四色方框来回碰撞移动,控制黑色方框躲避他们,得分是坚持多少的秒数。
贴代码,新手水平,可以打磨的地方还有很多
import sys,pygame,timefrom pygame.locals import *pygame.init()class box(object): """this produce a box which may block the controller""" def __init__(self, x, y, color, hor, ver, box_len): """x and y as the position of box. ver is for vertical, hor is for horizontal. those are the moving speed of box. box_len is the size""" self.x = x self.y = y self.color = color self.ver = ver self.hor = hor self.length = box_len pygame.draw.rect(screen, self.color, (self.x, self.y, self.length, self.length), 0) def move(self): if self.x < 0 or self.x > 500: self.hor = -self.hor if self.y < 0 or self.y > 400: self.ver = -self.ver self.x += self.hor self.y += self.ver pygame.draw.rect(screen, self.color, (self.x, self.y, self.length, self.length), 0) def box_R_xy(self): return((self.x, self.y, self.length))class controller(object): """this is what the mouse control""" #---------------------------------------------------------------------- def __init__(self,ctl_len, color): """Constructor""" self.x = 0 self.y = 0 self.length = ctl_len self.color = color #---------------------------------------------------------------------- def draw_controller(self): MOS_x, MOS_y = pygame.mouse.get_pos() #check the border value if MOS_x - self.length/2 < 0: MOS_x = self.length/2 if MOS_x + self.length/2 > 600: MOS_x = 600 - self.length/2 if MOS_y - self.length/2 < 0: MOS_y = self.length/2 if MOS_y + self.length/2 > 500: MOS_y = 500 - self.length/2 self.x = MOS_x self.y = MOS_y pygame.draw.rect(screen, self.color, (MOS_x-self.length/2, MOS_y-self.length/2, self.length, self.length), 0) def ctl_R_xy(self): """return x and y of controller""" return((self.x, self.y, self.length)) class timer(object): #use time.clock as a time counting tool def __init__(self): self.begin = True self.beginTime = 0 self.stopTime = 0 def start(self): if self.begin: self.beginTime = time.clock() self.begin = False return time.clock() - self.beginTime def stop(self): self.stopTime = self.start() self.beginTime = 0 self.begin = True return self.stopTimedef cld_dtcn(controller, box): Cx1,Cy1,Clen = controller.ctl_R_xy() Bx1,By1,Blen = box.box_R_xy() #check whether the four points of ctl in the box area if(Cx1-Clen/2 in range(Bx1, Bx1+Blen)) and (Cy1 - Clen/2 in range(By1, By1 + Blen)): return True if(Cx1+Clen/2 in range(Bx1, Bx1+Blen)) and (Cy1 - Clen/2 in range(By1, By1 + Blen)): return True if(Cx1-Clen/2 in range(Bx1, Bx1+Blen)) and (Cy1 + Clen/2 in range(By1, By1 + Blen)): return True if(Cx1+Clen/2 in range(Bx1, Bx1+Blen)) and (Cy1 + Clen/2 in range(By1, By1 + Blen)): return True return Falsedef print_text(font, text, pos, color): imgText = font.render(text, True, color) screen.blit(imgText, pos) # main beginscreen = pygame.display.set_mode((600,500))pygame.display.set_caption("Escaping the boxes")# default datawhite =255,255,255black = 0,0,0blue = 0,0,255red = 255,0,0green = 0,255,0yellow = 255,255,0purple = 255,0,255cyan1 = 0,255,255myFont = pygame.font.Font(None, 20)#length of controller lengthctl_len = 40#length of boxesbox_len = 100#initialize boxesb1 = box(0,0, blue, 2,1,box_len)b2 = box(500,0,red, 1,2,box_len)b3 = box(400,0,green, 1,1,box_len)b4 = box(500,400,yellow,3,1,box_len)#initialize controllerctl = controller(ctl_len, black)#about timebestTime = 0currentTime = 0tk = timer()while True: pygame.mouse.set_visible(False) for event in pygame.event.get(): if event.type == QUIT: sys.exit() keys = pygame.key.get_pressed() if keys[K_ESCAPE]: sys.exit() currentTime = tk.start() screen.fill(cyan1) print_text(myFont, "Best Score: " + str(bestTime), (0,0), black) print_text(myFont, "Current Score: " + str(currentTime), (0,40), black) ctl.draw_controller() b1.move() b2.move() b3.move() b4.move() if cld_dtcn(ctl,b1) or cld_dtcn(ctl,b2) or cld_dtcn(ctl,b3) or cld_dtcn(ctl,b4): stopTime = tk.stop() if bestTime < stopTime: bestTime = stopTime pygame.display.update()
因为比较懒,注释写的少,应该都还挺清楚的。
最有技巧的地方在碰撞检测,刚刚上手,还没看到pygame内部有没有碰撞检测的地方。所以就用黑色方块的四个角的坐标与其他的方块对比,如果控制的是圆形的话恐怕要算圆心距离了。暂时我也想不到其他的好办法。
timer()对象用来检测时间,time.clock()是一个全局的变量。不好控制,做定时器的话应该还有更好的方法。
时间关系,就先以上这些。
PS: 《python游戏 编程入门》这本书的错误不少,建议大家不要买这本书。
https://book.douban.com/subject/26311697/
0 0
- python pygame 复刻小游戏 躲避方块
- Pygame开发Python小游戏
- Python pygame 接球的小游戏
- Python:用Pygame实现飞机大战小游戏
- 【VC++】躲避小游戏
- HTML5躲避小游戏
- Pygame 打方块
- Android Unity3D游戏开发之躲避方块
- 消方块小游戏
- pygame模块中的小游戏。
- pygame实现2048小游戏
- Pygame:编写一个小游戏
- HTML5小游戏 -- 格罗斯方块
- j2me写的方块小游戏
- Java多线程-小游戏方块赛跑
- 开始 pygame 小游戏制作 (1)
- 基于Pygame的Python小游戏,《这是一场滑稽与阴险的较量》
- Python pygame 简单的pygame
- 线程之ThreadLocal
- Android自定义捕获Application全局异常
- untiy 简单的shader应用
- 嵌入式学习--step9 IIC学习
- 在 Mac 端获取【网易云音乐】付费音乐的播放地址(注意下载要坚决支持正版!)
- python pygame 复刻小游戏 躲避方块
- 【杭电oj2033】人见人爱A+B
- android BLE蓝牙
- go-并发
- 函数重载的本质
- jquery如何获取隐藏span的值
- 【杭电oj2032】杨辉三角
- Android应用开发性能优化完全分析
- kingshard初探