How to use element_size method in AutoItDriverServer

Best Python code snippet using AutoItDriverServer_python

cvp7x8NEW.py

Source:cvp7x8NEW.py Github

copy

Full Screen

1import random, copy, pygame2from pygame.locals import *3level = 2 4human = 15computer = 26element_size = 50 # size of each element of board 7clock_speed = 30 # fps for board updation8screen_width = 640 # width of game window9screen_height = 580 # height of game window10board_width = 8 # number of columns11board_height = 7 # number of rows12x_margin = ((screen_width - board_width * element_size) // 2)13y_margin = ((screen_height - board_height * element_size) // 2)14white = (255, 255, 255)15bgcolor = (150,200,150)16pygame.init()17pygame.display.set_caption('Connect 4')18clock = pygame.time.Clock()19game_display = pygame.display.set_mode((screen_width, screen_height))20redtoken_img = pygame.image.load('4row_red.png')21redtoken_img = pygame.transform.scale(redtoken_img, (element_size , element_size ))22blacktoken_img = pygame.image.load('4row_black.png')23blacktoken_img = pygame.transform.scale(blacktoken_img, (element_size, element_size))24element_img = pygame.image.load('4row_board.png')25element_img = pygame.transform.scale(element_img, (element_size, element_size))26arrow_img = pygame.image.load('4row_arrow.png')27human_img = pygame.image.load('image 1.png')28computer_img = pygame.image.load('image 2.png')29tie_img = pygame.image.load('4row_tie.png')30human_img = pygame.transform.smoothscale(human_img, (340, 80))31computer_img = pygame.transform.smoothscale(computer_img, (340, 80))32tie_img = pygame.transform.smoothscale(tie_img, (100, 80))33redtoken_rect = pygame.Rect(element_size // 2, screen_height - (3 * element_size // 2), element_size, element_size)34blacktoken_rect = pygame.Rect(screen_width - int(3 * element_size / 2), screen_height - int(3 * element_size / 2), element_size, element_size)35win_rect = human_img.get_rect()36win_rect.center = ((screen_width // 2), (screen_height // 10))37help_rect = arrow_img.get_rect()38help_rect.left = redtoken_rect.right + 1039help_rect.centery = redtoken_rect.centery40def main():41 while True:42 game_loop() 43def is_valid(board, col):# checks if move is valid44 if col < 0 or col >= (board_width) or board[col][0] != None:45 return False46 return True47def is_full(board): # checks if board is full48 for x in range(board_width):49 if board[x][0] == None:50 return False51 return True52def make_board():53 board = []54 for x in range(board_width):55 board.append([None] * board_height)56 return board57def check_move(board, player, col):58 lowest = lowest_space(board, col)59 if lowest != -1:60 board[col][lowest] = player61def draw_board_with_two_extra_tokens(board, extra_token=None, extra_token2=None):62 game_display.fill(bgcolor)63 # draw tokens64 token_rect = pygame.Rect(0, 0, element_size, element_size)65 for x in range(board_width):66 for y in range(board_height):67 token_rect.topleft = (x_margin + (x * element_size), y_margin + (y * element_size))68 if board[x][y] == human:69 game_display.blit(redtoken_img, token_rect)70 elif board[x][y] == computer:71 game_display.blit(blacktoken_img, token_rect)72 # draw the extra token73 if extra_token != None:74 if extra_token['color'] == human:75 game_display.blit(redtoken_img, (extra_token['x'], extra_token['y'], element_size, element_size))76 elif extra_token['color'] == computer:77 game_display.blit(blacktoken_img, (extra_token['x'], extra_token['y'], element_size, element_size))78 if extra_token2 != None:79 if extra_token2['color'] == human:80 game_display.blit(redtoken_img, (extra_token2['x'], extra_token2['y'], element_size, element_size))81 elif extra_token2['color'] == computer:82 game_display.blit(blacktoken_img, (extra_token2['x'], extra_token2['y'], element_size, element_size))83 # draw board over the tokens84 for x in range(board_width):85 for y in range(board_height):86 token_rect.topleft = (x_margin + (x * element_size), y_margin + (y * element_size))87 game_display.blit(element_img, token_rect)88 # draw the red and black tokens off to the side89 game_display.blit(redtoken_img, redtoken_rect) # red on the left90 game_display.blit(blacktoken_img, blacktoken_rect) # black on the right91def draw_board(board, extra_token=None):92 game_display.fill(bgcolor)93 # 94 token_rect = pygame.Rect(0, 0, element_size, element_size)95 for x in range(board_width):96 for y in range(board_height):97 token_rect.topleft = (x_margin + (x * element_size), y_margin + (y * element_size))98 if board[x][y] == human:99 game_display.blit(redtoken_img, token_rect)100 elif board[x][y] == computer:101 game_display.blit(blacktoken_img, token_rect)102 # draw the token when in motion103 if extra_token != None:104 if extra_token['turn'] == human:105 game_display.blit(redtoken_img, (extra_token['x'], extra_token['y'], element_size, element_size))106 elif extra_token['turn'] == computer:107 game_display.blit(blacktoken_img, (extra_token['x'], extra_token['y'], element_size, element_size))108 # draw board109 for x in range(board_width):110 for y in range(board_height):111 token_rect.topleft = (x_margin + (x * element_size), y_margin + (y * element_size))112 game_display.blit(element_img, token_rect)113 game_display.blit(redtoken_img, redtoken_rect) # draw red token on left114 game_display.blit(blacktoken_img, blacktoken_rect) # draw black token on right 115def human_move(board, is_first_move):116 not_dragging = True117 pos_x, pos_y = None, None118 lx,ly = 0,0 #PS119 while True:120 for event in pygame.event.get():121 if event.type == QUIT:122 pygame.quit()123 exit()124 elif event.type == MOUSEBUTTONDOWN and not_dragging and redtoken_rect.collidepoint(event.pos):# start dragging125 not_dragging = False126 pos_x, pos_y = event.pos127 elif event.type == MOUSEMOTION and not not_dragging:# update position while dragging128 pos_x, pos_y = event.pos129 lx,ly = animate_probable_position(board, lx, int((pos_x - x_margin) / element_size), color) #PS130 elif event.type == MOUSEBUTTONUP and not not_dragging:# stop dragging131 if pos_y < y_margin and pos_x > x_margin and pos_x < screen_width - x_margin:# drop the token132 col = int((pos_x - x_margin) / element_size)133 if is_valid(board, col):134 move_token(board, col, human)135 board[col][lowest_space(board, col)] = human136 draw_board(board)137 pygame.display.update()138 return139 pos_x, pos_y = None, None140 not_dragging = True141 if pos_x != None and pos_y != None and lx>=x_margin and lx<=x_margin+(board_width*element_size): #PS142 draw_board_with_two_extra_tokens(board, {'x':lx, 'y':ly, 'color':human}, {'x':pos_x - int(element_size / 2), 'y':pos_y - int(element_size / 2), 'color':human}) #PS143 elif pos_x != None and pos_y != None:144 draw_board(board, {'x':pos_x - int(element_size / 2), 'y':pos_y - (element_size // 2), 'turn':human})145 else:146 draw_board(board)147 148 if is_first_move:149 game_display.blit(arrow_img, help_rect)# show help for first move150 pygame.display.update()151 clock.tick()152def animate_probable_position(board, last_x, col, color):153 if col<0 or col>7:154 return 0, 100155 lowestEmptySpace = lowest_space(board, col)156 new_x = (col*element_size)+x_margin157 new_y = y_margin+(lowestEmptySpace*element_size)158 return new_x,new_y159def move_token(board, col, player):160 pos_x = x_margin + col * element_size161 pos_y = y_margin - element_size162 speed = 5163 lowestNoneSpace = lowest_space(board, col)164 while True:165 pos_y += speed166 if ((pos_y - y_margin) / element_size) >= lowestNoneSpace:167 return168 draw_board(board, {'x':pos_x, 'y':pos_y, 'turn':player})169 pygame.display.update()170 clock.tick()171def computer_move(board):172 possible_moves_arr = possible_moves(board, computer, level)173 best_score = -1174 for i in range(board_width):# get the best score based on heuristics175 if possible_moves_arr[i] > best_score and is_valid(board, i):176 best_score = possible_moves_arr[i]177 best_scores_arr = []178 for i in range(len(possible_moves_arr)):179 if possible_moves_arr[i] == best_score and is_valid(board, i):180 best_scores_arr.append(i)# get the first move encountered with this best score 181 break182 return best_scores_arr[0]183def computer_move_animation(board, col):184 pos_x = blacktoken_rect.left185 pos_y = blacktoken_rect.top186 speed = 7187 while pos_y > (y_margin - element_size):# move token up188 pos_y -= speed189 draw_board(board, {'x':pos_x, 'y':pos_y, 'turn':computer})190 pygame.display.update()191 clock.tick()192 pos_y = y_margin - element_size# move token sideways193 speed = 6194 while pos_x > (x_margin + col * element_size):195 pos_x -= speed196 draw_board(board, {'x':pos_x, 'y':pos_y, 'turn':computer})197 pygame.display.update()198 clock.tick()199 # drop the token200 move_token(board, col, computer)201def possible_moves(board, player, depth):202 if depth == 0 or is_full(board):203 return [0] * board_width204 if player == human:205 opponent = computer206 else:207 opponent = human208 possible_moves_arr = [0] * board_width209 for moves in range(board_width):210 copy_board = copy.deepcopy(board)211 if not is_valid(copy_board, moves):212 continue213 check_move(copy_board, player, moves)214 if is_win(copy_board, player):215 possible_moves_arr[moves] = 1.0 # score the winning move216 else: 217 if is_full(copy_board):218 possible_moves_arr[moves] = 0.0219 else:220 for opponent_moves in range(board_width):221 copy_board2 = copy.deepcopy(copy_board)222 if not is_valid(copy_board2, opponent_moves):223 continue224 check_move(copy_board2, opponent, opponent_moves)225 if is_win(copy_board2, opponent):# score the losing move226 possible_moves_arr[moves] = -1.0227 else:228 points_arr = possible_moves(copy_board2, player, depth - 1)229 possible_moves_arr[moves] += (sum(points_arr) / board_width) / board_width# calculate the heuristics based on board depth230 return possible_moves_arr231def lowest_space(board, col):# gives the lowest empty row number of specified column 232 for y in range(board_height-1, -1, -1):233 if board[col][y] == None:234 return y235 return -1236def game_loop():237 238 is_help = True239 if random.randint(0, 1) == 1:# choose who plays the first move240 turn = computer241 else:242 turn = human243 board = make_board()# make an empty board244 while True: 245 if turn == human:246 human_move(board, is_help)247 if is_help:248 # no help needed after first move has been played249 is_help = False250 if is_win(board, human):251 win_img = human_img252 break253 turn = computer 254 else:255 col = computer_move(board)256 computer_move_animation(board, col)257 check_move(board, computer, col)258 if is_win(board, computer):259 win_img = computer_img260 break261 turn = human 262 if is_full(board):# tie situation263 264 win_img = tie_img265 break266 while True:267 draw_board(board)# display the board till user clicks on screen or exits268 game_display.blit(win_img, win_rect)269 pygame.display.update()270 clock.tick()271 for event in pygame.event.get(): 272 if event.type == QUIT:273 pygame.quit()274 exit()275 elif event.type == MOUSEBUTTONUP:276 return277 is_help = False278def is_win(board, player):# check if any current board situation leads to winning for any player279 for x in range(board_width - 3):280 for y in range(board_height):281 if board[x][y] == board[x+1][y] == board[x+2][y] == board[x+3][y] == player:282 return True283 284 for x in range(board_width):285 for y in range(board_height - 3):286 if board[x][y] == board[x][y+1] == board[x][y+2] == board[x][y+3] == player:287 return True288 289 for x in range(board_width - 3):290 for y in range(3, board_height):291 if board[x][y] == board[x+1][y-1] == board[x+2][y-2] == board[x+3][y-3] == player:292 return True293 294 for x in range(board_width - 3):295 for y in range(board_height - 3):296 if board[x][y] == board[x+1][y+1] == board[x+2][y+2] == board[x+3][y+3] == player:297 return True298 return False299if __name__ == '__main__':...

