def run(self): self.start() self.sock.send(PROT_START) data = self.recv_buf() if not self.verify_hello(data): return self.sym_key = self.security.key_exchange_client(self.sock) file_name = self.recv() self.permmisions.Access_Denied(file_name) self.ExplorerManager.begin(file_name) pipe=Pipe() message="" while message!="You failed to login 3 times. Access to folder denied." or message != "login successful": loggedindetails=pipe.communication() print message self.send(loggedindetails) message = self.recv() pipe.pipesend(message) if "login failed" in message: self.Folder_Encrypt.encrypt_file(os.urandom(32), file_name) print raw_input() self.permmisions.remove_ace(file_name)
def run(self): self.start() self.sock.send(PROT_START) data = self.recv_buf() if not self.verify_hello(data): return self.sym_key = self.security.key_exchange_client(self.sock) print self.sym_key pipe=Pipe() loggedindetails=pipe.communication() print loggedindetails self.send(loggedindetails) pipe.pipesend(self.recv())
def __init__(self): self.security = Security() self.sym_key =None self.sock = socket.socket() self.AES= AESCrypt() self.ExplorerManager = ExplorerManager() self.permmisions = permmisions() self.Folder_Encrypt = Folder_Encrypt() self.Pipe = Pipe()
class NetWorkClient: def __init__(self): self.security = Security() self.sym_key =None self.sock = socket.socket() self.AES= AESCrypt() self.ExplorerManager = ExplorerManager() self.permmisions = permmisions() self.Folder_Encrypt = Folder_Encrypt() self.Pipe = Pipe() def start(self): self.sock.connect((SERVER_ADDRESS, SERVER_PORT)) def recv_buf(self): #content="" #while True: # data = self.clientSock.recv(LEN_UNIT_BUF) # if not data: break # content += data #print content #return content.split(END_LINE)[0] return self.sock.recv(LEN_UNIT_BUF).split(END_LINE)[0] def verify_hello(self, data): if len(data): # Verify Hello at beginning of communication if not (data == PROT_START ): self.sock.send(ERROR_SOCKET + END_LINE + "Error in protocol establishment ( 'Hello' )" + END_LINE) time.sleep(0.5) self.sock.close() return False return True return False def send(self, data): data = self.AES.encryptAES(self.sym_key, data) self.sock.send(data) def recv(self): encrypted_data = self.sock.recv(LEN_UNIT_BUF) data = self.AES.decryptAES(self.sym_key, encrypted_data) return data def run(self): self.start() self.sock.send(PROT_START) data = self.recv_buf() if not self.verify_hello(data): return self.sym_key = self.security.key_exchange_client(self.sock) file_name = self.recv() self.permmisions.Access_Denied(file_name) print self.ExplorerManager.begin(file_name) if self.ExplorerManager.begin(file_name)== "folder open": #subprocess.check_call(r"C:\Users\User\Desktop\Project-master\Code\GUI\WindowsFormsApplication5\bin\Debug\WindowsFormsApplication5.exe") self.Pipe.pipesend("folder open") message = "" count=0 while count<3: loggedindetails=self.Pipe.communication() print message self.send(loggedindetails) message = self.recv() self.Pipe.pipesend(message) count+=1 if "login failed" in message: self.Folder_Encrypt.encrypt_file(os.urandom(32), file_name) if message == "login successful": self.permmisions.remove_ace(file_name)
def main(genomes, config): global GEN GEN += 1 nets = [] ge = [] birds = [] for _, g in genomes: net = neat.nn.FeedForwardNetwork.create(g, config) nets.append(net) birds.append(Bird(230, 350)) g.fitness = 0 ge.append(g) base = Base(BASE_LVL) pipes = [Pipe(WIN_WIDTH)] win = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT)) clock = pygame.time.Clock() score = 0 run = True while run: clock.tick(45) for event in pygame.event.get(): if event.type == pygame.QUIT: run == False pygame.quit() quit() # if event.type == pygame.KEYDOWN: # if event.key == pygame.K_SPACE: # bird.jump() pipe_ind = 0 if len(birds) > 0: if len(pipes) > 1 and birds[ 0].x > pipes[0].x + pipes[0].PIPE_TOP.get_width(): pipe_ind = 1 else: run = False break for x, bird in enumerate(birds): bird.move() ge[x].fitness += 0.1 output = nets[x].activate( (bird.y, abs(bird.y - pipes[pipe_ind].height), abs(bird.y - pipes[pipe_ind].bottom))) if output[0] > 0.5: bird.jump() rem = [] add_pipe = False for pipe in pipes: for x, bird in enumerate(birds): if pipe.collide(bird): ge[x].fitness -= 1 birds.pop(x) nets.pop(x) ge.pop(x) if not pipe.passed and pipe.x < bird.x: pipe.passed = True add_pipe = True if pipe.x + pipe.PIPE_TOP.get_width() < 0: rem.append(pipe) pipe.move() if add_pipe: score += 1 for g in ge: g.fitness += 5 pipes.append(Pipe(WIN_WIDTH)) for r in rem: pipes.remove(r) for x, bird in enumerate(birds): if bird.y + bird.img.get_height() >= BASE_LVL or bird.y < 0: birds.pop(x) nets.pop(x) ge.pop(x) base.move() draw_window(win, birds, pipes, base, score, GEN)
def main(): # 初始化 pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32) pygame.display.set_caption('FlappyBird-公众号: Charles的皮卡丘') # 载入图片 background_img = pygame.image.load("./resources/images/background.png") gameover_img = pygame.image.load("./resources/images/gameover.png") # 载入音乐 jump_sound = pygame.mixer.Sound("./resources/audios/jump.wav") jump_sound.set_volume(6) pygame.mixer.music.load('./resources/audios/moonlight.wav') pygame.mixer.music.play(-1, 0.0) pygame.mixer.music.set_volume(12) # 载入字体 font = pygame.font.Font("./resources/fonts/simkai.ttf", 24) # 时钟 clock = pygame.time.Clock() # 小鸟 bird = Bird.Bird(HEIGHT, WIDTH) # 管道 pipes = [] # 时间 time0 = 0 time_interval = 2 # 分数 SCORE = 0 running = True # 主循环 while running: # 画背景 screen.fill(0) for x in range(WIDTH // background_img.get_width() + 1): for y in range(HEIGHT // background_img.get_height() + 1): screen.blit(background_img, (x * 100, y * 100)) time_passed = clock.tick() / 1000 # 事件捕获 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit(0) if event.type == pygame.KEYDOWN: if event.key == K_SPACE: jump_sound.play() bird.cur_jump_height = 0 bird.is_jump = True # 画鸟 bird.update(time_passed) screen.blit(bird.rotated_bird, bird.rect) if bird.is_dead(): running = False # 画管道 time1 = pygame.time.get_ticks() / 1000 if time1 - time0 > time_interval: time0 = time1 pipes.append(Pipe.Pipe(HEIGHT, WIDTH)) for i, pipe in enumerate(pipes): pipe.update(time_passed) for p in pipe.pipe: screen.blit(p.img, p.rect) if bird.rect.left > pipe.x + Pipe.pipeHead().width and not pipe.add_score: SCORE += 1 pipe.add_score = True if pipe.x + Pipe.pipeHead().width < 0: pipes.pop(i) # 碰撞检测 if pygame.sprite.spritecollide(bird, pipe.pipe, False, None): if bird.rect.left < pipe.x + (Pipe.pipeHead().width + Pipe.pipeBody().width) / 2: running = False # 显示分数 scoreText = font.render('Score: ' + str(SCORE), True, (0, 0, 0)) scoreRect = scoreText.get_rect() scoreRect.topleft = [10, 10] screen.blit(scoreText, scoreRect) pygame.display.flip() pygame.display.update() screen.blit(gameover_img, (0, 0)) pygame.display.flip() pygame.display.update() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit(0)
def main(): max_score = 0 loop = 0 global cur_pipe pygame.init() screen = pygame.display.set_mode((screen_width, screen_heigh)) pygame.display.set_caption('Flappy Bird') scene = Scene.Scene(pygame) bird = Bird.Bird(int(screen_width * 0.2), int(screen_heigh * 0.4), scene.birdImgs) pygame.display.set_icon(bird.getIcon()) screen.blit(scene.background, (0, 0)) base = Base.Base(scene.base, 0, screen_heigh - 112, scene.base.get_width() - scene.background.get_width()) pipeList = [] pipePosX = 0 # Q Learing Code -- Begin m_state = {'vertical_distance': 0, 'horizontal_distance': 0} m_state_dash = {'vertical_distance': 0, 'horizontal_distance': 0} action_to_perform = 'do_nothing' explore = 0.0 resolution = 4 alpha_QL = 0.7 vertical_distance_range = [-350, 190] horizontal_distance_range = [0, 180] Q = [] for vert_dist in range( int((vertical_distance_range[1] - vertical_distance_range[0]) / resolution)): tmp = [] for hori_dist in range( int((horizontal_distance_range[1] - horizontal_distance_range[0]) / resolution)): tmp.append({'click': 0, 'do_nothing': 0}) Q.append(tmp) vaild = False reward = 0 # Q Learing Code -- End for i in range(100): if i == 0: pipePosX = screen_width - 250 pipePosX += screen_width - 80 pipe = Pipe.Pipe(scene.pipeImg, pipePosX, random.randint(0, 120), screen_heigh) pipeList.append(pipe) score = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() if event.type == pygame.KEYDOWN and (event.key == pygame.K_SPACE): scene.Sounds['wing'].play() # 按下空格,小鸟处于“振翅”状态,即需要往上飞 bird.birdFlapped = True # 小鸟最初的速度最大=10,逐步递减 bird.bird_vel_y = 10 if bird.bird_state == bird.BIRD_BEGIN: vaild = False score = 0 pipePosX = 0 pipeList.clear() for i in range(2): if i == 0: pipePosX = screen_width - 250 pipePosX += screen_width - 50 pipe = Pipe.Pipe(scene.pipeImg, pipePosX, random.randint(0, 120), screen_heigh) pipeList.append(pipe) bird.bird_state = bird.BIRD_RUNNING elif bird.bird_state == bird.BIRD_RUNNING: # Q Learing Code -- Begin vaild = True reward = 1 # Q Learing Code -- End screen.blit(scene.background, (0, 0)) # “振翅”状态,且速度没到最大速度 if bird.birdFlapped and bird.bird_vel_y > bird.BIRD_MIN_VEL_Y: bird.bird_vel_y -= bird.BIRD_ACC_Y bird.posY -= bird.bird_vel_y # 往上飞到达最大速度,此时应该下降,即脱离“振翅”状态 if bird.birdFlapped and bird.bird_vel_y <= bird.BIRD_MIN_VEL_Y: bird.bird_vel_y = 1 bird.birdFlapped = False # 处于非振翅状态时,小鸟下落 if not bird.birdFlapped: bird.bird_vel_y += bird.BIRD_ACC_Y bird.posY += bird.bird_vel_y screen.blit(bird.getBirdImg(), (bird.posX, bird.posY)) # 显示柱子 for p in pipeList: p.posX -= REMOVE_SPEED if pipeList[0].posX + pipeList[0].pipeImg[0].get_width() < 0: newPipe = Pipe.Pipe( scene.pipeImg, pipeList[len(pipeList) - 1].posX + screen_width - 80, random.randint(0, 120), screen_heigh) pipeList.append(newPipe) pipeList.remove(p) screen.blit(p.pipeImg[0], (p.posX, p.posY[0])) screen.blit(p.pipeImg[1], (p.posX, p.posY[1])) # 在X方向移动地面图片 base.pos_x = -((-base.pos_x + REMOVE_SPEED) % base.shift) screen.blit(scene.base, (base.pos_x, base.pos_y)) rect_bird = pygame.Rect(bird.posX, bird.posY, bird.birdImgWidth, bird.birdImgHeight) # pygame.draw.rect(screen, (255, 0, 0), rect_bird, 1) rect_base = pygame.Rect(base.pos_x, base.pos_y, base.width, base.height) # 碰撞检测1:检查是否与地面相碰 if rect_bird.colliderect(rect_base): bird.bird_state = bird.BIRD_DIE scene.Sounds['hit'].play() # 碰撞检测2:检测是否与当前屏幕显示的柱子碰撞 cur_pipe = pipeList[0] for p in pipeList: if p.posX < screen_width and p.isOver == False: cur_pipe = p rect_pipe_up = pygame.Rect(cur_pipe.posX, cur_pipe.posY[0], cur_pipe.imgWidth, cur_pipe.imgHeight) rect_pipe_down = pygame.Rect(cur_pipe.posX, cur_pipe.posY[1], cur_pipe.imgWidth, cur_pipe.imgHeight) # pygame.draw.rect(screen, (255, 0, 0), rect_pipe_up, 1) # pygame.draw.rect(screen, (255, 0, 0), rect_pipe_down, 1) if rect_bird.colliderect(rect_pipe_up): bird.bird_state = bird.BIRD_DIE scene.Sounds['hit'].play() break if rect_bird.colliderect(rect_pipe_down): bird.bird_state = bird.BIRD_DIE scene.Sounds['hit'].play() break break # 越过柱子得分+1 if bird.posX > cur_pipe.posX + cur_pipe.imgWidth: scene.Sounds['point'].play() score += 1 cur_pipe.isOver = True # 显示分数 score_digits = [int(x) for x in list(str(score))] total_width = 0 # total width of all numbers to be printed for digit in score_digits: total_width += scene.number[digit].get_width() x_offset = (screen_width - total_width) / 2 for digit in score_digits: screen.blit(scene.number[digit], (x_offset, screen_heigh * 0.1)) x_offset += scene.number[digit].get_width() elif bird.bird_state == bird.BIRD_DIE: # Q Learing Code -- Begin max_score = max(max_score, score) loop = loop + 1 print('第', loop + 1, '轮, ', '最高分:', max_score) vaild = True reward = -1000 # Q Learing Code -- End bird.posY = int(screen_heigh * 0.4) bird.bird_vel_y = 1 bird.bird_state = bird.BIRD_BEGIN # Q Learing Code -- Begin if vaild: horizontal_distance = cur_pipe.posX + cur_pipe.imgWidth - bird.posX vertical_distance = bird.posY - cur_pipe.posY[1] m_state_dash['vertical_distance'] = vertical_distance m_state_dash['horizontal_distance'] = horizontal_distance state_bin_v = max( min( int((vertical_distance_range[1] - vertical_distance_range[0] - 1) / resolution), int((m_state['vertical_distance'] - vertical_distance_range[0]) / resolution)), 0) state_bin_h = max( min( int((horizontal_distance_range[1] - horizontal_distance_range[0] - 1) / resolution), int((m_state['horizontal_distance'] - horizontal_distance_range[0]) / resolution)), 0) state_dash_bin_v = max( min( int((vertical_distance_range[1] - vertical_distance_range[0] - 1) / resolution), int((m_state_dash['vertical_distance'] - vertical_distance_range[0]) / resolution)), 0) state_dash_bin_h = max( min( int((horizontal_distance_range[1] - horizontal_distance_range[0] - 1) / resolution), int((m_state_dash['horizontal_distance'] - horizontal_distance_range[0]) / resolution)), 0) click_v = Q[state_dash_bin_v][state_dash_bin_h]["click"] do_nothing_v = Q[state_dash_bin_v][state_dash_bin_h]["do_nothing"] V_s_dash_a_dash = max(click_v, do_nothing_v) Q_s_a = Q[state_bin_v][state_bin_h][action_to_perform] Q[state_bin_v][state_bin_h][ action_to_perform] = Q_s_a + alpha_QL * ( reward + V_s_dash_a_dash - Q_s_a) m_state = copy.deepcopy(m_state_dash) if random.random() - 0.5 < explore: action_to_perform = 'click' if random.randint( 0, 2) == 0 else 'do_nothing' else: state_bin_v = max( min( int((vertical_distance_range[1] - vertical_distance_range[0] - 1) / resolution), int((m_state['vertical_distance'] - vertical_distance_range[0]) / resolution)), 0) state_bin_h = max( min( int((horizontal_distance_range[1] - horizontal_distance_range[0] - 1) / resolution), int((m_state['horizontal_distance'] - horizontal_distance_range[0]) / resolution)), 0) click_v = Q[state_bin_v][state_bin_h]['click'] do_nothing_v = Q[state_bin_v][state_bin_h]['do_nothing'] action_to_perform = 'click' if click_v > do_nothing_v else 'do_nothing' if action_to_perform == "click": scene.Sounds['wing'].play() # 按下空格,小鸟处于“振翅”状态,即需要往上飞 bird.birdFlapped = True # 小鸟最初的速度最大=10,逐步递减 bird.bird_vel_y = 10 # Q Learing Code -- End pygame.display.update() pygame.time.Clock().tick(FPS)
def main(): # 初始化 pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32) pygame.display.set_caption('FlappyBird-公众号: Charles的皮卡丘') # 载入图片 background_img = pygame.image.load("./resources/images/background.png") gameover_img = pygame.image.load("./resources/images/gameover.png") # 载入音乐 jump_sound = pygame.mixer.Sound("./resources/audios/jump.wav") jump_sound.set_volume(6) pygame.mixer.music.load('./resources/audios/moonlight.wav') pygame.mixer.music.play(-1, 0.0) pygame.mixer.music.set_volume(12) # 载入字体 font = pygame.font.Font("./resources/fonts/simkai.ttf", 24) # 时钟 clock = pygame.time.Clock() # 小鸟 bird = Bird.Bird(HEIGHT, WIDTH) # 管道 pipes = [] # 时间 time0 = 0 time_interval = 2 # 分数 SCORE = 0 running = True # 主循环 while running: # 画背景 screen.fill(0) for x in range(WIDTH // background_img.get_width() + 1): for y in range(HEIGHT // background_img.get_height() + 1): screen.blit(background_img, (x * 100, y * 100)) time_passed = clock.tick() / 1000 # 事件捕获 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit(0) if event.type == pygame.KEYDOWN: if event.key == K_SPACE: jump_sound.play() bird.cur_jump_height = 0 bird.is_jump = True # 画鸟 bird.update(time_passed) screen.blit(bird.rotated_bird, bird.rect) if bird.is_dead(): running = False # 画管道 time1 = pygame.time.get_ticks() / 1000 if time1 - time0 > time_interval: time0 = time1 pipes.append(Pipe.Pipe(HEIGHT, WIDTH)) for i, pipe in enumerate(pipes): pipe.update(time_passed) for p in pipe.pipe: screen.blit(p.img, p.rect) if bird.rect.left > pipe.x + Pipe.pipeHead( ).width and not pipe.add_score: SCORE += 1 pipe.add_score = True if pipe.x + Pipe.pipeHead().width < 0: pipes.pop(i) # 碰撞检测 if pygame.sprite.spritecollide(bird, pipe.pipe, False, None): if bird.rect.left < pipe.x + (Pipe.pipeHead().width + Pipe.pipeBody().width) / 2: running = False # 显示分数 scoreText = font.render('Score: ' + str(SCORE), True, (0, 0, 0)) scoreRect = scoreText.get_rect() scoreRect.topleft = [10, 10] screen.blit(scoreText, scoreRect) pygame.display.flip() pygame.display.update() screen.blit(gameover_img, (0, 0)) pygame.display.flip() pygame.display.update() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit(0)
import pygame from Settings import * from Bird import * from Pipe import * from random import * pygame.init() sc = pygame.display.set_mode((WIDTH, HEIGHT)) clock = pygame.time.Clock() pipes = [] passed_pipes = [] pipe = Pipe(sc, GREEN, WIDTH, Vx, ALLOWED_HEIGHT, PIPE_WIDTH, randint(ALLOWED_HEIGHT + 150, HEIGHT - GROUND_HEIGHT - 150), False) pipes.append(pipe) bird = Bird(sc, x_0, y_0, 0, YELLOW, RADIUS, DASH_V, pipes, passed_pipes) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() sc.fill(BLUE) bird.movement() bird.draw() for pipe in pipes: if pipe.existing() == False: pipes.remove(pipe) else:
import pygame import Bird import Pipe pygame.init() SCREEN_XSIZE = 300 SCREEN_YSIZE = 400 screen = pygame.display.set_mode((SCREEN_XSIZE, SCREEN_YSIZE)) pygame.display.set_caption('FlapPY') bird_x = 50 # Bird fixed x gravity = 0.0008 # Gravity for the bird score = 0 spawnpoints = [300, 450, 600] # Initial spawn points of the pipes pipes = [] for spawn in spawnpoints: # Adding pipes pipes.append(Pipe.Pipe(spawn, SCREEN_YSIZE)) bird = Bird.Bird(200) # The bird spawns at y = 200 speed = 0.05 # Pipe speed # Loading all the required images birdpng = pygame.image.load('bird.png').convert_alpha() birdpng = pygame.transform.scale(birdpng, (20, 20)) pygame.display.set_icon(birdpng) background = pygame.rect.Rect(0, 0, SCREEN_XSIZE, SCREEN_YSIZE) backpng = pygame.image.load('background.png').convert() backpng = pygame.transform.scale(backpng, (SCREEN_XSIZE, SCREEN_YSIZE)) top_pipe = pygame.image.load('top_pipe.png').convert() top_pipe = pygame.transform.scale(top_pipe, (50, 30)) # Initializing font to render the score on screen pygame.font.init() myfont = pygame.font.SysFont('Arial Bold', 40) done = False
height = 800 color_bg = '#61c3ff' root = Tk() root.geometry(str(width) + 'x' + str(height)) root.resizable(False, False) root.title("My Own FlappyBird Game") canvas = Canvas(root, width=width, height=height, bg=color_bg) canvas.pack() canvas.update() score = 0 score_label = canvas.create_text((500, 30), text=score, fill='red', font=("Courier", 44)) bird = Bird(canvas) pipes = [Pipe(canvas)] # ReDraw the score label def update_score(): global score_label canvas.delete(score_label) global score score_label = canvas.create_text((500, 30), text=score, fill='red', font=("Courier", 44)) canvas.lift(score_label) # Game loop while True: bird.move() # Add one more pipe