''' 如何設計使用隨機變數來賽馬,當兩匹馬平手時算第一批馬獲勝 by Ching-Shoei Chiang ''' import random, pygame, sys from pygame.locals import * pygame.init() FPS = 30 # frames per second setting fpsClock = pygame.time.Clock() # set up the window screen = pygame.display.set_mode((800, 200), 0, 32) pygame.display.set_caption('Animation') WHITE = (255, 255, 255) horse1Img = pygame.image.load('image1.jpg') horse2Img = pygame.image.load('image2.jpg') horse1x = horse1y = 0 horse2x = 0 horse2y = 100 notend = True winner = 0 while notend: # the main game loop for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() screen.fill(WHITE) horse1x = horse1x + random.randint(1,3) horse2x = horse2x + random.randint(1,3) if horse2x > 650: winner = 2 notend = False if horse1x > 650: winner = 1 # 平手算第一個贏 notend = False if winner == 0: screen.blit(horse1Img, (horse1x, horse1y)) screen.blit(horse2Img, (horse2x, horse2y)) elif winner == 1: screen.blit(horse1Img, (horse1x, horse1y)) else: screen.blit(horse2Img, (horse2x, horse2y)) pygame.display.update() fpsClock.tick(FPS)