您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)怎么在python中使用pygame實(shí)現(xiàn)一個(gè)球球大作戰(zhàn)游戲,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
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()
看完上述內(nèi)容,你們對(duì)怎么在python中使用pygame實(shí)現(xiàn)一個(gè)球球大作戰(zhàn)游戲有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。