Untitled
unknown
python
2 years ago
4.6 kB
9
Indexable
import pygame
from block import Block
from settings import tile_size, screen_width, screen_height, map, map2
from user import Character
class World:
def __init__(self,world_data,surface):
# level setup
self.display_surface = surface
self.world_init(world_data)
self.level_shift = -2 # value that determines if the world moves and how fast
self.current_x = 0
def world_init(self,layout):
self.blocks = pygame.sprite.Group()
self.user = pygame.sprite.GroupSingle()
#new = Block.block_type
#picture = pygame.image.load('ScuttleKnight/levels/Texture/slicedImages/ground1_01.png')
#screen = pygame.display.set_mode((screen_width,screen_height))
for row_index,row in enumerate(layout): # giving an index to both row and column
for col_index,cell in enumerate(row): # searching through the entire map array
x = col_index * tile_size
y = row_index * tile_size
if cell.isdigit(): # if the item in the array is an integer
floor = Block((x,y),tile_size, cell)
self.blocks.add(floor)
if cell == 'U':
character = Character((x,y))
self.user.add(character)
def change_map(self, map2):
self.blocks.empty()
self.user.empty()
self.world_init(map2)
def scroll_x(self):
user = self.user.sprite
user_x = user.rect.centerx
direction_x = user.direction.x
if user_x < screen_width / 3.5 and direction_x < 0:
#self.level_shift = 8
user.speed = 0
elif user_x > screen_width - (screen_width / 3.5) and direction_x > 0:
#self.level_shift = -8
user.speed = 0
else:
#self.level_shift = 0
user.speed = 8
def horizontal_collision(self):
user = self.user.sprite
user.rect.x += user.direction.x * user.speed
for sprite in self.blocks.sprites():
if sprite.rect.colliderect(user.rect):
if user.direction.x < 0:
user.rect.left = sprite.rect.right
user.c_left = True
self.current_x = user.rect.left
elif user.direction.x > 0:
user.rect.right = sprite.rect.left
user.c_right = True
user.current_x = user.rect.right
if user.c_left and (user.rect.left < self.current_x or user.direction.x <= 0):
user.c_left = False
if user.c_right and (user.rect.right > self.current_x or user.direction.x >= 0):
user.c_right = False
def vertical_collision(self):
user = self.user.sprite
user.insertGravity()
for sprite in self.blocks.sprites():
if sprite.rect.colliderect(user.rect):
if user.direction.y > 0:
user.rect.bottom = sprite.rect.top
user.direction.y = 0
user.c_ground = True
elif user.direction.y < 0:
user.rect.top = sprite.rect.bottom
user.direction.y = 0
user.c_ceiling = True
if user.c_ground and user.direction.y < 0 or user.direction.y > 1:
user.c_ground = False
if user.c_ceiling and user.direction.y > 0:
user.c_ceiling = False
def check_bounds(self):
user = self.user.sprite
# Check if user has gone out of the left or right bounds
if user.rect.left < 0:
return True
elif user.rect.right > screen_width:
return True
# Check if user has gone out of the top or bottom bounds
#if user.rect.top < 0:
# return False
# map_change = True
elif user.rect.bottom > screen_height:
return True
else:
return False
def next_level(self):
user = self.user.sprite
if user.rect.top < 0:
return True
else:
return False
def run(self):
#World
self.blocks.update(self.level_shift)
self.blocks.draw(self.display_surface)
pygame.display.update()
#self.scroll_x()
#user
self.user.update()
self.horizontal_collision()
self.vertical_collision()
self.user.draw(self.display_surface)Editor is loading...
Leave a Comment