How to use direction_changed method in fMBT

Best Python code snippet using fMBT_python

derbiili.py

Source:derbiili.py Github

copy

Full Screen

1from PyQt5.QtGui import QPixmap,QTransform2from PyQt5.QtWidgets import QGraphicsPixmapItem3from PyQt5.QtCore import Qt45from physics import Physics6import Blocks7from CONSTANTS import *8from PyQt5.Qt import QPointF, pyqtSignal, QObject9from animation import Animation10from signals import Signals11from Items.equipment import Equipment12from Particles.particleeffects import ParticleEffects1314class Derbiili(QGraphicsPixmapItem):15 16 def __init__(self, x, y, scene, parent=None):17 QGraphicsPixmapItem.__init__(self,parent)18 self.animation = Animation(self, "Textures/Derbiili",500)19 20 self.collision = False21 self.setPos(x*32,y*32)22 23 #values can be found in "CONSTANTS"24 self.speed = PLAYER_SPEED25 self.jump_height = JUMP_HEIGHT26 self.in_air = False27 self.crouching = False28 self.a = ACCELERATION29 self.friction = FRICTION30 self.jumps = 131 self.jumps_left = 132 33 self.vx = 0.034 self.vy = 0.035 self.direction = 'right'36 37 self.setZValue(2)38 self.scene = scene39 self.physics = Physics()40 self.signals = Signals()41 self.equipment = {}42 self.signals.direction_changed.connect(self.direction_changed_update)43 #self.particleeffects = ParticleEffects(self,self.scene)44 #self.signals.player_moved.connect(self.particleeffects.player_run_init)45 46 def get_direction(self):47 48 return self.direction49 50 def is_collidable(self):51 52 return self.collision53 54 def stand_on_effect(self,player,scene):55 56 return False57 58 def touch_effect(self,player,scene):59 60 pass61 62 def add_equipment(self,equipment,type):63 64 if type == 'Crown':65 self.equipment['Crown'] = equipment66 self.signals.direction_changed.connect(equipment.update)67 self.animation.synchronize_animations(equipment)68 self.add_super_powers()69 70 def add_super_powers(self):71 72 self.jumps = 273 74 def player_movement(self, keys_pressed,keybindings):75 dx = 076 dy = 077 78 self.is_touching()79 if self.scene.is_stopped():80 return 0,081 82 self.is_standing_on(dy)83 if self.scene.is_stopped():84 return 0,085 86 if keybindings['crouch'] in keys_pressed:87 if not self.crouching:88 self.crouching = True89 self.signals.direction_changed.emit()90 self.speed = 291 else:92 if self.crouching:93 self.crouching = False94 self.signals.direction_changed.emit()95 self.speed = PLAYER_SPEED96 97 if self.in_air:98 if self.jumps_left > 0 and keybindings['jump'] in keys_pressed:99 self.jumps_left -= 1100 dy = self.jump(self.jump_height)101 self.signals.direction_changed.emit()102 103 dv = self.physics.gravity()104 dy = self.vy-dv105 106 elif keybindings['jump'] in keys_pressed:107 keys_pressed.remove(keybindings['jump'])108 self.jumps_left -= 1109 dy = self.jump(self.jump_height)110 self.signals.direction_changed.emit()111 112 if keybindings['left'] in keys_pressed:113 if self.direction == 'right':114 self.direction = 'left'115 self.signals.direction_changed.emit()116 117 if self.vx > 0:118 self.vx -= self.friction119 dx += self.vx120 121 elif self.vx > -self.speed:122 self.vx -= self.a123 dx += self.vx124 else:125 dx -= self.speed126 127 if self.x()+dx < 0:128 dx = 0129 130 elif keybindings['right'] in keys_pressed:131 if self.direction == 'left':132 self.direction = 'right'133 self.signals.direction_changed.emit()134 135 if self.vx < 0:136 self.vx += self.friction137 dx += self.vx138 elif self.vx < self.speed:139 self.vx += self.a140 dx += self.vx141 else:142 dx += self.speed143 144 if self.x()+dx+32 > self.scene.getSceneX():145 dx = 0146 else:147 if self.vx == 0.0:148 pass149 elif self.vx > 0:150 if self.vx - self.friction < 0:151 self.vx = 0152 dx = 0153 else:154 self.vx -= self.friction155 dx = self.vx156 else:157 if self.vx + self.friction > 0:158 self.vx = 0159 dx = 0160 else:161 self.vx += self.friction162 dx = self.vx163 164 if dy <= 0:165 166 xdetect = self.physics.check_collisions_x(self,self.scene,dx)167 ydetect = self.physics.check_collisions_y(self,self.scene,dy)168 169 if xdetect is None:170 pass171 172 else:173 dx = xdetect174 self.vx = 0.0 175 176 if ydetect is None:177 pass178 else:179 self.jumps_left = self.jumps180 dy = ydetect181 self.vy = 0.0182 self.in_air = False183 self.physics.reset_gravity()184 self.signals.direction_changed.emit()185 186 elif dy > 0:187 188 xdetect = self.physics.check_collisions_x(self,self.scene,dx)189 ydetect = self.physics.check_collisions_y(self,self.scene,dy)190 191 if xdetect is None:192 pass193 else:194 dx = xdetect195 self.vx = 0.0 196 197 if ydetect is None:198 pass199 else:200 self.signals.direction_changed.emit()201 dy = ydetect202 self.vy = 0.0203 self.physics.reset_gravity()204 205 #positive dy moves player up, negative moves down206 207 self.move(dx,dy)208 209 def is_standing_on(self,dy):210 211 effect = False212 transform = QTransform()213 pos = self.pos()214 posdown1 = pos + QPointF(5.0,32.0)215 posdown2 = pos + QPointF(27.0,32.0)216 217 item1 = self.scene.itemAt(posdown1,transform)218 item2 = self.scene.itemAt(posdown2,transform)219 220 if item1 is None and item2 is None:221 self.in_air = True222 effect = True223 224 elif item1 is not None:225 226 effect = item1.stand_on_effect(self,self.scene)227 228 elif item2 is not None:229 230 effect = item2.stand_on_effect(self,self.scene)231 232 elif item1 is not None or item2 is not None:233 pass234 235 if not effect:236 self.reset_friction()237 238 def is_touching(self):239 240 items = self.scene.collidingItems(self)241 242 for item in items:243 item.touch_effect(self,self.scene)244 245 def jump(self,jump_height):246 247 self.vy = jump_height248 dy = self.vy249 self.in_air = True250 self.physics.set_jump_correction(jump_height)251 self.physics.reset_gravity()252 253 return dy254 255 def set_friction(self,df):256 257 self.friction = df258 self.a = df*2259 260 def reset_friction(self):261 262 self.friction = FRICTION263 self.a = ACCELERATION264 265 def move(self,dx,dy):266 267 self.setPos(self.x()+dx, self.y()-dy)268 269 def direction_changed_update(self):270 271 if self.crouching:272 self.animation.set_animation('anim2')273 if 'Crown' in self.equipment:274 self.equipment['Crown'].animation.set_animation('anim1')275 else:276 if self.in_air:277 self.animation.set_animation('anim1')278 if 'Crown' in self.equipment:279 self.equipment['Crown'].animation.set_animation('anim1')280 else:281 self.animation.set_animation('default')282 if 'Crown' in self.equipment:283 self.equipment['Crown'].animation.set_animation('default')284 285 self.animation.refresh_animation()286 if 'Crown' in self.equipment:287 self.equipment['Crown'].animation.refresh_animation()288 289 ...

