-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatforms.py
91 lines (67 loc) · 2.71 KB
/
platforms.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""platforms.py"""
"""
This file is part of The Last Caturai.
The Last Caturai is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The Last Caturai is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
"""
"""Creates and manages platforms."""
import pygame
import tile
#GRASS_LEFT = (576, 720, 70, 70)
#GRASS_RIGHT = (576, 576, 70, 70)
#GRASS_MIDDLE = (504, 576, 70, 70)
STONE_BLOCK = (504, 720, 70, 70)
DOOR = (74, 0, 70, 70)
EXIT = (288, 358, 70, 70)
STONE_PLATFORM_LEFT = (432, 720, 70, 40)
STONE_PLATFORM_MIDDLE = (648, 648, 70, 40)
STONE_PLATFORM_RIGHT = (792, 648, 70, 40)
class Platform(pygame.sprite.Sprite):
def __init__(self, tile_sheet_data):
pygame.sprite.Sprite.__init__(self)
tile_sheet = tile.Tile("tileset.png")
self.image = tile_sheet.get_image(tile_sheet_data[0], tile_sheet_data[1], tile_sheet_data[2], tile_sheet_data[3])
self.rect = self.image.get_rect()
class MovingPlatform(Platform):
change_x = 0
change_y = 0
boundary_top = 0
boundary_bottom = 0
boundary_left = 0
boundary_right = 0
level = None
player = None
def update(self):
# Moving the platform left and right
self.rect.x += self.change_x
# As the platform: am I hitting the player?
hit = pygame.sprite.collide_rect(self, self.player)
if hit:
if self.change_x < 0:
self.player.rect.right = self.rect.left
else:
self.player.rect.left = self.rect.right
# Moving the platform up and down
self.rect.y += self.change_y
# As the platform: am I hitting the player?
hit = pygame.sprite.collide_rect(self, self.player)
if hit: # Same
if self.change_y < 0:
self.player.rect.bottom = self.rect.top
else:
self.player.rect.top = self.rect.bottom
if self.rect.bottom > self.boundary_bottom or self.rect.top < self.boundary_top:
self.change_y *= -1
cur_pos = self.rect.x - self.level.world_shift
if cur_pos < self.boundary_left or cur_pos > self.boundary_right:
self.change_x *= -1