十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
今天就跟大家聊聊有关怎么在python中使用pygame实现一个球球大作战游戏,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
为纳溪等地区用户提供了全套网页设计制作服务,及纳溪网站建设行业解决方案。主营业务为成都网站建设、成都做网站、纳溪网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!from random import randint,randrange import pygame from math import sqrt,pi class Ball(object): def __init__(self, center, color, radius, sx, 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 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: # self._sx = -abs(self._sx) # elif x + self._radius < 0: # self._sx = abs(self._sx) # elif y +self._radius > 800: # self._sy = -abs(self._sy) # elif y +self._radius < 0: # self._sy = abs(self._sy) if x + self._radius >= 800 or x - self._radius <= 0 or x <= 0: self._sx = -self._sx if y +self._radius >= 800 or y - self._radius <= 0 or y <= 0: self._sy = -self._sy def draw(self,screen): pygame.draw.circle(screen, self._color, self._center, self._radius, 0) def eat(self, other): a = sqrt((self._center[0] - other.center[0]) ** 2 + (self._center[1] - other.center[1]) ** 2) if a < self._radius + other.radius and self._radius < other.radius: other.radius = self._radius + other.radius self.radius = 0 elif a < self._radius + other.radius and self._radius > other.radius: self._radius = self._radius + other.radius other.radius = 0 def main(): balls = [] pygame.init() screen = pygame.display.set_mode([800,800]) pygame.display.set_caption('大球吃小球') c = 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,100) sx, sy = randint(-10,10), randint(-10,10) ball = Ball(event.pos, color, radius, sx, sy) balls.append(ball) refresh(screen,balls) c.tick(20) # 50帧 for ball in balls: ball.move() balls_len = len(balls) for i in range(balls_len): for x in range(balls_len): balls[i].eat(balls[x]) for ball in balls: if ball.radius == 0: balls.remove(ball) pygame.quit() def refresh(screen,balls): bg_color = [255, 255, 255] screen.fill(bg_color) for ball in balls: ball.draw(screen) pygame.display.flip() def random_color(): return [randint(1,255), randint(1,255), randint(1,255)] if __name__ == '__main__': main()
看完上述内容,你们对怎么在python中使用pygame实现一个球球大作战游戏有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联成都网站设计公司行业资讯频道,感谢大家的支持。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。