Full Screen

Full Screen

snek.py

Source:snek.py Github

copy

Full Screen

1from collections import deque2from enum import Enum3import pygame as pg4import snek5class Direction(Enum):6 UP = 07 DOWN = 18 LEFT = 29 RIGHT = 310DIRECTION_MAPPING = {11 pg.K_UP: (Direction.UP, Direction.DOWN),12 pg.K_DOWN: (Direction.DOWN, Direction.UP),13 pg.K_LEFT: (Direction.LEFT, Direction.RIGHT),14 pg.K_RIGHT: (Direction.RIGHT, Direction.LEFT),15}16class Snek:17 __slots__ = ("part", "body", "maxlen", "direction", "speed", "movement_delay", "direction_changed")18 def __init__(self, part):19 self.part = part20 w = self.part.get_width()21 h = self.part.get_height()22 x = round((snek.WINDOW_WIDTH / 2) / w) * w23 y = round((snek.WINDOW_HEIGHT / 2) / h) * h24 self.body = deque(25 [snek.Sprite(self.part, x, y), snek.Sprite(self.part, x - w, y)]26 )27 self.maxlen = 228 self.direction = Direction.RIGHT29 self.speed = 1030 self.movement_delay = 0.31 self.direction_changed = False32 @property33 def movement_interval(self):34 return 1 / self.speed35 def change_direction(self, key):36 if not self.direction_changed:37 for k, (d, o) in DIRECTION_MAPPING.items():38 if key == k:39 if self.direction != o:40 self.direction = d41 self.direction_changed = True42 break43 def shift(self, x, y):44 self.body.appendleft(snek.Sprite(self.part, x, y))45 if len(self.body) > self.maxlen:46 self.body.pop()47 def extend(self):48 self.maxlen += 149 self.speed += 150 def move(self, delta):51 if self.movement_delay < self.movement_interval:52 self.movement_delay += delta53 return54 head = self.body[0]55 movement = [56 (head.x, head.y - head.h),57 (head.x, head.y + head.h),58 (head.x - head.w, head.y),59 (head.x + head.w, head.y)60 ][self.direction.value]61 self.shift(*movement)62 self.movement_delay = 0...

Full Screen

Full Screen

BounceTheBall.py

Source:BounceTheBall.py Github

copy

Full Screen

1import pygame2import sys3import random4def bounce():5 6 pygame.init()7 move_offset = [2,1]8 screen_size = screen_width,screen_height = 900 ,9009 screen = pygame.display.set_mode(screen_size)10 bg_field_surface = pygame.image.load('field.jpeg')11 screen.blit(bg_field_surface,(0,0))12 ball_surface = pygame.image.load('ball.png')13 ball_surface_rect = ball_surface.get_rect()14 pygame.display.update()15 direction_sing = [1,1]16 17 while True:18 for event in pygame.event.get():19 if(event.type == pygame.QUIT):20 pygame.quit()21 sys.exit()22 23 screen.blit(bg_field_surface,ball_surface_rect,ball_surface_rect) # erase 24 ball_surface_rect = ball_surface_rect.move(move_offset)25 direction_changed = False26 27 if ball_surface_rect.left < 0 or ball_surface_rect.right > screen_width:28 direction_sing[0] = -direction_sing[0]29 direction_changed = True30 if ball_surface_rect.top < 0 or ball_surface_rect.bottom > screen_height:31 direction_sing[1] = -direction_sing[1]32 direction_changed = True33 34 if direction_changed:35 move_offset[0] = random.randint(1,3) * direction_sing[0]36 move_offset[1] = random.uniform(1,2) * direction_sing [1]37 38 # screen.fill((0,0,0))39 screen.blit(ball_surface,ball_surface_rect)40 pygame.time.delay(10)41 #pygame.display.flip()...

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 fMBT 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