A 4x4 Field in Pygame - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: A 4x4 Field in Pygame (/thread-20447.html) |
A 4x4 Field in Pygame - Piethon - Aug-11-2019 Hello everybody, I want to make a game in Pygame, where you have a 4x4 squared field and in there are two "cards" (same size as one field) and when you press an arrow key, the cards move in this direction if possible (it shouldn't be possible, when there is no space left in one line). So my problem is, that I don't know, how to create that field, and making it look, if it is possible to move the card or not. I've already tried making a field like this: field = [1,2,3,4, 5,6,7,8, 9,10,11,12, 13,14,15,16]But now I don't know, how to tell python, that all numbers in the [], should represent a square, and that there can be movable card in them. Thanks for your help. Piethon RE: A 4x4 Field in Pygame - metulburr - Aug-11-2019 x = left and right y up and down A (x,y) coordinate system would work. The first number (x) not able to pass 3 (0-3) (which is the fourth element), same with the next number (y). RE: A 4x4 Field in Pygame - SheeppOSU - Aug-12-2019 You can make a dictionary for the spaces. You can type out the dictionary or do this mapDict = {} for x in range(1, 17): #repeat 16 times mapDict.update({x : None}) #None means it isn't occupiedThis function will check if the card can be moved def CheckIfPossible(direction, space): #direction is the direction they are trying to move the card in. space is the space the card is in coordDict = {'x1' : 'left', 'x4' : 'right', 'y1' : 'up', 'y4' : 'down'} #Directions the card cannot go if at a specific coord y = #Get y coord as in 2 if it were in the 2nd row x = #Get x coord for example 1 if it were in the first column if x == 1 and coordDict['x1'] == direction.lower(): return False elif x == 4 and coordDict['x4'] == direction.lower(): return False elif y == 1 and coordDict['y1'] == direction.lower(): return False elif y == 4 and coordDict['y4'] == direction.lower(): return False return TrueHope this helps! RE: A 4x4 Field in Pygame - Piethon - Aug-13-2019 (Aug-12-2019, 08:15 PM)SheeppOSU Wrote: You can make a dictionary for the spaces. You can type out the dictionary or do this Your code looks good, and I tried it. Every time I run the code, I'm getting an error on this line. Exactly I'm getting an error on the #2nd row y = #Get y coord as in 2 if it were in the 2nd row x = #Get x coord for example 1 if it were in the first columnI am not sure, what I shall write, before both Hashtags. I wrote x = 2 and y = 1 and every time I run the code, Python crashes. Eh...yeah. Python crashes. When I'm running other Pygame-Projects, it's not crashing. I ran your code a few times, and the first few times, Python just crashed. I tried it again and then it was not crashing anymore, but just nothing happened (I didn't got an error message). Thanks for the help, but still not working. Edit: I just imported pygame. So above the code, I just have the lines: import pygame and pygame.init()(But I'm not sure, if I really need the pygame.init()). RE: A 4x4 Field in Pygame - Windspar - Aug-13-2019 Also could just do this. def can_move(x, y): if 0 <= x < 4 and 0 <= y < 4: return True return False # in event loop if event.key == pygame.K_LEFT: if can_move(x - 1, y): x -= 1 or # in event loop if event.key == pygame.K_LEFT: if 0 <= x - 1 < 4: x -= 1 RE: A 4x4 Field in Pygame - Piethon - Aug-14-2019 (Aug-13-2019, 10:38 AM)Windspar Wrote: Also could just do this. Thank you. I'll try it, but I still have got this problem, I've mentioned above.: every time I run the code, Python crashes. Eh...yeah. Python crashes. When I'm running other Pygame-Projects, it's not crashing. I ran your code a few times, and the first few times, Python just crashed. I tried it again and then it was not crashing anymore, but just nothing happened (I didn't got an error message). Thanks for the help, but still not working. Edit: I just imported pygame. So above the code, I just have the lines: import pygame and pygame.init()(But I'm not sure, if I really need the pygame.init()). If somebody helps me with this, I'll be able, trying your code. RE: A 4x4 Field in Pygame - Windspar - Aug-14-2019 You definitely need pygame.init(). It initializes all pygame modules. Make sure you install all sdl one libraries. You need mixer, ttf. Make sure you didn't name a file pygame.py. This will be imported instead of pygame library. for some info. print(pygame.init()) Show how many modules were successful and failed. print(pygame.get_error()) See if and sdl errors happen. print(pygame.__file__) To see where it importing from. RE: A 4x4 Field in Pygame - Piethon - Aug-18-2019 (Aug-14-2019, 12:24 PM)Windspar Wrote: You need mixer, ttf. I tried importing mixer. The first thing, I've tried is pip3 install mixer. Said, I should use the --user option. So I tried this: python3 -m pip install -U mixer -—user Now it said this: Usage: /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -m pip install [options] <requirement specifier> [package-index-options] ... /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -m pip install [options] -r <requirements file> [package-index-options] ... /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -m pip install [options] [-e] <vcs project url> ... /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -m pip install [options] [-e] <local project path> ... /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -m pip install [options] <archive url/path> ... no such option: -— Hm...what can I do about that? RE: A 4x4 Field in Pygame - Windspar - Aug-18-2019 You can't install sdl_mixer or sdl_ttf by pip. They are c libraries. You have to download them. If using linux. Then use package manager. RE: A 4x4 Field in Pygame - metulburr - Aug-18-2019 I would give this a read if you are missing dependencies. https://python-forum.io/Thread-PyGame-Install-and-Troubleshooting?pid=34043#pid34043 |