Collisions w/ Tiles on Tile Map Python 3.6 - python

I am making a small game for my AP Econ class and have been following a tutorial to create a tile map and am having difficulty with collisions. I have searched through the questions on this site as well as anywhere else that I could find programming related questions (Primarily here) but have not been able to find any that are don't have a map built by hand (Building a map by hand in a text document or directly in the python file with a list). I am new at python so the answer might be fairly straight forward XD.
Main Program: https://pastebin.com/VbGVzJab
Textures: https://pastebin.com/iTBpiub3
Colors: https://pastebin.com/ywMKv6Z1
Globals: https://pastebin.com/E40a7Kss
Map_Engine: https://pastebin.com/PJgnSFJP
Map_Editor: https://pastebin.com/mvgaYBDF
Tutorial I have been following: https://www.youtube.com/watch?v=sxk4Oi9QOrM&t=997s
This is how my folders are set up:
My issue is that I my character (player) continues to walk instead of stopping at the water (Or whichever tile type is designated (In my case it is currently water). Thank you all in advance!!

I don't know if this will help, but you can try either making movement variables to signal if the person can move that way, and when you come across that area of coordinates (the water), you can set those coordinates to False. If you made coordinates (x and y to move your character), it shouldn't be hard.
Please note I'm just using a random coordinate area
MOVE_RIGHT = True
MOVE_LEFT = True
MOVE_UP = True
MOVE_DOWN = True
if x == 250:
MOVE_RIGHT = False
if event.key == K_RIGHT and MOVE_RIGHT == True:
x += 1
Hope this helps!

Related

Is there a way to keep an object in bounds in pygame, and make it move when you hold the directional keys down?

I am using pygame 1.9.6 and python 3.7.7. I am looking for a way to have something drawn by pygame(or maybe even an image) that stays inside the window border(and stops when I release the key. I started with the “if keys[]:” method:
if keys[pygame.K_LEFT] and x>0:
x-=speed
that made it stay in bounds, but it only moved a few pixels and then did not repeat.
Next I tried the “event.key” method:
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_LEFT:
xchange=-speed
ychange=-0
but that just makes the object move forever. I tried putting “and x>0” on the same line right after the direction, an “if x>0:” before, an “if x<0:” after, but the event never updates to see that the coordinates are past the edge, even with update commands. It just keeps going and going. I also don’t know how to make the object stop moving when I release the key, since event.key’s have the event always on.
Thank you for all the help you can offer.
In order for something to stay within a frame, you must check if it's coordinates are not past the border before moving it.
keys = pygame.key.get_pressed()
if(keys[K_LEFT]):
if(square.left > 0):
square.left -= 10
if(keys[K_RIGHT]):
if(square.right < 600):
square.left += 10
if(keys[K_UP]):
if(square.top > 0):
square.top -= 10
if(keys[K_DOWN]):
if(square.bottom < 600):
square.bottom += 10
This is an example of how you would do this with a Rect object, it checks for border and then moves the object if its within those border and the appropriate key is being pressed. I can't apply this to your code as I cannot see enough of it but you just have to replace the coordinates. For example, you might have thing.x instead of square.left

Python Inputs module - detect axis of gamepad joystick with lenience?

To simplify this problem, let's say I simply want to be able to detect whether my gamepad's right thumbstick is being pushed up (but not horizontally, within some lenience), pushed to the right (but not up, within some lenience) or being pushed to the left (but not up, within some lenience).
Unfortunately, the lenience is required, but I haven't found a way to do it yet. Thusfar I have been using the inputs library:
https://pypi.org/project/inputs/
But it's not super well documented and as far as I can tell it doesn't let you compare an exact angle of the thumbstick at any one time.
Using their own example code, I have this snippet of a function:
def get_true_game_input():
inputArray = [0,0,0,0,0,0,0,0,0,0,0,0,0,0]
# All the different buttons that are important to be detected, including whether the thumbstick has been moved left, right, or up
events = get_gamepad()
for event in events:
if event.code == "ABS_RY":
if event.state >= 8000:
print("top Guard!")
inputArray[5] = 1
elif event.code == "ABS_RX":
if event.state >= 8000:
inputArray[6] = 1
print("right guard!")
elif event.state <= -8000:
inputArray[4] = 1
print("left guard!")
Which works up until one moves the stick, for example, upwards, then ever so slightly to the left or right, whereupon it will detect that instead.
The problem is that, with the way Inputs seems to work, events is always either empty, or of size 1. So it will either detect a horizontal movement or a vertical movement, and one will overwrite the other.
Is there some way to solve this? I am also open to rebuilding this using a different library, but cannot find any other. I am using a dualshock 4 controller using the DS4 Tool.
Any help or advice would be greatly appreciated. Thank you.

Finding Next Obstacle in List

I am trying to create a game that moves to the end of a square. Inside the game I created obstacles by making a list of true and false (false=obstacles). What I have (but did not post) is something that detects an obstacle ONLY IF the user lands on the obstacle. However, I want to find a way to detect an obstacle BEFORE the user makes its next move and make it stay in place if the next spot will be an obstacle. In other words I want to find the next index of list before proceeding. Here's some pseudo code for a better picture:
if next_left != [[False]]: # if there is no obstacle
officially_move_left
else: # if there is an obstacle
user_do_nothing
What I have (in pseudo):
def moving_pos(user):
copy_user.pos = user.pos
if copy_user.pos +1 == [True]
user.pos += 1
copy_user.pos += 1
else:
return user.pos
if (user_position == obstacle):
next_square_is_obstacle = true
user_move_backwards
ie, move the player, detect the object, move the player back again. It's very difficult to help without seeing the actual implementation
Edit: Okay, in your new edit I don't see any reason why the program should have such constraints. However, let me suggest something new-
obstacle_index = [obs1, obs2, obs3...]
if user_position+1 in obstacle_index:
do_nothing;
else
user_move_forward;
Can you post the actual minimal working code, or at least a more detailed pseudocode of the implementation?

My Character Sprite Doesn't Move Off Screen

I have been going over Paul Craven's Python and Pygame tutorial and lately I am trying to understand everything in Sprites. So, being inspired by Craven's multiple levels code, I've written my own code on it and it worked fine.
The logic is, if player sprite is off screen(for example screen width is equal to 640 and player's x value is greater than 640, than you are in the next level). Than, I decided to add menu to my game(?) and menu works fine as well.
The problem is, now my character can't get to second level. It seems it's x value is unable to pass 640, and I don't know why.
Since I am not able to use Stackoverflow editor good enough to share Python code(you know, intended), I will be using Pastebin to share my code.
http://pastebin.ubuntu.com/10392389/
Stackoverflow doesn't let me post the image files I've used, but they are classic pictures I got from the internet.
Any help is appreciated, thanks.
if current_level_num == 1 and player.rect.x > SCREEN_WIDTH:
current_level_num = 2
current_level = levels[current_level_num]
player.rect.x = 0
if current_level_num == 2 and player.rect.x < 1:
current_level_num = 1
current_level = levels[current_level_num]
player.rect.x = SCREEN_WIDTH
The character moves to the 2nd level, then immediately moves back to the 1st. The player should be moved farther to the right than the level 2 boundary.

I'm Programming a Roguelike in Python and I need a Specific Monster Following Script

I'm using libtcod and python to make a roguelike; the tutorial I'm following the monsters only follow you if you're in their field of view. Obviously this is insufficient; as it means you can turn a corner and they don't follow you around the corner.
I tried something like this;
class BasicMonster:
def take_turn(self, seen):
self.seen = False
monster = self.owner
if lib.map_is_in_fov(fov_map, monster.x, monster.y):
self.seen == True
if self.seen == True:
self.move_towards(player.x, player.y)
To no avail. It raises
TypeError: take_turn() takes exactly 2 arguments (1 given)
Not sure how to implement this.
I'm calling I'm calling take_turn under
if game_state == 'playing' and player_action != 'didnt-take-turn':
for object in objects:
if object.ai:
object.ai.take_turn()
A simple solution is to keep track of the position of the player the last time the monster saw the player.
When the player moves out of view, simply move towards that last position.
class BasicMonster:
def __init__(self):
self.last_player_pos = None
def take_turn(self):
monster = self.owner
if libtcod.map_is_in_fov(fov_map, monster.x, monster.y):
# store position we last saw the player
self.last_player_pos = player.x, player.y
#move towards player if far away
# ... rest of code here ...
else:
if self.last_player_pos:
monster.move_towards(*self.last_player_pos)
This is of course very simple (and exploitable), but you'll get the idea. Further steps could be creating some sort of time-out and/or tracking the player by sound/scent or anything not related to FOV.
Scent-tracking is a very effective way to control monster movement; I've used a variation in my C++ roguelike project that sets a 'scent trail', which makes it possible to 'confuse' a monster by backtracking or walking over your own scent multiple times.
I only have experience with C++, but if you search for 'libtcod C++ tutorial', it should bring the page with the scent routine up in the first few results. It should be easy enough to get the general idea.
Good luck!

Categories

Resources