赞
踩
源代码:
- import pygame
- import random
- import sys
-
- pygame.init()
- W, H = 600, 500
- FPS = 100
- CLOCK = pygame.time.Clock()
- screen = pygame.display.set_mode((W, H))
- pygame.display.set_caption("BallWar")
-
- def draw_bg():
- for i in range(0, H, 25):
- pygame.draw.line(screen, (86, 86, 86, 0.2), (0, i), (W, i), 1)
- for i in range(0, W, 25):
- pygame.draw.line(screen, (86, 86, 86, 0.2), (i, 0), (i, H), 1)
-
- def draw_text(surf, text, size, postion=set(), color=(0, 0, 0), font=None, isAllCenter=False):
- font = pygame.font.SysFont(font, size)
- font_surface = font.render(text, True, color)
- text_rect = font_surface.get_rect()
- text_rect.left = postion[0]
- text_rect.top = postion[1]
- if isAllCenter:
- text_rect.center = postion
- surf.blit(font_surface, text_rect)
-
- def welcome():
- running = True
- while running:
- CLOCK.tick(FPS)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_SPACE:
- running = False
- screen.fill((255, 255, 255))
- draw_bg()
- draw_text(screen, "BallWarGame", 100, (W/2, 60), (10, 10, 10), isAllCenter=True)
- draw_text(screen, "made by HuangYiMing", 30, (W/2+60, 100), (50, 50, 50), isAllCenter=True)
- draw_text(screen, "----Space bar start----", 60, (W/2, 160), (0, 0, 0), isAllCenter=True)
- pygame.display.update()
-
- def main():
- def make_snack(num=1, type='snack', a_=()):
- for i in range(num):
- s = Snack()
- snack_list.add(s)
- if type != 'snack':
- for i in range(num):
- a = AI(a_)
- ai_list.add(a)
- class Player(pygame.sprite.Sprite):
- def __init__(self):
- pygame.sprite.Sprite.__init__(self)
- self.radius = 8
- self.image = pygame.Surface((self.radius, self.radius))
- self.rect = self.image.get_rect()
- self.moveSpeed = 2
- self.rect.center = (W/2, H/2)
-
- def update(self):
- pressed = pygame.key.get_pressed()
- if pressed[pygame.K_UP]:
- if self.rect.centery - self.radius > 0:
- self.rect.centery -= self.moveSpeed
- if pressed[pygame.K_DOWN]:
- if self.rect.centery + self.radius < H:
- self.rect.centery += self.moveSpeed
- if pressed[pygame.K_LEFT]:
- if self.rect.centerx - self.radius > 0:
- self.rect.centerx -= self.moveSpeed
- if pressed[pygame.K_RIGHT]:
- if self.rect.centerx + self.radius < W:
- self.rect.centerx += self.moveSpeed
-
- def draw(self):
- pygame.draw.circle(screen, (200, 0, 0), self.rect.center, self.radius)
- # draw_text(screen, str(self.radius).split(".")[0], int(self.radius)*4-15, self.rect.center, (200, 200, 100), isAllCenter=True)
-
- class Snack(pygame.sprite.Sprite):
- def __init__(self):
- pygame.sprite.Sprite.__init__(self)
- self.radius = random.randint(3, 5)
- self.image = pygame.Surface(((self.radius), (self.radius)))
- self.rect = self.image.get_rect()
- self.rect.center = (random.randint(int(self.radius), W-int(self.radius)), random.randint(int(self.radius), H-int(self.radius)))
- self.colors = (
- (91,165,133),
- (192,138,100),
- (134,71,95),
- (42,52,52)
- )
- self.color = random.choice(self.colors)
-
- def draw(self):
- pygame.draw.circle(screen, self.color, self.rect.center, (self.radius))
-
- class AI(pygame.sprite.Sprite):
- def __init__(self, center=()):
- pygame.sprite.Sprite.__init__(self)
- self.radius = random.randint(8, 18)
- self.image = pygame.Surface((self.radius, self.radius))
- self.rect = self.image.get_rect()
- self.moveSpeed = 2.1
- if center == ():
- self.rect.center = (random.randint(0, W), random.randint(0, H))
- else:
- self.rect.top = center[1]
- self.rect.centerx = center[0]
- self.direction = 0
- self.rand_direction()
- self.step = random.randint(3, 50)
-
- def rand_direction(self):
- self.direction = random.randint(1, 4+1)
- if self.direction == 1:
- self.direction = 'up'
- if self.direction == 2:
- self.direction = 'right'
- if self.direction == 3:
- self.direction = 'down'
- if self.direction == 4:
- self.direction = 'left'
-
- def update(self, onlyDraw=False):
- if onlyDraw is False:
- if self.direction == 'right':
- self.rect.x += self.moveSpeed
- if self.direction == 'left':
- self.rect.x -= self.moveSpeed
- if self.direction == 'up':
- self.rect.y -= self.moveSpeed
- if self.direction == 'down':
- self.rect.y += self.moveSpeed
- self.step -= 1
- if self.step <= 0:
- self.step = random.randint(3, 50)
- self.rand_direction()
- if self.rect.centery + self.radius > H:
- self.direction = 'up'
- if self.rect.centery - self.radius < 0:
- self.direction = 'down'
- if self.rect.centerx - self.radius < 0:
- self.direction = 'right'
- if self.rect.centerx + self.radius > W:
- self.direction = 'left'
-
- self.draw()
-
- def draw(self):
- pygame.draw.circle(screen, (0, 0, 180), self.rect.center, self.radius)
- draw_text(screen, str(self.radius).split(".")[0], int(self.radius)*4-15, self.rect.center, (200, 200, 100), isAllCenter=True)
-
- player = Player()
- snack_list = pygame.sprite.Group()
- ai_list = pygame.sprite.Group()
- running = True
- for i in range(0, 3+1):
- if i % 2 == 0:
- make_snack(3, 'ai')
- continue
- make_snack(5)
- start = pygame.time.get_ticks()
- while running:
- CLOCK.tick(FPS)
- now = pygame.time.get_ticks()
- if now - start >= 1000:
- start = now
- make_snack(random.randint(1, 5))
- if len(ai_list.sprites()) <= 12:
- if random.randint(1, 100) >= 90:
- make_snack(random.randint(0, 2), 'ai', (random.randint(0, W), 0))
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- screen.fill((255, 255, 255))
- draw_bg()
- for sprite in snack_list.sprites():
- sprite.draw()
-
- player.update()
- player.draw()
- ai_list.update()
-
- hit = pygame.sprite.groupcollide(ai_list, snack_list, False, True, pygame.sprite.collide_circle)
- for i in hit:
- i.radius += 0.7
-
- hit = pygame.sprite.spritecollide(player, snack_list, [0, 1], pygame.sprite.collide_circle)
- if hit:
- player.radius += 0.3
-
- hit = pygame.sprite.spritecollide(player, ai_list, False, pygame.sprite.collide_circle)
- for i in hit:
- if i.radius >= player.radius:
- i.radius += player.radius//5
- player.kill()
- running = False
- if player.radius > i.radius:
- player.radius += i.radius//10
- i.kill()
-
- draw_text(screen, 'Size:'+str(int(player.radius)), 30, (W/2-200, 0), (200, 100, 80))
-
- pygame.display.update()
- return {"score": str(int(player.radius)), "player": player, "ai": ai_list}
-
- def over():
- running = True
- rect = pygame.Rect(W/2-250, 10, 500, 180)
- while running:
- CLOCK.tick(FPS)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_b:
- running = False
- screen.fill((255, 255, 255))
- draw_bg()
- datas['player'].draw()
- datas['ai'].update(onlyDraw=True)
- pygame.draw.rect(screen, (0, 0, 0), rect)
- draw_text(screen, 'Your size is '+datas['score'], 50, (W/2, 60), (200, 200, 0), isAllCenter=True)
- draw_text(screen, '-----B key returns-----', 40, (W/2, 100), (100, 100, 100), isAllCenter=True)
- pygame.display.update()
-
- while True:
- welcome()
- datas = main()
- over()
做的有些简陋,不需要素材,直接就可以运行
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。