I can't get this task right regarding a simple ' 2D game' as a matrix

In summary: Player is attacked by an enemy. Player symbol: 5, weak enemies (-50 health): 1, strong enemies (-100 health): 2. Possible player movement: LEFT, RIGHT, UP, DOWN (move one field on map). player_health = 0 #Player cannot displace beyond the boundaries of the playing field.
  • #1
simphys
322
45
Homework Statement
Hello guys, if you don't mind, I will attach the whole exercise in the pdf(it is the 1st difficult task RPG_game).

This is the description of the exact task(task 3 of difficult task 1) WITH CODE HIGHLIGHTED(function starts from line 36, the other stuff is for context):

[3 Points] Write a function player_move that takes the same three parameters as the previous function, plus a fourth parameter direction, which represents the direction of the move (“UP”, “DOWN”, “LEFT”, “RIGHT”). The function determines whether the intended move of the player is possible, and what consequences it has for the player’s health. If the move is possible, the function returns a list that includes (1) the player’s health after the move, (2) the player’s position, and (3) the updated state of the world. If the move is impossible, the player does not move and nothing changes. Should the player die, they are no longer present in the matrix, since the position where they died will be occupied by the enemy that defeated them. In that case the returned position is the position where the player died.
Relevant Equations
So It seems to me that i have done everything correctly, comparing it with the test underneath the file as well, yet it doesn't seem to work out.
Can someone help/steer me in the right direction please?
And perhaps, if possible, give some advice on how I have written the code. It's the first time I use 2D Lists in matrix-form, just something to keep in mind :'D
I have also put some notes on what is to be done in the problematic function
Note also that this is not homework, but am just preparing for an exam.

Thanks in advance!

forgot to provide the code here, so here it is:
Python:
# RPG subsystem: check whether the next player move on a 5x5 tileset is possible, and if player
# is attacked by an enemy. Player symbol: 5, weak enemies (-50 health): 1, strong enemies
# (-100 health): 2. Possible player movement: LEFT, RIGHT, UP, DOWN (move one field on map).
#player cannot displace beyond the boundaries of the playing field.

def translate_move(direction):
    coordinate = [0,0] #starting point.
    #+ve y-axis is DONW!
    if direction == 'UP':
        coordinate[0] -= 1
    elif direction == 'DOWN':
        coordinate[0] += 1
    elif direction == 'LEFT':
        coordinate[1] -= 1
    elif direction == 'RIGHT':
        coordinate[1] += 1
    return coordinate
 
 

def fight_enemy(health, position, world):
    #players fight with enemy that is at a certain position. player receives remaining health back.
    # position 1 loses 50 hjealth at position 2 loses -100 health.
    #position = [row, col]
    row = position[0]
    col = position [-1]
    position_in_world = world[row][col]
    if position_in_world == 2:
        health -= 100
    elif position_in_world == 1:
        health -= 50
    if health <= 0:
        health = 0
    return health
 
def player_move(health, position, world, direction):
    '''
        Function displays whether a desirable move of the player is possible and what the consequences are.
    '''
    #Updated positions.
    updated_position = [position[0] + translate_move(direction)[0], position[1] + translate_move(direction)[1]]
    row = updated_position[0]
    col = updated_position[1]
    old_row = position[0]
    old_col = position[1]
    ###if move not possible, return original
    if row > 4 or row < 0 or col > 4 or col < 0:
        return [health, position, world]
    #Updating world from old to new position (changing 5)
    if old_row == 0 or old_row == 2 or old_row == 3:
        world[old_row][old_col] = 0
    elif old_row == 1:
        world[old_row][old_col] = 1
    elif old_row == 4:
        world[old_row][old_col] = 2
    ###if player gets killed --> remove player, change with enemy + return updated position
    health = fight_enemy(health, updated_position, world)
    if health == 0:
        return [health, updated_position, world]
    #if move possible + not killed --> return [updated health, updated position, updated world]
    else:
        world[row][col] = 5
        return [health, updated_position, world]
 
 

### DO NOT TOUCH THE CODE BELOW THIS LINE ###

def print_world(world):
    for row in world:
        print(row)
    print()

def test():
    score = 0
            
          
    game_world = [
                  #0 1 2 3 4
                  [0,0,0,0,0],#0
                  [1,1,1,1,1],#1
                  [0,0,0,0,0],#2
                  [0,0,0,0,0],#3
                  [2,2,2,2,2]]#4
    player_health = 100
    player_location = [0,0]

    # print_world(game_world)
    game_world[player_location[0]][player_location[1]] = 5
    print_world(game_world)
    print("Initial player health:", player_health)
    print()
  # TEST TASK 3 #
    t3_1 = player_move(100, [0,0], [[5,0,0,0,0], [1,1,1,1,1], [0,0,0,0,0], [0,0,0,0,0], [2,2,2,2,2]], "DOWN")
    print( "World state after move down:" )
    print_world( t3_1[2] )
 
    t3_2 = player_move(50, [1,0], [[0,0,0,0,0], [5,1,1,1,1], [0,0,0,0,0], [0,0,0,0,0], [2,2,2,2,2]], "LEFT")
    print( "World state after move left:" )
    print_world( t3_2[2] )
 
    t3_3 = player_move(50, [1,0], [[0,0,0,0,0], [5,1,1,1,1], [0,0,0,0,0], [0,0,0,0,0], [2,2,2,2,2]], "RIGHT" )
    print( "World state after move right:" )
    print_world( t3_3[2] )
 
    if t3_1[0] == 50 and t3_1[1] == [1,0] and t3_1[2] == [[0,0,0,0,0], [5,1,1,1,1], [0,0,0,0,0], [0,0,0,0,0], [2,2,2,2,2]] and t3_2[0] == 50 and t3_2[1] == [1,0] and t3_2[2] == [[0,0,0,0,0], [5,1,1,1,1], [0,0,0,0,0], [0,0,0,0,0], [2,2,2,2,2]] and t3_3[0] == 0 and t3_3[1] == [1,1] and t3_3[2] == [[0,0,0,0,0], [0,1,1,1,1], [0,0,0,0,0], [0,0,0,0,0], [2,2,2,2,2]]:
        print("# TEST 3 SUCCESSFUL #")
        score += 3
    else:
        print("# TEST 3 FAILED #")
    print()
 
    print()
    print("Tentative overall score: ", score, "/ 6")
    print()
    print("This is an indicative value only. You also need to follow the instructions of the exam,")
    print("and code in a flexible way (i.e., your code still needs to work when tested with different values).")
 
# run the program.
if __name__ == "__main__":
    test()
 

Attachments

  • lab10_assignment_en.pdf
    844.6 KB · Views: 123
Last edited:
Physics news on Phys.org
  • #2
simphys said:
Homework Statement:: Hello guys, if you don't mind, I will attach the whole exercise in the pdf(it is the 1st difficult task RPG_game) and my code as well.
No code.
 
  • Like
Likes simphys
  • #3
pbuk said:
No code.
my apologies, didn't send through perhaps! Uploading Right now, thanks for notifying me :)
 
  • #4
pbuk said:
No code.
I uploaded the whole file, which is apparently not possible
 
  • #5
simphys said:
I uploaded the whole file, which is apparently not possible
No, you can't upload source files here. Post your source using the </> icon if it is a sensible length, if it is not a sensible length then edit it down to just the bit you have difficulty with.
 
  • #6
pbuk said:
No, you can't upload source files here. Post your source using the </> icon if it is a sensible length, if it is not a sensible length then edit it down to just the bit you have difficulty with.
hey @pbuk is it possible to leave the code like this for context and simply highlight the problematic code? Thanks in advance
 
  • #7
simphys said:
hey @pbuk is it possible to leave the code like this for context and simply highlight the problematic code? Thanks in advance
It's now fine, although yes it would be even better if you highlighted the problem lines.
 
  • Like
Likes simphys
  • #8
On line 46 you set new_position_in_world = world[row][col].
  • How do you know that row and col are valid indices?
  • Do you use new_position_in_world anyway (or old_position_in_world)?
I don't understand what lines 51-57 are trying to do: what is the significance of old_row here?
 
  • Like
Likes simphys
  • #9
pbuk said:
On line 46 you set new_position_in_world = world[row][col].
  • How do you know that row and col are valid indices?
  • Do you use new_position_in_world anyway (or old_position_in_world)?
I don't understand what lines 51-57 are trying to do: what is the significance of old_row here?
Thanks, so yes
  1. for new_position_in_world and old_position_in_world, I forgot to remove these.
  2. so that is what I am checking initially, if these are: col<0(or >4) and row<0(or >4) then I just return everything how it was given as input otherwise I continue further with the other code
 
  • #10
pbuk said:
On line 46 you set new_position_in_world = world[row][col].
  • How do you know that row and col are valid indices?
  • Do you use new_position_in_world anyway (or old_position_in_world)?
I don't understand what lines 51-57 are trying to do: what is the significance of old_row here?
as for row_old change the position in the matrix from 5(position where the player is located back to its' original number 0, 1 or 2 and then at the end only for when the player is still living I update his position in the 'world'
 
  • #11
@pbuk It turns out that my code is COMPLETELY fine...! altough messy.
Someone told me that was a solution of the mock exam. I went through the code and then I realized that all that struggle was for nothing... When he updates the world(/matrix) he does not update the position back to 0,1 or 2 but just changes the old position with a zero.

Whilst I on the other hand was changing it back to its' original state..
Anyway Thanks for having a look at it!
 
  • Like
Likes berkeman

Related to I can't get this task right regarding a simple ' 2D game' as a matrix

1. Why am I having trouble creating a 2D game using a matrix?

Creating a 2D game using a matrix can be challenging because it involves understanding and manipulating multiple layers of data. It requires a strong understanding of programming concepts such as arrays, loops, and conditional statements.

2. How can I improve my skills in creating a 2D game using a matrix?

To improve your skills in creating a 2D game using a matrix, it is important to practice and familiarize yourself with the necessary programming concepts. You can also seek out online tutorials and resources to learn more about creating games with matrices.

3. What are some common mistakes to avoid when working with a matrix for a 2D game?

Some common mistakes to avoid when working with a matrix for a 2D game include not properly initializing the matrix, using incorrect indices to access elements, and not considering edge cases and boundary conditions. It is important to thoroughly test your code and debug any errors that may arise.

4. Can I use a matrix for any type of 2D game?

Yes, a matrix can be used for any type of 2D game. However, it may be more suitable for certain types of games, such as grid-based games or games that involve complex data structures.

5. Are there any alternative methods for creating a 2D game besides using a matrix?

Yes, there are alternative methods for creating a 2D game besides using a matrix. Some other options include using game engines or libraries that provide built-in functions for creating games, or using object-oriented programming to create game objects and interactions. It ultimately depends on the specific needs and goals of your game.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
769
  • Programming and Computer Science
Replies
7
Views
580
  • Special and General Relativity
Replies
8
Views
985
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
9K
  • Special and General Relativity
Replies
11
Views
337
  • Programming and Computer Science
Replies
33
Views
6K
  • Programming and Computer Science
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top