'Why is python telling me that a string has no atrribute "x"?
def __init__(box):
box.board = []
with io.StringIO(text) as fp:
for line in fp:
box.board.append(list(line))
def show_board (box):
for row in box.board:
for item in row:
if item == "B":
print(item, end = '', flush = True)
else:
print(item, end = '', flush = True)
def detect_box_point(box):
for y, row in enumerate(box.board):
for x, item in enumerate(row):
if item == "B":
box.x, box.y = x, y
What I'm trying to do is make a pushable box, using the same set of code as the player (yes I know this is probably a stupid way I'm still new to this sort of thing), and for some reason, it keeps telling me that "str has no atrribute x" when it gets to
def move_player_box(player, move, box):
clear()
moved = False
if move == "d":
new_x = player.x + 2
new_y = player.y
**newbox_x = box.x + 2
newbox_y = box.y**
moved = True
Any help with this or any better ways to go about this?
Full code:
text = """This is just text to place the stuff"""
player = "#"
box = "B"
class Game():
def __init__(player):
player.board = []
with io.StringIO(text) as fp:
for line in fp:
player.board.append(list(line))
def show_board (player):
for row in player.board:
for item in row:
if item == "#":
print(item, end = '', flush = True)
else:
print(item, end = '', flush = True)
def detect_start_point(player):
for y, row in enumerate(player.board):
for x, item in enumerate(row):
if item == "#":
player.x, player.y = x, y
def __init__(box):
box.board = []
with io.StringIO(text) as fp:
for line in fp:
box.board.append(list(line))
def show_board (box):
for row in box.board:
for item in row:
if item == "B":
print(item, end = '', flush = True)
else:
print(item, end = '', flush = True)
def detect_box_point(box):
for y, row in enumerate(box.board):
for x, item in enumerate(row):
if item == "B":
box.x, box.y = x, y
def move_player_box(player, move, box):
global i
clear()
moved = False
if move == "d":
new_x = player.x + 2
new_y = player.y
newbox_x = box.x + 2
newbox_y = box.y
moved = True
if move == "a":
new_x = player.x - 2
new_y = player.y
newbox_x = box.x + 2
newbox_y = box.y
moved = True
if move == "w":
new_x = player.x
new_y = player.y - 1
newbox_x = box.x
newbox_y = box.y - 1
moved = True
if move == "s":
new_x = player.x
new_y = player.y + 1
newbox_x = box.x
newbox_y = box.y + 1
moved = True
if moved:
if player.board[new_y][new_x] in (' '):
player.current = player.board[new_y][new_x]
player.board[player.y][player.x] = ' '
player.x = new_x
player.y = new_y
player.board[player.y][player.x] = "#"
if player.board[new_y][new_x] in ('B'):
if box.board[newbox_y][newbox_x] != ('-') and box.board[newbox_y][newbox_x] != ('|'):
player.current = player.board[new_y][new_x]
box.current = box.board[newbox_y][newbox_x]
player.board[player.y][player.x] = ' '
box.board[box.y][box.x] = ' '
box.x = newbox_x
box.y = newbox_y
box.board[box.y][box.x] = "B"
box.current = box.board[newbox_y][newbox_x]
player.x = new_x
player.y = new_y
player.board[player.y][player.x] = "#"
else:
player.current = player.board[player.y][player.x]
box.current = box.board[box.y][box.x]
return
board = Game()
board.detect_start_point()
board.detect_box_point()
while(True):
move = getkey()
board.move_player_box(move, box)
board.show_board()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
