def __init__(self):
super().__init__()
self.width = 5000
self.height = 4000
self.margin = 10
self.keys = {
Qt.Key_W: False,
Qt.Key_A: False,
Qt.Key_S: False,
Qt.Key_D: False,
} # This will be modified by View, and be read by GuiClient
self.mouseDown = False
self.mousePos = QPoint()
self.decaying = []
self.setSceneRect(5, 5, self.width, self.height)
self.polygons = ObjectTracker()
self.heroes = ObjectTracker()
self.bullets = ObjectTracker()
self.experienceBar = ExperienceBar()
self.addItem(self.experienceBar)
self.scoreboard = Scoreboard()
self.addItem(self.scoreboard)
python类Key_D()的实例源码
def action(self, **kwds):
x = y = 0
if self.scene.keys[Qt.Key_W]:
y -= 1
if self.scene.keys[Qt.Key_A]:
x -= 1
if self.scene.keys[Qt.Key_S]:
y += 1
if self.scene.keys[Qt.Key_D]:
x += 1
if x or y:
self.accelerate(math.atan2(y, x))
mpos = self.scene.views()[0].mapToScene(self.scene.mousePos)
self.shoot_at(
mpos.x(), mpos.y(),
rotate_only=not self.scene.mouseDown)
try:
while True:
self.level_up(self.to_level_up.pop())
except IndexError:
pass
def keyPressEvent(self, event):
if not self.isStarted or self.isPaused or self.curPiece.shape() == NoShape:
super(TetrixBoard, self).keyPressEvent(event)
return
key = event.key()
if key == Qt.Key_Left:
self.tryMove(self.curPiece, self.curX - 1, self.curY)
elif key == Qt.Key_Right:
self.tryMove(self.curPiece, self.curX + 1, self.curY)
elif key == Qt.Key_Down:
self.tryMove(self.curPiece.rotatedRight(), self.curX, self.curY)
elif key == Qt.Key_Up:
self.tryMove(self.curPiece.rotatedLeft(), self.curX, self.curY)
elif key == Qt.Key_Space:
self.dropDown()
elif key == Qt.Key_D:
self.oneLineDown()
else:
super(TetrixBoard, self).keyPressEvent(event)
def keyPressEvent(self, event):
if not self.isStarted or self.isPaused or self.curPiece.shape() == NoShape:
super(TetrixBoard, self).keyPressEvent(event)
return
key = event.key()
if key == Qt.Key_Left:
self.tryMove(self.curPiece, self.curX - 1, self.curY)
elif key == Qt.Key_Right:
self.tryMove(self.curPiece, self.curX + 1, self.curY)
elif key == Qt.Key_Down:
self.tryMove(self.curPiece.rotatedRight(), self.curX, self.curY)
elif key == Qt.Key_Up:
self.tryMove(self.curPiece.rotatedLeft(), self.curX, self.curY)
elif key == Qt.Key_Space:
self.dropDown()
elif key == Qt.Key_D:
self.oneLineDown()
else:
super(TetrixBoard, self).keyPressEvent(event)
def keyPressEvent(self, event):
if not self.isStarted or self.isPaused or self.curPiece.shape() == NoShape:
super(TetrixBoard, self).keyPressEvent(event)
return
key = event.key()
if key == Qt.Key_Left:
self.tryMove(self.curPiece, self.curX - 1, self.curY)
elif key == Qt.Key_Right:
self.tryMove(self.curPiece, self.curX + 1, self.curY)
elif key == Qt.Key_Down:
self.tryMove(self.curPiece.rotatedRight(), self.curX, self.curY)
elif key == Qt.Key_Up:
self.tryMove(self.curPiece.rotatedLeft(), self.curX, self.curY)
elif key == Qt.Key_Space:
self.dropDown()
elif key == Qt.Key_D:
self.oneLineDown()
else:
super(TetrixBoard, self).keyPressEvent(event)
def keyPressEvent(self, event):
key_handlers = {
Qt.Key_1: self.select_number(1),
Qt.Key_2: self.select_number(2),
Qt.Key_3: self.select_number(3),
Qt.Key_4: self.select_number(4),
Qt.Key_5: self.select_number(5),
Qt.Key_6: self.select_number(6),
Qt.Key_7: self.select_number(7),
Qt.Key_8: self.select_number(8),
Qt.Key_9: self.select_number(9),
Qt.Key_0: self.select_number(0),
Qt.Key_A: self.start_edit(self.goals.add, 'Add new goal'),
Qt.Key_C: self.with_refresh(self.goals.toggle_close),
Qt.Key_D: self.with_refresh(self.goals.delete),
Qt.Key_I: self.start_edit(self.goals.insert, 'Insert new goal'),
Qt.Key_L: self.with_refresh(self.goals.toggle_link),
Qt.Key_Q: self.quit_app.emit,
Qt.Key_R: self.start_edit(self.goals.rename, 'Rename goal'),
Qt.Key_S: self.with_refresh(self.goals.swap_goals),
Qt.Key_V: self.toggle_view,
Qt.Key_Z: self.toggle_zoom,
Qt.Key_Escape: self.cancel_edit,
Qt.Key_Space: self.with_refresh(self.goals.hold_select),
}
if event.key() in key_handlers:
key_handlers[event.key()]()
else:
super().keyPressEvent(event)
def set_up_keyboard_keys(self):
row_1 = [Qt.Key_Q, Qt.Key_W, Qt.Key_E, Qt.Key_R, Qt.Key_T, Qt.Key_Y,
Qt.Key_Y, Qt.Key_I, Qt.Key_O, Qt.Key_P, Qt.Key_Backspace]
row_2 = [Qt.Key_A, Qt.Key_S, Qt.Key_D, Qt.Key_F, Qt.Key_G, Qt.Key_H,
Qt.Key_J, Qt.Key_K, Qt.Key_L, Qt.Key_Ccedilla, Qt.Key_Return]
row_3 = [Qt.Key_Aring, Qt.Key_Z, Qt.Key_X, Qt.Key_C, Qt.Key_V,
Qt.Key_B, Qt.Key_N, Qt.Key_M, Qt.Key_Comma, Qt.Key_Period,
Qt.Key_Question, Qt.Key_Aring]
row_4 = [Qt.Key_Aring, Qt.Key_Space,
Qt.Key_Left, Qt.Key_Right, Qt.Key_Aring]
self.keyboard_keys = [row_1, row_2, row_3, row_4]
def keyPressEvent(self, event):
"""Keyboard press event
Effective key: W, A, S, D, ?, ?, ?, ?
Press a key on keyboard, the function will get an event, if the condition is met, call the function
run_action().
Args:
event, this argument will get when an event of keyboard pressed occured
"""
key_press = event.key()
# don't need autorepeat, while haven't released, just run once
if not event.isAutoRepeat():
if key_press == Qt.Key_Up: # up
run_action('camup')
elif key_press == Qt.Key_Right: # right
run_action('camright')
elif key_press == Qt.Key_Down: # down
run_action('camdown')
elif key_press == Qt.Key_Left: # left
run_action('camleft')
elif key_press == Qt.Key_W: # W
run_action('forward')
elif key_press == Qt.Key_A: # A
run_action('fwleft')
elif key_press == Qt.Key_S: # S
run_action('backward')
elif key_press == Qt.Key_D: # D
run_action('fwright')
def keyReleaseEvent(self, event):
"""Keyboard released event
Effective key: W,A,S,D, ?, ?, ?, ?
Release a key on keyboard, the function will get an event, if the condition is met, call the function
run_action().
Args:
event, this argument will get when an event of keyboard release occured
"""
# don't need autorepeat, while haven't pressed, just run once
key_release = event.key()
if not event.isAutoRepeat():
if key_release == Qt.Key_Up: # up
run_action('camready')
elif key_release == Qt.Key_Right: # right
run_action('camready')
elif key_release == Qt.Key_Down: # down
run_action('camready')
elif key_release == Qt.Key_Left: # left
run_action('camready')
elif key_release == Qt.Key_W: # W
run_action('stop')
elif key_release == Qt.Key_A: # A
run_action('fwstraight')
elif key_release == Qt.Key_S: # S
run_action('stop')
elif key_release == Qt.Key_D: # D
run_action('fwstraight')
def keyPressEvent(self, event):
"""Keyboard press event
Press a key on keyboard, the function will get an event, if the condition is met, call the function
run_action().
In camera calibration mode, Effective key: W,A,S,D, ?, ?, ?, ?, ESC
In front wheel calibration mode, Effective key: A, D, ?, ?, ESC
In back wheel calibration mode, Effective key: A, D, ?, ?, ESC
Args:
event, this argument will get when an event of keyboard pressed occured
"""
key_press = event.key()
if key_press in (Qt.Key_Up, Qt.Key_W): # UP
if self.calibration_status == 1:
cali_action('camcaliup')
elif self.calibration_status == 2:
pass
elif self.calibration_status == 3:
pass
elif key_press in (Qt.Key_Right, Qt.Key_D): # RIGHT
if self.calibration_status == 1:
cali_action('camcaliright')
elif self.calibration_status == 2:
cali_action('fwcaliright')
elif self.calibration_status == 3:
cali_action('bwcaliright')
elif key_press in (Qt.Key_Down, Qt.Key_S): # DOWN
if self.calibration_status == 1:
cali_action('camcalidown')
elif self.calibration_status == 2:
pass
elif self.calibration_status == 3:
pass
elif key_press in (Qt.Key_Left, Qt.Key_A): # LEFT
if self.calibration_status == 1:
cali_action('camcalileft')
elif self.calibration_status == 2:
cali_action('fwcalileft')
elif self.calibration_status == 3:
cali_action('bwcalileft')
cali_action('forward')
elif key_press == Qt.Key_Escape: # ESC
run_action('stop')
self.close()