Best Python code snippet using robotframework-pageobjects_python
Ball.py
Source:Ball.py
1import pygame2import random3import Score4from Score import ScoreTracker5from pygame import mixer6mixer.init()7def change_opponent_speed():8 BallBehavior.opponent_speed = random.randint(9, 11)9 return BallBehavior.opponent_speed10def move_ball(go_to):11 if go_to == "go_top_left":12 BallBehavior.ball_x -= 1013 BallBehavior.ball_y -= 1014 if go_to == "go_top_right":15 BallBehavior.ball_x += 1016 BallBehavior.ball_y -= 1017 if go_to == "go_bottom_left":18 BallBehavior.ball_x -= 1019 BallBehavior.ball_y += 1020 if go_to == "go_bottom_right":21 BallBehavior.ball_x += 1022 BallBehavior.ball_y += 1023class BallBehavior:24 score = Score25 opponent_speed = random.randint(9, 11)26 WHITE = (255, 255, 255)27 ball_x = None28 ball_y = None29 # go_to top/bottom right/left30 go_to = ""31 rand_pos = None32 pong_paddle = mixer.Sound('PongPaddleBounce.wav')33 pong_wall_bounce = mixer.Sound('PongWallBounce.wav')34 score_sound = mixer.Sound('PongScored.wav')35 game_over_sound = mixer.Sound('PongGameOver.wav')36 def __init__(self, screen):37 self.circle = None38 self.screen = screen39 BallBehavior.ball_x = self.screen.get_width() / 240 BallBehavior.ball_y = self.screen.get_height() / 241 self.radius = 1042 self.velocity = 1743 self.screen_top = self.screen.get_height() / 10 + 1044 self.screen_bottom = self.screen.get_height() - 1245 self.new_round = True46 self.player_rect = None47 self.opponent_rect = None48 def draw_ball(self, player_rect, opponent_rect):49 score_tracker = ScoreTracker(self.screen)50 score_tracker.draw_to_screen()51 self.player_rect = player_rect52 self.opponent_rect = opponent_rect53 ball_rect = pygame.draw.circle(self.screen, BallBehavior.WHITE, (BallBehavior.ball_x, BallBehavior.ball_y),54 self.radius)55 # call random initial path56 if self.new_round:57 BallBehavior.start_ball(self)58 # check next ball path59 if ball_rect.colliderect(60 opponent_rect) and BallBehavior.go_to == "go_top_right" or BallBehavior.ball_y > self.screen_bottom and BallBehavior.go_to == "go_bottom_left":61 BallBehavior.go_to = "go_top_left"62 if ball_rect.colliderect(63 opponent_rect) and BallBehavior.go_to == "go_bottom_right" or BallBehavior.ball_y < self.screen_top and BallBehavior.go_to == "go_top_left":64 BallBehavior.go_to = "go_bottom_left"65 if ball_rect.colliderect(66 player_rect) and BallBehavior.go_to == "go_top_left" or BallBehavior.ball_y > self.screen_bottom and BallBehavior.go_to == "go_bottom_right":67 BallBehavior.go_to = "go_top_right"68 if ball_rect.colliderect(69 player_rect) and BallBehavior.go_to == "go_bottom_left" or BallBehavior.ball_y < self.screen_top and BallBehavior.go_to == "go_top_right":70 BallBehavior.go_to = "go_bottom_right"71 # changes opponent speed72 if ball_rect.colliderect(player_rect) or ball_rect.x >= self.screen.get_width() -1 or ball_rect.x <= 1:73 change_opponent_speed()74 # call movement function75 if BallBehavior.go_to == "go_top_left":76 move_ball("go_top_left")77 if BallBehavior.go_to == "go_top_right":78 move_ball("go_top_right")79 if BallBehavior.go_to == "go_bottom_left":80 move_ball("go_bottom_left")81 if BallBehavior.go_to == "go_bottom_right":82 move_ball("go_bottom_right")83 # play sounds84 if ball_rect.colliderect(player_rect) or ball_rect.colliderect(opponent_rect):85 BallBehavior.pong_paddle.play()86 if BallBehavior.ball_y < self.screen_top or BallBehavior.ball_y > self.screen_bottom:87 BallBehavior.pong_wall_bounce.play()88 BallBehavior.restart(self)89 # give ball's initial trajectory90 def start_ball(self):91 self.new_round = False92 rand_pos = random.randint(1, 4)93 if rand_pos == 1:94 BallBehavior.go_to = "go_top_left"95 if rand_pos == 2:96 BallBehavior.go_to = "go_top_right"97 if rand_pos == 3:98 BallBehavior.go_to = "go_bottom_left"99 if rand_pos == 4:100 BallBehavior.go_to = "go_bottom_right"101 def restart(self):102 # opponent scored103 if BallBehavior.ball_x < 0:104 BallBehavior.score.update_opponent_score()105 if BallBehavior.score.ScoreTracker.player_score < 15:106 BallBehavior.score_sound.play()107 self.new_round = True108 # player scored109 if BallBehavior.ball_x > self.screen.get_width():110 BallBehavior.score.update_player_score()111 if BallBehavior.score.ScoreTracker.opponent_score < 15:112 BallBehavior.score_sound.play()113 self.new_round = True114 if self.new_round:...
海绵宝宝.py
Source:海绵宝宝.py
1from turtle import *2def go_to(x, y):3 penup()4 goto(x, y)5 pendown()6def help_do():7 go_to(-400, 0)8 forward(800)9 go_to(-400, 100)10 forward(800)11 go_to(-400,200)12 forward(800)13 go_to(-400, -100)14 forward(800)15 go_to(-400, -200)16 forward(800)17 left(90)18 go_to(0,-300)19 forward(600)20 go_to(100, -300)21 forward(600)22 go_to(-100, -300)23 forward(600)24 go_to(-200, -300)25 forward(600)26 go_to(200, -300)27 forward(600)28def head():29 go_to(-200, 180)30 fillcolor('yellow')31 begin_fill()32 seth(-30)33 for _ in range(6):34 circle(36, 60)35 circle(-36, 60)36 seth(-125)37 for _ in range(5):38 circle(40,60)39 circle(-40,60)40 seth(-210)41 for _ in range(4):42 circle(45,60)43 circle(-45,60)44 seth(65)45 for _ in range(5):46 circle(40,60)47 circle(-40,60)48 end_fill()49def eye():50 # ç¼ç½51 go_to(14, -5)52 fillcolor('#f0f0f0')53 begin_fill()54 circle(65, 360)55 end_fill()56 begin_fill()57 go_to(13,12)58 seth(98)59 circle(-65,360)60 end_fill()61 #ç¼ç62 go_to(-10,20)63 fillcolor('blue')64 begin_fill()65 circle(20,360)66 end_fill()67 go_to(-22,20)68 fillcolor('black')69 begin_fill()70 circle(7,360)71 end_fill()72 go_to(40,15)73 fillcolor('blue')74 begin_fill()75 circle(-20, 360)76 end_fill()77 go_to(53,15)78 fillcolor('black')79 begin_fill()80 circle(-7,360)81 end_fill()82 #ç«æ¯83 go_to(-95,65)84 left(20)85 forward(40)86 go_to(-50,87)87 right(25)88 forward(32)89 go_to(0,70)90 right(25)91 forward(40)92 go_to(40, 75)93 left(35)94 forward(40)95 go_to(90, 87)96 right(18)97 forward(30)98 go_to(120, 70)99 right(25)100 forward(40)101def nose():102 fillcolor('yellow')103 go_to(0, -7)104 begin_fill()105 right(50)106 circle(-60, 30)107 color('yellow')108 goto(15,-40)109 end_fill()110 color('black')111 go_to(0, -7)112 seth(-75)113 forward(30)114 go_to(30,-7)115 seth(-105)116 forward(30)117def mouth():118 go_to(-120, - 60)119 seth(-45)120 circle(200, 30)121 seth(0)122 forward(100)123 seth(15)124 circle(200, 30)125def tooth():126 go_to(-30,-114)127 seth(-95)128 fillcolor('white')129 begin_fill()130 forward(30)131 seth(0)132 forward(40)133 seth(95)134 forward(30)135 go_to(-30,-114)136 end_fill()137 go_to(30, -114)138 seth(-95)139 fillcolor('white')140 begin_fill()141 forward(30)142 seth(0)143 forward(40)144 seth(95)145 forward(30)146 go_to(60, -114)147 end_fill()148def hole():149 go_to(-160,160)150 # fillcolor('#ffd700')151 # begin_fill()152 circle(30, 360)153 # a=1154 # for i in range(120):155 # if 0<=i<30 or 60<=i<90:156 # a=a+0.2157 # lt(3)158 # forward(a)159 # else:160 # a=a-0.2161 # lt(3)162 # forward(a)163 # end_fill()164def face():165 eye()166 nose()167 mouth()168 tooth()169 # hole()170def body():171 go_to(-170,-180)172 seth(-120)173 circle(150, 30)174 seth(0)175 forward(40)176 seth(100)177 forward(35)178 seth(-80)179 forward(100)180 fillcolor('brown')181 begin_fill()182 seth(0)183 forward(300)184 seth(80)185 forward(110)186 seth(-100)187 forward(65)188 seth(180)189 forward(315)190 go_to(-118,-400)191 end_fill()192 go_to(-170,-255)193 fillcolor('yellow')194 begin_fill()195 seth(-75)196 forward(80)197 seth(0)198 forward(17)199 seth(105)200 forward(85)201 end_fill()202 go_to(200, -170)203 seth(-60)204 circle(-150,30)205 seth(-180)206 forward(45)207 begin_fill()208 seth(0)209 forward(20)210 seth(-100)211 forward(85)212 seth(180)213 forward(20)214 end_fill()215def tie():216 go_to(-50,-225)217 seth(-40)218 forward(40)219 seth(30)220 forward(52)221 go_to(30,-225)222 seth(-30)223 forward(40)224 seth(40)225 forward(45)226 fillcolor('red')227 go_to(0, -240)228 begin_fill()229 seth(-60)230 forward(10)231 seth(0)232 forward(30)233 seth(60)234 forward(15)235 go_to(30,-225)236 end_fill()237 go_to(4,-250)238 begin_fill()239 seth(-100)240 forward(80)241 seth(0)242 forward(55)243 seth(100)244 forward(80)245 end_fill()246def spongeBob():247 # help_do()248 head()249 face()250 body()251 tie()252if __name__=='__main__':253 screensize(800, 600, 'white')254 pensize(3)255 speed(10)256 go_to(0, 0)257 spongeBob()258 go_to(-100,240)259 write('海绵å®å®',font=('BRUSHSCI.TTF', '20', 'bold'))...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!