Full Screen

Full Screen

memory.py

Source:memory.py Github

copy

Full Screen

1'''2Writable memory view.3(c) Mit authors 2019-20204The package is distributed under the MIT/X11 License.5THIS PROGRAM IS PROVIDED AS IS, WITH NO WARRANTY. USE IS AT THE USER’S6RISK.7'''8from ctypes import addressof9from .binding import uword_max, word_bytes10class Memory:11 '''12 A writable byte- or word-addressed view of a block of memory.13 - buffer - a value returned by ctypes.create_string_buffer - the memory.14 - element_size - int - the number of bytes read/written at a time.15 - view - the underlying memoryview.16 - start - int - the start address of the memory.17 - end - int - the end address of the memory.18 Memory addresses are specified in bytes. Only addresses that are multiples19 of `element_size` are valid.20 Memory address slices' `start` and `stop` fields must be valid addresses.21 The `step` field must be `None` or `element_size`; either will be treated as22 `element_size`, i.e. only the valid addresses will be accessed.23 `len()` gives the number of elements.24 '''25 def __init__(self, buffer, element_size=1):26 self.buffer = buffer27 assert element_size in (1, word_bytes)28 self.element_size = element_size29 self.view = memoryview(self.buffer).cast('B')30 if element_size == word_bytes:31 self.view = self.view.cast('N')32 self.start = addressof(self.buffer)33 self.end = self.start + len(self) * self.element_size34 def __len__(self):35 return len(self.view)36 def __iter__(self):37 raise NotImplementedError38 def _address_to_index(self, addr):39 if isinstance(addr, slice):40 assert addr.step in (None, self.element_size)41 return slice(42 self._address_to_index(addr.start),43 self._address_to_index(addr.stop),44 )45 else:46 assert addr % self.element_size == 047 index = (addr - self.start) // self.element_size48 if index < 0 or index >= len(self):49 raise IndexError(addr)50 return index51 def __getitem__(self, addr):52 return self.view.__getitem__(self._address_to_index(addr))53 def __setitem__(self, addr, value):54 if not isinstance(value, bytes):55 value = int(value) & uword_max...

