forked from ismailhasannnnnn/PyMario
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity.py
More file actions
118 lines (93 loc) · 3.85 KB
/
entity.py
File metadata and controls
118 lines (93 loc) · 3.85 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import pygame as pg
from pygame.sprite import Sprite, Group
from vector import Vector
from coords import Coords
class Entities:
def __init__(self, game):
self.game = game
self.settings = game.settings
self.screen = game.screen
self.screen_rect = self.screen.get_rect()
self.entities = Group()
def create_entity(self, ul, image):
entity = Entity(self.game, ul, image)
self.entities.add(entity)
def update(self):
for entity in self.entities:
entity.update()
def draw(self):
self.item_brick = Coords.coords["?_1"]
for entity in self.entities:
entity.draw()
class Entity(Sprite):
def __init__(self, game, ul, image):
super().__init__()
self.game = game
self.screen = game.screen
self.settings = game.settings
self.entities = game.entities
self.ul = ul
self.image = image
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = ul
self.lastBg_x = 0
self.mushroom = pg.transform.rotozoom(self.game.spritesheet.image_at(Coords.coords["MUSHROOM"]), 0,
2.5).convert_alpha()
def check_collision(self):
# if abs(self.mario.rect.x - self.rect.x) < self.rect.w abs(self.mario.rect.centery - self.rect.y) < self.rect.h:
# self.mario.v = Vector(0, 0)
collision_tolerance = 10
if self.rect.colliderect(self.mario.rect):
self.mario.colliding = True
if abs(self.mario.rect.top - self.rect.bottom) < collision_tolerance:
print('1')
self.entities.create_entity((self.rect.x * 16 * 2.5, (self.rect.y + 16) * 16 * 2.5), self.mushroom)
self.entities.draw()
if abs(self.mario.rect.bottom - self.rect.top) < collision_tolerance and self.mario.isJumping:
print('2')
self.mario.onGround = True
self.mario.v.y = 0
# if abs(self.mario.rect.left - self.rect.right) < collision_tolerance:
# print('3')
# if self.game.scrolling:
# self.game.canScroll = False
# else:
# self.game.scrolling = True
# self.mario.v.x = 0
# if abs(self.mario.rect.right - self.rect.left) < collision_tolerance:
# print('4')
# if self.game.scrolling:
# self.game.canScroll = False
# else:
# self.game.scrolling = True
# self.mario.v.x = 0
else:
self.mario.onGround = False
self.mario.colliding = False
# if self.mario.v.x < 0:
# print('1')
# self.mario.rect.x = self.rect.x + self.rect.w
#
# if self.mario.v.x > 0 or self.game.scrolling:
# print('2')
# self.mario.rect.x = self.rect.x - self.rect.w
# if self.mario.v.y < 0:
# print('3')
# self.mario.rect.y = self.rect.y + self.rect.h
# self.mario.v.y = 0
# if self.mario.v.y > 0:
# print('4')
# self.mario.rect.y = self.rect.y - self.rect.h - 16
# self.mario.v.y = 0
# print(self.mario.onGround)
def update(self):
self.mario = self.game.mario
self.enemies = self.game.enemies
self.check_collision()
self.center = Vector(self.rect.centerx, self.rect.centery)
self.difference = self.enemies.bg_x - self.lastBg_x
self.rect.centerx += self.difference
self.lastBg_x = self.enemies.bg_x
def draw(self):
self.screen.blit(self.image, (self.rect.x, self.rect.y))
# pg.draw.rect(self.screen, (0, 0, 0), self.rect)