|
1 |
| -import pygame |
2 |
| - |
3 |
| -pygame.init() |
4 |
| - |
5 |
| -# Set up the window |
6 |
| -screen = pygame.display.set_mode((640, 480)) |
7 |
| - |
8 |
| -# Load the sprite image |
9 |
| -sprite_image = pygame.image.load("sprite.png") |
10 |
| - |
11 |
| -# Define the rotation center offset relative to the sprite's origin |
12 |
| -rotation_center_offset = (20, 10) |
13 |
| - |
14 |
| -# Define the initial angle of the sprite |
15 |
| -angle = 0 |
16 |
| - |
17 |
| -while True: |
18 |
| - for event in pygame.event.get(): |
19 |
| - if event.type == pygame.QUIT: |
20 |
| - pygame.quit() |
21 |
| - quit() |
22 |
| - |
23 |
| - # Calculate the rotation center position relative to the sprite's origin |
24 |
| - rotation_center = ( |
25 |
| - rotation_center_offset[0], |
26 |
| - rotation_center_offset[1] |
27 |
| - ) |
28 |
| - |
29 |
| - # Rotate the sprite image around the rotation center |
30 |
| - rotated_image = pygame.transform.rotate(sprite_image, angle) |
31 |
| - |
32 |
| - # Get the rect of the rotated image |
33 |
| - rect = rotated_image.get_rect() |
34 |
| - |
35 |
| - # Calculate the position of the sprite based on the rotation center and rect dimensions |
36 |
| - position = ( |
37 |
| - rotation_center[0] - rect.width / 2, |
38 |
| - rotation_center[1] - rect.height / 2 |
39 |
| - ) |
40 |
| - |
41 |
| - # Draw the rotated image to the screen at the calculated position |
42 |
| - screen.blit(rotated_image, position) |
43 |
| - |
44 |
| - # Increment the angle |
45 |
| - angle += 0.125 |
46 |
| - |
47 |
| - # Update the screen |
48 |
| - pygame.display.update() |
| 1 | +import pygame as pg |
| 2 | +from pygame.math import Vector2 |
| 3 | + |
| 4 | + |
| 5 | +class Entity(pg.sprite.Sprite): |
| 6 | + |
| 7 | + def __init__(self, pos): |
| 8 | + super().__init__() |
| 9 | + self.image = pg.Surface((122, 70), pg.SRCALPHA) |
| 10 | + pg.draw.polygon(self.image, pg.Color('dodgerblue1'), |
| 11 | + ((1, 0), (120, 35), (1, 70))) |
| 12 | + # A reference to the original image to preserve the quality. |
| 13 | + self.orig_image = self.image |
| 14 | + self.rect = self.image.get_rect(center=pos) |
| 15 | + self.pos = Vector2(pos) # The original center position/pivot point. |
| 16 | + self.offset = Vector2(50, 0) # We shift the sprite 50 px to the right. |
| 17 | + self.angle = 0 |
| 18 | + |
| 19 | + def update(self): |
| 20 | + self.angle += 2 |
| 21 | + self.rotate() |
| 22 | + |
| 23 | + def rotate(self): |
| 24 | + """Rotate the image of the sprite around a pivot point.""" |
| 25 | + # Rotate the image. |
| 26 | + self.image = pg.transform.rotozoom(self.orig_image, -self.angle, 1) |
| 27 | + # Rotate the offset vector. |
| 28 | + offset_rotated = self.offset.rotate(self.angle) |
| 29 | + # Create a new rect with the center of the sprite + the offset. |
| 30 | + self.rect = self.image.get_rect(center=self.pos+offset_rotated) |
| 31 | + |
| 32 | + |
| 33 | +def main(): |
| 34 | + screen = pg.display.set_mode((640, 480)) |
| 35 | + clock = pg.time.Clock() |
| 36 | + entity = Entity((320, 240)) |
| 37 | + all_sprites = pg.sprite.Group(entity) |
| 38 | + |
| 39 | + while True: |
| 40 | + for event in pg.event.get(): |
| 41 | + if event.type == pg.QUIT: |
| 42 | + return |
| 43 | + |
| 44 | + keys = pg.key.get_pressed() |
| 45 | + if keys[pg.K_d]: |
| 46 | + entity.pos.x += 5 |
| 47 | + elif keys[pg.K_a]: |
| 48 | + entity.pos.x -= 5 |
| 49 | + |
| 50 | + all_sprites.update() |
| 51 | + screen.fill((30, 30, 30)) |
| 52 | + all_sprites.draw(screen) |
| 53 | + pg.draw.circle(screen, (255, 128, 0), [int(i) for i in entity.pos], 3) |
| 54 | + pg.draw.rect(screen, (255, 128, 0), entity.rect, 2) |
| 55 | + pg.draw.line(screen, (100, 200, 255), (0, 240), (640, 240), 1) |
| 56 | + pg.display.flip() |
| 57 | + clock.tick(30) |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == '__main__': |
| 61 | + pg.init() |
| 62 | + main() |
| 63 | + pg.quit() |
0 commit comments