Advertisement
Hppavilion1

Sprite Won't Render

Dec 27th, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. # Note that qp is .begin'd before this, so that isn't the issue
  2.  
  3. def draw(self, game, qp):  # When called, draws its "sprite" attribute using qp. Qp should be a QPainter.
  4.     if self is game.player:  # For a sprite which is the player (rendered in the centre of the window)
  5.         geom = game.geometry()  # Get the size of the game window to find the centre
  6.         sprite = self.sprite  # May be the problem (an empty image), but I would think that would raise an error
  7.                               # when the image is loaded
  8.         r = self.rotation  # The rotation in degrees of the current sprite
  9.  
  10.         dx = 10*sprite.width()  # x-offset of opposing corner
  11.         dy = 10*sprite.height()  # y-offset of opposing corner
  12.         qp.save()  # Record the current transformation (at least, that's what it seems to do)
  13.         qp.translate(geom.width()/2, geom.height()/2)  # Go to the center of the canvas
  14.         qp.rotate(r)  # Rotate the image accordingly
  15.  
  16.         rec = QRect(-dx/20, -dy/20, dx, dy)  # The area where the sprite should be drawn (I, frankly, have no clue
  17.                                              # why this is the way it is, but it worked in the other one I made)
  18.  
  19.         qp.drawImage(rec, sprite, game.rect)  # Draw the image on the game (game.rect is the rect() method of any
  20.                                               # paint event that occurs, to keep everything lined up)
  21.  
  22.         qp.restore()  # Revert the transformations so that subsequent drawings are in the right location
  23.  
  24.     else:  # For any other non-player sprite
  25.         transformed = pos_transform(game.player.position, self.position)  # pos_transform is used for relative
  26.                                                                           # positioning based on a fixed point
  27.         x = transformed.x  # pos_transform returns a point with the attributes x and y
  28.         y = transformed.y
  29.        
  30.         # From this point on, future code will only finish if the sprite falls in the game geometry, such as to be
  31.         # more efficient
  32.         sprite = self.sprite  # Again, a QImage
  33.         r = self.rotation  # The rotation in degrees of the current sprite
  34.  
  35.         dx = 10*sprite.width()  # x-offset
  36.         dy = 10*sprite.height()  # y-offset
  37.         qp.save()  # Save transformation
  38.         qp.translate(x, y)  # Go to the proper location
  39.         qp.rotate(r)  # Rotation
  40.  
  41.         rec = QRect(-dx/20, -dy/20, dx, dy)  # Still don't know why this works
  42.  
  43.         qp.drawImage(rec, sprite, game.rect)  # Draw the image
  44.  
  45.         qp.restore()  # Revert the transformations
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement