Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A 4x4 Field in Pygame
#1
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
Reply
#2
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).
Recommended Tutorials:
Reply
#3
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 occupied
This 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 True
Hope this helps!
Reply
#4
(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
mapDict = {}

for x in range(1, 17): #repeat 16 times
    mapDict.update({x : None}) #None means it isn't occupied
This 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 True
Hope this helps!

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 column
I 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()).
Reply
#5
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
99 percent of computer problems exists between chair and keyboard.
Reply
#6
(Aug-13-2019, 10:38 AM)Windspar Wrote: 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

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.
Reply
#7
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.
99 percent of computer problems exists between chair and keyboard.
Reply
#8
(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?
Reply
#9
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.
99 percent of computer problems exists between chair and keyboard.
Reply
#10
I would give this a read if you are missing dependencies.
https://python-forum.io/Thread-PyGame-In...3#pid34043
Recommended Tutorials:
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020