python pygame 复刻小游戏 躲避方块

    xiaoxiao2025-06-16  9

    新上手pygame,断断续续学习python也有一短时间了,心血来潮复刻一个小游戏(其实差的不少)。

    界面就是这样:

    游戏目标也很简单:四色方框来回碰撞移动,控制黑色方框躲避他们,得分是坚持多少的秒数。

    贴代码,新手水平,可以打磨的地方还有很多

    import sys,pygame,time from 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.stopTime def 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 False def print_text(font, text, pos, color): imgText = font.render(text, True, color) screen.blit(imgText, pos) # main begin screen = pygame.display.set_mode((600,500)) pygame.display.set_caption("Escaping the boxes") # default data white =255,255,255 black = 0,0,0 blue = 0,0,255 red = 255,0,0 green = 0,255,0 yellow = 255,255,0 purple = 255,0,255 cyan1 = 0,255,255 myFont = pygame.font.Font(None, 20) #length of controller length ctl_len = 40 #length of boxes box_len = 100 #initialize boxes b1 = 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 controller ctl = controller(ctl_len, black) #about time bestTime = 0 currentTime = 0 tk = 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/

    转载请注明原文地址: https://ju.6miu.com/read-1299997.html
    最新回复(0)