Full Screen

Full Screen

figures.py

Source:figures.py Github

copy

Full Screen

1from constans import *2start_x = round(dis_width / 2)3start_y = 04fig_0 = [[start_x, start_y]]5fig_1 = [[start_x, start_y], [start_x - element_size, start_y], [start_x, start_y - element_size],6 [start_x + element_size, start_y - element_size]]7fig_2 = [[start_x, start_y], [start_x + element_size, start_y], [start_x, start_y - element_size],8 [start_x - element_size, start_y - element_size]]9fig_3 = [[start_x, start_y], [start_x - element_size, start_y], [start_x - element_size, start_y - element_size],10 [start_x, start_y - element_size]]11fig_4 = [[start_x, start_y], [start_x + element_size, start_y], [start_x - element_size, start_y],12 [start_x - 2 * element_size, start_y]]13fig_5 = [[start_x, start_y], [start_x + element_size, start_y], [start_x - element_size, start_y],14 [start_x - element_size, start_y - element_size]]15fig_6 = [[start_x, start_y], [start_x + element_size, start_y], [start_x - element_size, start_y],16 [start_x + element_size, start_y - element_size]]17fig_7 = [[start_x, start_y], [start_x + element_size, start_y], [start_x - element_size, start_y],18 [start_x, start_y - element_size]]19list_of_figures = [fig_1, fig_2, fig_3, fig_4, fig_5, fig_6, fig_7]...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run AutoItDriverServer automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful