本文整理汇总了Python中display.Display.draw方法的典型用法代码示例。如果您正苦于以下问题:Python Display.draw方法的具体用法?Python Display.draw怎么用?Python Display.draw使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类display.Display
的用法示例。
在下文中一共展示了Display.draw方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Life
# 需要导入模块: from display import Display [as 别名]
# 或者: from display.Display import draw [as 别名]
class Life(object):
__display = None
grid_size = [100, 100]
def __init__(self):
self.__display = Display(title='PyLife', grid_size=self.grid_size, width=800, height=800)
def initial_seed(self):
cells = {}
for x in range(self.grid_size[0]):
for y in range(self.grid_size[1]):
if random.randint(0, 1) == 1:
cells[(x, y)] = 1
return cells
def get_cell_neighbours(self, item, cells):
neighbours = 0
for x in range(-1, 2):
for y in range(-1, 2):
cell = (item[0] + x, item[1] + y)
if cell[0] in range(0, self.grid_size[0]) and cell[1] in range(0, self.grid_size[1]) and cell in cells:
if (x == 0 and y == 0) is False:
neighbours += 1
return neighbours
def get_cells(self, cells):
new_cells = {}
for x in range(self.grid_size[0]):
for y in range(self.grid_size[1]):
neighbours = self.get_cell_neighbours((x, y), cells)
if ((x, y) in cells) and (neighbours == 2 or neighbours == 3):
new_cells[(x, y)] = 1
elif ((x, y) in cells) is False and neighbours == 3:
new_cells[(x, y)] = 1
del cells
return new_cells
def run(self):
cells = self.initial_seed()
while True:
if self.__display.update() is False:
self.__display.quit()
sys.exit(0)
cells = self.get_cells(cells)
for cell in cells:
self.__display.draw(cell[0], cell[1])
示例2: CountdownTimer
# 需要导入模块: from display import Display [as 别名]
# 或者: from display.Display import draw [as 别名]
class CountdownTimer(Object, Drawable, Enableable, Completable):
def __init__(self, x, y, maxTime, numNums=3):
image, width = const.numbers
numbers = Files.loadImage(image)
numbers.set_colorkey(numbers.get_at((0, 0)))
super().__init__(rect=(x, y, width * numNums, numbers.get_height()), enabled=True)
self._numbers = []
self._digits = []
self._maxTime = maxTime
self._currentTime = maxTime
self._blank = Display(Surface((width, self.h)))
for i in range(width):
self._numbers.append(Display(numbers.subsurface((i * width, 0, width, self.h))))
surf = Surface((self.w + 2 * numNums, self.h))
self._display = Display(surf, self, transparent=True, alpha=200)
for i in range(numNums):
self._digits.append(surf.subsurface((i * width + 2 * i, 0, width, self.h)))
self._lastUpdate = -maxTime
def reset(self):
self.setInProgress()
self._currentTime = self.maxTime
self.tick()
def _getDigit(self, index):
t = self._currentTime
for i in range(len(self._digits) - index - 1):
t = t // 10
return t % 10
def tick(self):
if self._currentTime >= 0:
newTime = time.perf_counter()
if newTime - self._lastUpdate > 1:
for i, digit in enumerate(self._digits):
self._blank.draw(digit)
self._numbers[self._getDigit(i)].draw(digit)
self._currentTime -= 1
self._lastUpdate = newTime
else:
self.setFinished()
示例3: main
# 需要导入模块: from display import Display [as 别名]
# 或者: from display.Display import draw [as 别名]
def main():
points = []
for _ in range(100):
points.append(Point(randrange(Display.height - 1),
randrange(Display.width),
randrange(-1,2),
randrange(-1,2)
)
)
grid_size = GridSize(Display.height - 1, Display.width)
for _ in range(250):
Display.draw(points)
for point in points:
point.move()
point.bounce(grid_size)
示例4: __init__
# 需要导入模块: from display import Display [as 别名]
# 或者: from display.Display import draw [as 别名]
class Manager:
def __init__(self):
self.display = Display()
self._spawn_piece()
def _spawn_piece(self):
'''
Produce a new piece
'''
self.display.update_board()
self.current_piece = Piece()
try:
self.display.add_piece(self.current_piece)
self.display.draw()
except OverlapError:
print('Game Over')
exit()
def game_loop(self):
'''
Execute for each user input
'''
move = raw_input('Please enter a move: ').strip()
action = ACTIONS.get(move)
if action is None:
print('Entered move does not exist! Try again.')
return
self.current_piece.move(action)
try:
self.display.show_piece()
except OverlapError:
self.current_piece.undo()
print('Invalid move! Try again.')
return
if self.display.check_valid_moves():
self.display.draw()
else:
self._spawn_piece()
示例5: initialize_interface
# 需要导入模块: from display import Display [as 别名]
# 或者: from display.Display import draw [as 别名]
class GlobalMapState:
def initialize_interface(self):
self.display = Display(self.game_data)
self.uis = UIState(self.game_data)
#Building buttons callbacks
self.uis.button_homes.callback = self.generate_building_callback('houses')
self.uis.button_fields.callback = self.generate_building_callback('field')
self.uis.button_woodcutter.callback = self.generate_building_callback('woodcutter')
self.uis.button_road.callback = self.generate_building_callback('road')
self.uis.button_port.callback = self.generate_building_callback('port')
self.uis.button_stockpile.callback = self.generate_building_callback('stockpile')
self.uis.button_stone_carrier.callback = self.generate_building_callback('stone_carrier')
self.uis.button_workshop.callback = self.generate_building_callback('workshop')
self.uis.button_iron_mine.callback = self.generate_building_callback('iron_mine')
self.uis.button_destruct.callback = self.generate_building_callback('destruct')
self.display.uis = self.uis
self.game_data.uis = self.uis
self.uis.display = self.display
self.uis.save_button.callback = self.save_game
self.uis.load_button.callback = self.load_game
#Listeners
event_manager.event_manager.add_listener(event_manager.MESSAGE_MOUSE_UP, self.uis.mouse_button_up_callback)
event_manager.event_manager.add_listener(event_manager.MESSAGE_KEY_UP, self.keyup_callback)
event_manager.event_manager.add_listener(event_manager.MESSAGE_KEY_DOWN, self.keydown_callback)
event_manager.event_manager.add_listener(event_manager.MESSAGE_MOUSE_MOTION, self.mouse_move)
event_manager.event_manager.add_listener(event_manager.MESSAGE_MOUSE_DOWN, self.mouse_key_down_callback)
event_manager.event_manager.add_listener(event_manager.MESSAGE_KEY_UP, self.uis.check_button_up)
def remove_listeners(self):
event_manager.event_manager.remove_listener(event_manager.MESSAGE_MOUSE_UP, self.uis.mouse_button_up_callback)
event_manager.event_manager.remove_listener(event_manager.MESSAGE_KEY_UP, self.keyup_callback)
event_manager.event_manager.remove_listener(event_manager.MESSAGE_KEY_DOWN, self.keydown_callback)
event_manager.event_manager.remove_listener(event_manager.MESSAGE_MOUSE_MOTION, self.mouse_move)
event_manager.event_manager.remove_listener(event_manager.MESSAGE_MOUSE_DOWN, self.mouse_key_down_callback)
event_manager.event_manager.remove_listener(event_manager.MESSAGE_KEY_UP, self.uis.check_button_up)
def __init__(self):
self.game_data = None
def process(self):
self.game_data.process()
self.display.draw()
def generate_building_callback(self, building):
def callback():
self.uis.building = building
return callback
def keyup_callback(self, key):
if not self.uis.dialog:
if key == pygame.K_RETURN:
self.game_data.next_turn()
if key == pygame.K_ESCAPE:
self.uis.dialog = self.uis.main_menu
else:
if key == pygame.K_RETURN:
if self.uis.dialog.enter_btn:
self.uis.dialog.enter_btn.run_callback()
elif key == pygame.K_ESCAPE:
if self.uis.dialog.esc_btn:
self.uis.dialog.esc_btn.run_callback()
if key == pygame.K_UP or key == pygame.K_DOWN:
self.game_data.scroll_spd_y = 0
if key == pygame.K_LEFT or key == pygame.K_RIGHT:
self.game_data.scroll_spd_x = 0
def keydown_callback(self, key):
if key == pygame.K_UP:
self.game_data.scroll_spd_y = -5
if key == pygame.K_LEFT:
self.game_data.scroll_spd_x = -5
if key == pygame.K_DOWN:
self.game_data.scroll_spd_y = 5
if key == pygame.K_RIGHT:
self.game_data.scroll_spd_x = 5
def mouse_move(self, pos_x, pos_y):
self.display.mouse_x, self.display.mouse_y = pos_x, pos_y
if self.game_data.drag:
#And here we move the map on the screen
mouse_pos_x, mouse_pos_y = pos_x, pos_y
self.game_data.dx = self.game_data.old_dx + (mouse_pos_x - self.game_data.mouse_drag_x)
self.game_data.dy = self.game_data.old_dy + (mouse_pos_y - self.game_data.mouse_drag_y)
else:
if pos_x < 10:
self.game_data.scroll_spd_x = -5
elif 590 < pos_x < 600:
self.game_data.scroll_spd_x = 5
else:
self.game_data.scroll_spd_x = 0
if pos_y < 10:
self.game_data.scroll_spd_y = -5
elif pos_y > 590:
self.game_data.scroll_spd_y = 5
else:
self.game_data.scroll_spd_y = 0
def mouse_key_down_callback(self, pos_x, pos_y, button):
if button == 1 and pos_x < 600 and not self.uis.dialog:
#Here we start drag the map
#.........这里部分代码省略.........
示例6: GameManager
# 需要导入模块: from display import Display [as 别名]
# 或者: from display.Display import draw [as 别名]
'''
Created on April 12, 2014
@author: Clayton
'''
import pygame
pygame.init()
screenSize = (320, 318)
surface = pygame.display.set_mode(screenSize)
from display import Display
from gameManager import GameManager
gameManager = GameManager()
display = Display(gameManager)
clock = pygame.time.Clock()
winner = None
while winner is None:
clock.tick(30)
winner = gameManager.doTurn()
display.draw(surface)
if winner:
print "You Won!"
else:
print 'You Lost!'
示例7: Display
# 需要导入模块: from display import Display [as 别名]
# 或者: from display.Display import draw [as 别名]
from display import Display
from controller import Controller
display = Display(10, 20)
controller = Controller()
led_x = 1
led_y = 20
while True:
display.pixel_off(led_x, led_y)
cmd = controller.get_next_cmd()
if cmd == 'LEFT':
if led_x != 1:
led_x -= 1
elif cmd == 'RIGHT':
if led_x != 10:
led_x += 1
led_y += 1
pixel_test = {'x': led_x, 'y': led_y, 'r': 1, 'g': 1, 'b': 1}
display.pixel_on(pixel=pixel_test)
display.draw()
示例8: Display
# 需要导入模块: from display import Display [as 别名]
# 或者: from display.Display import draw [as 别名]
import zmq
from display import Display
port = '5555'
context = zmq.Context()
socket = context.socket(zmq.PAIR)
socket.RCVTIMEO = 1000
socket.connect('tcp://localhost:%s' % port)
display = Display()
while True:
msg = 'DOWN'
try:
msg = socket.recv()
except zmq.ZMQError:
msg = 'DOWN'
display.draw(msg)
socket.close()
示例9: Viewport
# 需要导入模块: from display import Display [as 别名]
# 或者: from display.Display import draw [as 别名]
class Viewport(ScaledObject, Scaled):
"""
A `Scaleable` view onto some object.
"""
def __init__(self, size, scale, target=None, boundBox=Object(), maxBounds=None):
"""
`size` - The size (width and height).
`scale` - The scale at which to render the view.
`target` - (Optional) An object that the position of the `Viewport` should follows. If
not set, the `Viewport` will not move automatically.
`boundBox` - (Optional) A sub-view of the `Viewport`, inside which the `target` should
always remain. If not set, it will be set to the Viewport's size.
`maxBounds` - (Optional) A Rectangle inside of which the `Viewport` should always remain.
If not set, there will be no max/min bounds on the position of the `Viewport`.
"""
super().__init__(size=size, scale=scale)
self._target = target
self._boundBox = ScaledOffsetObject(rect=boundBox, scale=self.getScale)
self._maxBounds = maxBounds
self._display = Display(Surface(size))
self._displaySurfaceCache = self._display.getImage()
self._surfaceCache = None
self._lastScale = None
def getAbsolutePos(self, pos):
"""
Returns the `pos` converted into terms of the `Viewport`.
This is important when the scale is not 1 because the on-screen position will not
match the actual position being passed in at arbitrary scales.
"""
return self.x + (pos[0] * self.getScale()), self.y + (pos[1] * self.getScale())
def tick(self):
"""
Adjusts the position of the `Viewport` to keep the `target` inside `boundBox`, and
to keep the `Viewport` inside `maxBounds`.
"""
# Adjusts x and y to relative to the target.
if self._target:
if self.x > self._target.x - self._boundBox.x:
self.x = self._target.x - self._boundBox.x
if self.x < self._target.x + self._target.w + self._boundBox.w - self.w:
self.x = self._target.x + self._target.w + self._boundBox.w - self.w
if self.y > self._target.y - self._boundBox.y:
self.y = self._target.y - self._boundBox.y
if self.y < self._target.y + self._target.h + self._boundBox.h - self.h:
self.y = self._target.y + self._target.h + self._boundBox.h - self.h
# Adjusts x and y relative to the maximum/minimum bounds.
if self._maxBounds:
if self.x < self._maxBounds.x:
self.x = self._maxBounds.x
if self.x + self.w > self._maxBounds.w:
self.x = self._maxBounds.w - self.w
if (self.x < 0) and (self.x + self.w > self._maxBounds.w):
self.x = self._target.x + self._target.w / 2 - self.w / 2
if self.y < self._maxBounds.y:
self.y = self._maxBounds.y
if self.y + self.h > self._maxBounds.h:
self.y = self._maxBounds.h - self.h
if (self.y < 0) and (self.y + self.h > self._maxBounds.h):
self.y = self._target.y + self._target.h / 2 - self.h / 2
def draw(self, surface, total_surface):
"""
Calculate the scaling and display to the inputted surface.
`surface` - The `Surface` being drawn to.
`total_surface` - The `Surface` being copied from.
"""
scale = self.getScale()
# Optimization, no need to do scaling when the scale is 1.
if scale == 1:
self._display.update(total_surface, Display.translate(total_surface.get_rect(), self))
else:
# Optimization, caches the `Surface` object so long as the scale isn't changing.
if scale != self._lastScale:
self._surfaceCache = Surface((self.w, self.h))
self._surfaceCache.blit(total_surface, (-self.x, -self.y))
scaleTo = self.unscaled()
transform.scale(self._surfaceCache, (scaleTo.w, scaleTo.h), self._displaySurfaceCache)
self._lastScale = scale
self._display.draw(surface)
示例10: display
# 需要导入模块: from display import Display [as 别名]
# 或者: from display.Display import draw [as 别名]
def display(self):
Display.draw(self.tag_cloud_items)
示例11: __init__
# 需要导入模块: from display import Display [as 别名]
# 或者: from display.Display import draw [as 别名]
class Carte:
"""Represent a map containing BTS and MS"""
def __init__(self, width=800, height=600):
(self.__width, self.__height) = (width, height)
self.__display = Display(width, height)
self.__bts = {}
self.__ms = set()
self.__color_index = 0
self.__filename = None
self.__speed = 1.
self.__freqs_color = {}
log.nb_handover = 0
self.__display.action_load.triggered.connect(self.load_file)
self.__display.action_reload.triggered.connect(self.reload_file)
self.__display.action_play.triggered.connect(self.__update_moving_ms)
self.__display.action_incSpeed.triggered.connect(
lambda: self.change_speed(1))
self.__display.action_decSpeed.triggered.connect(
lambda: self.change_speed(-1))
self.__display.speed_lineedit.editingFinished.connect(
lambda: self.set_speed(float(self.__display.speed_lineedit.text())))
self.__move_timer = QtCore.QTimer()
self.__move_timer.timeout.connect(self.movems)
self.set_speed(1)
self.__display.show()
def add(self, elem):
"""Add an element on map"""
# Add a BTS and update all MS
if isinstance(elem, BTS):
if not elem.f in self.__freqs_color:
self.__freqs_color[elem.f] = len(self.__freqs_color) + 1
idx_color = self.__freqs_color[elem.f]
color = get_color(idx_color, elem.network == "GSM")
self.__bts[elem] = QtGui.QColor(*color)
bts_set = set(self.__bts.keys())
for ms in self.__ms:
ms.update_bts_list(bts_set)
# Add a MS and update it with BTS list
elif isinstance(elem, MS):
self.__ms.add(elem)
elem.set_speed(self.__speed)
elem.update_bts_list(set(self.__bts.keys()))
else:
return
self.refresh()
def refresh(self):
"""Refresh map display"""
self.__display.clean()
for (bts, color) in self.__bts.iteritems():
self.__display.draw(bts, color)
for ms in self.__ms:
self.__display.draw(ms, self.__bts[ms.bts])
self.__display.update()
def change_speed(self, offset):
"""Change map speed with offset"""
self.set_speed(self.__speed + offset)
def set_speed(self, speed):
"""Set map speed"""
speed = float(min(5, max(0, speed)))
self.__move_timer.setInterval(800/pow(2, speed-1))
self.__speed = speed
for ms in self.__ms:
ms.set_speed(speed)
self.__display.speed_lineedit.setText(str(self.__speed))
def movems(self):
"""Move all MS"""
for ms in self.__ms:
ms.random_move(self.__width-1, self.__height-1)
self.refresh()
def start_moving_ms(self):
"""Start movin MS on map"""
self.__move_timer.start()
def __update_moving_ms(self):
"""Update MS movig state according to interface"""
if self.__display.action_play.isChecked():
self.__move_timer.stop()
else:
self.__move_timer.start()
def load_file(self, filename=None):
"""Load an xml file"""
if not filename:
#.........这里部分代码省略.........
示例12: Char
# 需要导入模块: from display import Display [as 别名]
# 或者: from display.Display import draw [as 别名]
class Char(Object, Dir, IDed, Drawable, Health, Subscribee, Alive, Inputable):
def __init__(self, level, speed, tileset, **kwargs):
super().__init__(
dirRule=lambda: {
-1: Direction.Left,
0: None,
1: Direction.Right
}[self.move.getDir(x=True)],
**kwargs)
self.collision = Collision(self, level)
self.move = Move(self, speed, collision=self.collision)
self._gravity = GravityLine(self, 2, h=level.map.h // 2)
self.jumping = Jumping(self.move, self._gravity, 2)
self._input = Input(inputStream=self.getInputStream())
self.applyInputSettings()
self._damageTimer = MinTimeEventStream(duration=2)
self._damageTimer.subscribe("self", self._takeDmg, autoInit=False)
def _isMoving():
return self.move.getSpeed(x=True) != 0
def _hDir():
return self.move.getDir(x=True)
def _vDir():
return {False: -1, True: 1}[self._gravity.positiveDir()]
image = Files.loadImage(tileset)
self._display = Display(image, self, Animation(image, 11, _isMoving, _hDir, _vDir), True)
self._weapon = Weapon(level,
anchor=self,
offsetFunc=lambda: ((5 * self.getIntDir()), 0),
inputStream=self.getInputStream())
def applyInputSettings(self):
self._input.set(pygame.KEYDOWN, self.startMove, pygame.K_a, "left")
self._input.set(pygame.KEYDOWN, self.startMove, pygame.K_d, "right")
self._input.set(pygame.KEYDOWN, self.startMove, pygame.K_w, "up")
self._input.set(pygame.KEYDOWN, self.startMove, pygame.K_s, "down")
self._input.set(pygame.KEYUP, self.stopMove, pygame.K_a, "left")
self._input.set(pygame.KEYUP, self.stopMove, pygame.K_d, "right")
self._input.set(pygame.KEYUP, self.stopMove, pygame.K_w, "up")
self._input.set(pygame.KEYUP, self.stopMove, pygame.K_s, "down")
def startMove(self, arg):
if arg == "left":
self.move.setSpeed(x=-self.move.getTopSpeed(x=True))
if arg == "right":
self.move.setSpeed(x=self.move.getTopSpeed(x=True))
if arg == "up":
self.jumping.jump()
def stopMove(self, arg):
xspeed = self.move.getSpeed(x=True)
if arg == "left" and xspeed < 0:
self.move.setSpeed(x=0)
if arg == "right" and xspeed > 0:
self.move.setSpeed(x=0)
if arg == "up":
self.jumping.muteJump()
def draw(self, surface, camera=Object()):
self._display.draw(surface, camera)
self._weapon.draw(surface, camera)
def subscribe(self, id, callback, autoInit=True):
self._damageTimer.subscribe(id, callback, autoInit)
def unsubscribe(self, id):
self._damageTimer.unsubscribe(id)
def _takeDmg(self, ignore):
self.decHealth(1)
self.move.setSpeed(y=8)
def tick(self):
super().tick()
self._input()
self._weapon.tick()
collisions = self.move()
self.jumping.tick(collisions)
self._gravity.tick(collisions)
if collisions.get(Tiles.Deadly):
self._damageTimer.notify()
if self.getHealth() == 0:
self.kill()
return collisions