-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (57 loc) · 2.05 KB
/
Copy pathmain.py
File metadata and controls
74 lines (57 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from pathlib import Path
import pygame
from pygame import Rect, Color, Surface
from pygame.sprite import Sprite
from PygameToolsBox.animated_object import AnimatedObject, ANIMATION_END
from PygameToolsBox.spritesheet import SpriteSheet
pygame.init()
win = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
class Bullet(Sprite):
def __init__(self):
super().__init__()
self.image = Surface((10, 10))
self.mask = pygame.mask.from_surface(self.image)
self.rect = self.image.get_rect()
def update(self, pos: Rect):
self.rect.topleft = pos.topleft
sprites = SpriteSheet(Path("sprites.png").resolve(), 60, 60, 3, 7)
player = AnimatedObject(sprites.get_sprite_list(), 1)
player.add_action("run", -1, 8, 15)
player.add_action("jump", 0, 1, 7)
player.add_action("begin_slide", 0, 16, 17)
player.add_action("end_slide", 0, 17, 20)
player.add_action("dead", -1, 0, 0)
player.set_action("run")
player.rect = Rect(100, 100, 60, 60)
bullet = Bullet()
run = True
while run:
clock.tick(20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == ANIMATION_END:
player.set_action("run")
keys = pygame.key.get_pressed()
action = player.get_action()
if keys[pygame.K_DOWN] and action != "begin_slide":
player.set_action("begin_slide")
elif not keys[pygame.K_DOWN] and action == "begin_slide":
player.set_action("end_slide")
elif keys[pygame.K_UP] and action != "jump":
player.set_action("jump")
if pygame.sprite.collide_rect(player, bullet) and pygame.sprite.collide_mask(player, bullet):
if pygame.mouse.get_pressed(3)[0]:
player.set_action("dead")
bullet.image.fill(Color(255, 0, 0))
else:
bullet.image.fill(Color(0, 0, 255))
win.fill(Color(0, 0, 0))
player.pos.x += 1
player.update()
player.draw(win)
bullet.rect.center = pygame.mouse.get_pos()
win.blit(bullet.image, bullet.rect)
pygame.display.update()