|
发表于 2017-12-8 14:40:16
|
显示全部楼层
- import random
- ## 之前用Pygame写的一个双色球选号程序。
- import pygame
- from pygame.locals import *
- BLACK = [0, 0, 0]
- RED = [255, 0, 0]
- GREEN = [0, 255, 0]
- BLUE = [0, 0, 255]
- YELLOW = [255, 255, 0]
- WHITE = [255, 255, 255]
- WIDTH = 800
- HEIGHT = 600
- pygame.init()
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- pygame.display.set_caption('Double Color Ball')
- font1 = pygame.font.SysFont('Arial', 40)
- def printText(text, y, shadow = True):
- if shadow:
- textImg = font1.render(text, True, RED)
- screen.blit(textImg, (WIDTH//2 - textImg.get_width()//2 - 2, y - 2))
- textImg = font1.render(text, True, YELLOW)
- screen.blit(textImg, (WIDTH//2 - textImg.get_width()//2, y))
- class Ball(pygame.sprite.Sprite):
- def __init__(self):
- super().__init__()
- self.image = pygame.Surface(((80+20)*6+80, 80))
- self.rect = self.image.get_rect()
- self.rect.center = WIDTH//2, HEIGHT//2
- self.numbers = []
- self.selected = False
- def getNumbers(self):
- if not self.selected:
- allRedBalls = list(range(1, 34))
- redBalls = []
- for i in range(6):
- number = random.choice(allRedBalls)
- allRedBalls.remove(number)
- redBalls.append(number)
- blueBall = random.randint(1, 16)
- self.numbers = sorted(redBalls)
- self.numbers.append(blueBall)
- def update(self):
- self.image.fill(GREEN)
- self.getNumbers()
- for i in range(7):
- if i < 6:
- pygame.draw.circle(self.image, RED, (i*100+40, 40), 40)
- else:
- pygame.draw.circle(self.image, BLUE, (i*100+40, 40), 40)
- numberImg = font1.render(str(self.numbers[i]), True, YELLOW)
- numberImgRect = numberImg.get_rect()
- numberImgRect.center = (i*100+40, 40)
- self.image.blit(numberImg, numberImgRect)
-
- allGroup = pygame.sprite.Group()
- balls = Ball()
- allGroup.add(balls)
- done = False
- while not done:
- for event in pygame.event.get():
- if event.type == QUIT:
- done = True
- elif event.type == MOUSEBUTTONDOWN and event.button == 1:
- if balls.selected == True:
- balls.selected = False
- else:
- balls.selected = True
- screen.fill(GREEN)
- allGroup.update()
- allGroup.draw(screen)
- printText('DOUBLE COLOR BALL', 100)
- printText('CLICK TO SELECT', 450)
- pygame.display.flip()
- pygame.quit()
复制代码
|
|