Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys, time
- import random
- import pygame
- from pygame.locals import *
- FPS = 60
- class get_color(pygame.sprite.Sprite):
- def __init__(self,on_off):
- super(get_color,self).__init__()
- self.surf = pygame.Surface((25,25))
- if on_off%2 == 0 : self.surf.fill((0,0,0))
- if on_off%2 == 1 : self.surf.fill((255,255,255))
- self.rect = self.surf.get_rect()
- def main():
- a1 = random.randint(0,1)
- a2 = random.randint(0,1)
- a3 = random.randint(0,1)
- a4 = random.randint(0,1)
- a5 = random.randint(0,1)
- a6 = random.randint(0,1)
- a7 = random.randint(0,1)
- a8 = random.randint(0,1)
- a9 = random.randint(0,1)
- print(a1,a2,a3,a4,a5,a6,a7,a8,a9)
- b1 = 0
- b2 = 0
- b3 = 0
- b4 = 0
- b5 = 0
- b6 = 0
- b7 = 0
- b8 = 0
- b9 = 0
- q1_on_off = (a1 + a2 + a4)%2
- q2_on_off = (a1 + a2 + a3 + a5)%2
- q3_on_off = (a2 + a3 + a6)%2
- q4_on_off = (a1 + a4 + a5 + a7)%2
- q5_on_off = (a2 + a4 + a5 + a6 + a8)%2
- q6_on_off = (a3 + a5 + a6 + a9)%2
- q7_on_off = (a4 + a7 + a8)%2
- q8_on_off = (a5 + a7 + a8 + a9)%2
- q9_on_off = (a6 + a8 + a9)%2
- w1_on_off = (b1 + b2 + b4)%2
- w2_on_off = (b1 + b2 + b3 + b5)%2
- w3_on_off = (b2 + b3 + b6)%2
- w4_on_off = (b1 + b4 + b5 + b7)%2
- w5_on_off = (b2 + b4 + b5 + b6 + b8)%2
- w6_on_off = (b3 + b5 + b6 + b9)%2
- w7_on_off = (b4 + b7 + b8)%2
- w8_on_off = (b5 + b7 + b8 + b9)%2
- w9_on_off = (b6 + b8 + b9)%2
- q1 = get_color(q1_on_off)
- q2 = get_color(q2_on_off)
- q3 = get_color(q3_on_off)
- q4 = get_color(q4_on_off)
- q5 = get_color(q5_on_off)
- q6 = get_color(q6_on_off)
- q7 = get_color(q7_on_off)
- q8 = get_color(q8_on_off)
- q9 = get_color(q9_on_off)
- w1 = get_color(w1_on_off)
- w2 = get_color(w2_on_off)
- w3 = get_color(w3_on_off)
- w4 = get_color(w4_on_off)
- w5 = get_color(w5_on_off)
- w6 = get_color(w6_on_off)
- w7 = get_color(w7_on_off)
- w8 = get_color(w8_on_off)
- w9 = get_color(w9_on_off)
- pygame.init()
- running = True
- screen = pygame.display.set_mode((800,600))
- pygame.display.set_caption('Turn on the Light')
- main_clock = pygame.time.Clock()
- screen.fill((255,100,255))
- # 宣告 font 文字物件
- head_font = pygame.font.SysFont(None, 50)
- # 渲染方法會回傳 surface 物件
- text1 = head_font.render('Goal:', True, (0, 0, 0))
- # blit 用來把其他元素渲染到另外一個 surface 上,這邊是 window 視窗
- screen.blit(text1, (25,25))
- # 宣告 font 文字物件
- head_font = pygame.font.SysFont(None, 50)
- # 渲染方法會回傳 surface 物件
- text2 = head_font.render('Your Light:', True, (0, 0, 0))
- # blit 用來把其他元素渲染到另外一個 surface 上,這邊是 window 視窗
- screen.blit(text2, (300,25))
- # 这一行表示:将surf画到屏幕 x:400.y:300的坐标上
- screen.blit(q1.surf,(100,100))
- screen.blit(q2.surf,(125,100))
- screen.blit(q3.surf,(150,100))
- screen.blit(q4.surf,(100,125))
- screen.blit(q5.surf,(125,125))
- screen.blit(q6.surf,(150,125))
- screen.blit(q7.surf,(100,150))
- screen.blit(q8.surf,(125,150))
- screen.blit(q9.surf,(150,150))
- # 这一行表示:将surf画到屏幕 x:400.y:300的坐标上
- screen.blit(w1.surf,(475,100))
- screen.blit(w2.surf,(500,100))
- screen.blit(w3.surf,(525,100))
- screen.blit(w4.surf,(475,125))
- screen.blit(w5.surf,(500,125))
- screen.blit(w6.surf,(525,125))
- screen.blit(w7.surf,(475,150))
- screen.blit(w8.surf,(500,150))
- screen.blit(w9.surf,(525,150))
- # 主循环
- while running:
- for event in pygame.event.get():
- # check for QUIT event: if QUIT, set running to false
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- elif event.type == MOUSEBUTTONDOWN:
- if 475 < pygame.mouse.get_pos()[0] < 500 and 100 < pygame.mouse.get_pos()[1] < 125:
- b1 +=1
- if 500 < pygame.mouse.get_pos()[0] < 525 and 100 < pygame.mouse.get_pos()[1] < 125:
- b2 +=1
- if 525 < pygame.mouse.get_pos()[0] < 550 and 100 < pygame.mouse.get_pos()[1] < 125:
- b3 +=1
- if 475 < pygame.mouse.get_pos()[0] < 500 and 125 < pygame.mouse.get_pos()[1] < 150:
- b4 +=1
- if 500 < pygame.mouse.get_pos()[0] < 525 and 125 < pygame.mouse.get_pos()[1] < 150:
- b5 +=1
- if 525 < pygame.mouse.get_pos()[0] < 550 and 125 < pygame.mouse.get_pos()[1] < 150:
- b6 +=1
- if 475 < pygame.mouse.get_pos()[0] < 500 and 150 < pygame.mouse.get_pos()[1] < 175:
- b7 +=1
- if 500 < pygame.mouse.get_pos()[0] < 525 and 150 < pygame.mouse.get_pos()[1] < 175:
- b8 +=1
- if 525 < pygame.mouse.get_pos()[0] < 550 and 150 < pygame.mouse.get_pos()[1] < 175:
- b9 +=1
- w1_on_off = (b1 + b2 + b4)%2
- w2_on_off = (b1 + b2 + b3 + b5)%2
- w3_on_off = (b2 + b3 + b6)%2
- w4_on_off = (b1 + b4 + b5 + b7)%2
- w5_on_off = (b2 + b4 + b5 + b6 + b8)%2
- w6_on_off = (b3 + b5 + b6 + b9)%2
- w7_on_off = (b4 + b7 + b8)%2
- w8_on_off = (b5 + b7 + b8 + b9)%2
- w9_on_off = (b6 + b8 + b9)%2
- w1 = get_color(w1_on_off)
- w2 = get_color(w2_on_off)
- w3 = get_color(w3_on_off)
- w4 = get_color(w4_on_off)
- w5 = get_color(w5_on_off)
- w6 = get_color(w6_on_off)
- w7 = get_color(w7_on_off)
- w8 = get_color(w8_on_off)
- w9 = get_color(w9_on_off)
- elif event.type == KEYDOWN:
- # if the Esc KEY has been pressed set running to false to exit the main loop
- if event.key == K_ESCAPE:
- running = False
- screen.fill((255,100,255))
- # 宣告 font 文字物件
- head_font = pygame.font.SysFont(None, 50)
- # 渲染方法會回傳 surface 物件
- text1 = head_font.render('Goal:', True, (0, 0, 0))
- # blit 用來把其他元素渲染到另外一個 surface 上,這邊是 window 視窗
- screen.blit(text1, (25,25))
- # 宣告 font 文字物件
- head_font = pygame.font.SysFont(None, 50)
- # 渲染方法會回傳 surface 物件
- text2 = head_font.render('Your Light:', True, (0, 0, 0))
- # blit 用來把其他元素渲染到另外一個 surface 上,這邊是 window 視窗
- screen.blit(text2, (300,25))
- if q1_on_off == w1_on_off and q2_on_off == w2_on_off and q3_on_off == w3_on_off and q4_on_off == w4_on_off and q5_on_off == w5_on_off and q5_on_off == w5_on_off and q6_on_off == w6_on_off and q7_on_off == w7_on_off and q8_on_off == w8_on_off and q9_on_off == w9_on_off:
- # 宣告 font 文字物件
- head_font = pygame.font.SysFont(None, 50)
- # 渲染方法會回傳 surface 物件
- text3 = head_font.render('Congratuation', True, (0, 0, 0))
- # blit 用來把其他元素渲染到另外一個 surface 上,這邊是 window 視窗
- screen.blit(text3, (200,300))
- # 这一行表示:将surf画到屏幕 x:400.y:300的坐标上
- screen.blit(q1.surf,(100,100))
- screen.blit(q2.surf,(125,100))
- screen.blit(q3.surf,(150,100))
- screen.blit(q4.surf,(100,125))
- screen.blit(q5.surf,(125,125))
- screen.blit(q6.surf,(150,125))
- screen.blit(q7.surf,(100,150))
- screen.blit(q8.surf,(125,150))
- screen.blit(q9.surf,(150,150))
- # 这一行表示:将surf画到屏幕 x:400.y:300的坐标上
- screen.blit(w1.surf,(475,100))
- screen.blit(w2.surf,(500,100))
- screen.blit(w3.surf,(525,100))
- screen.blit(w4.surf,(475,125))
- screen.blit(w5.surf,(500,125))
- screen.blit(w6.surf,(525,125))
- screen.blit(w7.surf,(475,150))
- screen.blit(w8.surf,(500,150))
- screen.blit(w9.surf,(525,150))
- # 更新
- pygame.display.flip()
- # 事件队列中的循环
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement