Jul-23-2023, 01:49 AM
I need some help. Whenever I try to run the code, it says that the showcase() and line 696 with the pot=input() is a 'str' object is not callable error. Can someone help me debug? Thanks!
# Make a website to showcase all my projects.
def player():
# This is a 2p tic tac toe game
from enum import Enum
class GridSquare():
state = ""
pos = 0
def __init__(self, x):
self.pos = x
self.state = "-1"
def draw_space(self):
if self.state == "1":
return 'X'
if self.state == "0":
return 'O'
return str(self.pos)[0]
class TicTacToe():
cols = 3
rows = 3
total_turns = 0
winner = -1
class game_state(Enum):
OVER = 0
RUNNING = 1
current_state = game_state.OVER
board = None
def setup(self):
self.board = [[None] * self.cols for _ in range(self.rows)]
position = 1
r = 0
while r < self.rows:
c = 0
while c < self.cols:
self.board[r][c] = GridSquare(position)
position += 1
c += 1
r += 1
TicTacToe.current_state = TicTacToe.game_state.RUNNING
self.play_game()
def play_game(self):
while TicTacToe.current_state == TicTacToe.game_state.RUNNING:
self.display_board()
self.make_move()
if TicTacToe.current_state == TicTacToe.game_state.OVER:
self.display_game_over()
def display_board(self):
print(
"\n " + str(self.board[0][0].draw_space()) + " | " + str(self.board[0][1].draw_space()) + " | " + str(
self.board[0][2].draw_space()))
print(" ___|____|___ ")
print(
"\n " + str(self.board[1][0].draw_space()) + " | " + str(self.board[1][1].draw_space()) + " | " + str(
self.board[1][2].draw_space()))
print(" ___|____|___ ")
print(
"\n " + str(self.board[2][0].draw_space()) + " | " + str(self.board[2][1].draw_space()) + " | " + str(
self.board[2][2].draw_space()))
def make_move(self):
print("Player " + str(self.get_player()) + " choose a pos: ")
spot = input()
i = 0
while i < self.cols:
j = 0
while j < self.rows:
if str(self.board[i][j].state) == "-1" and str(self.board[i][j].pos) == spot:
self.board[i][j].state = str(self.total_turns % 2)
self.total_turns += 1
self.check_win(i, j, str(self.board[i][j].state))
j += 1
i += 1
def display_game_over(self):
self.display_board()
print('Game Over!')
if self.winner == '1':
print('X Wins!')
if self.winner == '0':
print('O Wins!')
if self.total_turns == 9 and not (self.winner == "1") and not self.winner == "0":
print('Tie Game!')
def get_player(self):
if self.total_turns % 2 == 0:
return 'O'
return 'X'
def check_win(self, x, y, turn):
col_win = 0
row_win = 0
diag1_win = 0
diag2_win = 0
i = 0
while i < 3:
if self.board[x][i].state == turn:
col_win += 1
if self.board[i][y].state == turn:
row_win += 1
if self.board[i][i].state == turn:
diag1_win += 1
if self.board[i][2 - i].state == turn:
diag2_win += 1
i += 1
if col_win == 3 or row_win == 3 or diag1_win == 3 or diag2_win == 3:
self.winner = turn
if self.winner != -1:
TicTacToe.current_state = TicTacToe.game_state.OVER
if self.total_turns == 9:
TicTacToe.current_state = TicTacToe.game_state.OVER
# Check for a win in every direction.
gameplay = TicTacToe()
gameplay.setup()
def ai():
# player vs. computer tic tac toe game!!!!
from enum import Enum
import random
class GridSquare():
state = ""
pos = 0
def __init__(self, x):
self.pos = x
self.state = "-1"
def draw_space(self):
if self.state == "1":
return 'X'
if self.state == "0":
return 'O'
return str(self.pos)[0]
class TicTacToe():
cols = 3
rows = 3
total_turns = 0
winner = -1
class game_state(Enum):
OVER = 0
RUNNING = 1
current_state = game_state.OVER
board = None
def setup(self):
self.board = [[None] * self.cols for _ in range(self.rows)]
position = 1
r = 0
while r < self.rows:
c = 0
while c < self.cols:
self.board[r][c] = GridSquare(position)
position += 1
c += 1
r += 1
TicTacToe.current_state = TicTacToe.game_state.RUNNING
self.play_game()
def play_game(self):
while TicTacToe.current_state == TicTacToe.game_state.RUNNING:
self.display_board()
self.make_move()
if TicTacToe.current_state == TicTacToe.game_state.OVER:
self.display_game_over()
def display_board(self):
print(
"\n " + str(self.board[0][0].draw_space()) + " | " + str(self.board[0][1].draw_space()) + " | " + str(
self.board[0][2].draw_space()))
print(" ___|____|___ ")
print(
"\n " + str(self.board[1][0].draw_space()) + " | " + str(self.board[1][1].draw_space()) + " | " + str(
self.board[1][2].draw_space()))
print(" ___|____|___ ")
print(
"\n " + str(self.board[2][0].draw_space()) + " | " + str(self.board[2][1].draw_space()) + " | " + str(
self.board[2][2].draw_space()))
def make_move(self):
if str(self.get_player()) == 'X':
ai = random.randint(1, 9)
print("Player X chose " + str(ai))
i = 0
while i < self.cols:
j = 0
while j < self.rows:
if str(self.board[i][j].state) == "-1" and str(self.board[i][j].pos) == str(ai):
self.board[i][j].state = str(self.total_turns % 2)
self.total_turns += 1
self.check_win(i, j, str(self.board[i][j].state))
self.display_board()
j += 1
i += 1
if str(self.get_player()) == "O":
print("Player " + str(self.get_player()) + " choose a pos: ")
spot = input()
i = 0
while i < self.cols:
j = 0
while j < self.rows:
if str(self.board[i][j].state) == "-1" and str(self.board[i][j].pos) == spot:
self.board[i][j].state = str(self.total_turns % 2)
self.total_turns += 1
self.check_win(i, j, str(self.board[i][j].state))
j += 1
i += 1
def display_game_over(self):
self.display_board()
print('Game Over!')
if self.winner == '1':
print('X Wins!')
if self.winner == '0':
print('O Wins!')
if self.total_turns == 9 and not (self.winner == "1") and not self.winner == "0":
print('Tie Game!')
def get_player(self):
if self.total_turns % 2 == 0:
return 'O'
return 'X'
def check_win(self, x, y, turn):
col_win = 0
row_win = 0
diag1_win = 0
diag2_win = 0
i = 0
while i < 3:
if self.board[x][i].state == turn:
col_win += 1
if self.board[i][y].state == turn:
row_win += 1
if self.board[i][i].state == turn:
diag1_win += 1
if self.board[i][2 - i].state == turn:
diag2_win += 1
i += 1
if col_win == 3 or row_win == 3 or diag1_win == 3 or diag2_win == 3:
self.winner = turn
if self.winner != -1:
TicTacToe.current_state = TicTacToe.game_state.OVER
if self.total_turns == 9:
TicTacToe.current_state = TicTacToe.game_state.OVER
# Check for a win in every direction.
gameplay = TicTacToe()
gameplay.setup()
def tictactoe():
print('Welcome to tic tac toe, would you like to play versus another player or play vs ai?(player/ai)')
choice = input()
if choice == 'player':
player()
elif choice == 'ai':
ai()
elif choice == 'quit':
active = False
else:
print('Please enter a valid statement.')
def caesarcypher():
letters = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z']
# make functions for each area
# ideas:
# encrypt:
# take index of each inputted letter
# add key
# print encrypted message
# decrypt:
# take index of each inputted letter
# subtract key from each index
# print decrypted message
def encrypt():
e_str = ""
for i in encrypt_message:
index = letters.index(i)
index = int(index)
index = index + key
index = index % 26
e_str += letters[index]
print(e_str)
def decrypt():
e_str = ""
for i in encrypt_message:
index = letters.index(i)
index = int(index)
index = index - key
index = index % 26
e_str += letters[index]
print(e_str)
choice = input('Welcome to Caesar Cypher! Would you like to encrypt or decrypt?')
active = True
while active:
if choice == 'encrypt':
key = input('Please enter the key!')
key = int(key)
encrypt_message = input('What is the message you want to encrypt in all caps?')
encrypt()
active = False
else:
# this is decrypt!
key = input('Please enter the key!')
key = int(key)
encrypt_message = input('What is the message you want to encrypt in all caps?')
decrypt()
active = False
def guessinggame():
import random
# guess a number game
# needs the following parts:
# tell if too high or too low
# generate a number between 100
# do this in 7 tries
# have a way to quit
print("Welcome to the number guessing game!")
print("Try to guess the number from 1 to 100 in 7 tries! Good luck! Type anything larger than 100 to exit!")
counter = 0
jackpot = random.randint(1, 100)
guessed = False
while not guessed:
number = input("Enter a number from 1-100!")
number = int(number)
if counter > 6:
print('You have run out of tries! Good luck next time!')
guessed = True
if number == jackpot:
print('Congrats! You win!')
guessed = True
elif number < jackpot:
print('Too low, try again!')
counter += 1
elif number > 100:
print('Thanks for playing, come back soon!')
guessed = True
else:
print('Too high, try again!')
counter += 1
def mazerunner():
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)
class Wall(pygame.sprite.Sprite):
"""This class represents the bar at the bottom that the player controls """
def __init__(self, x, y, width, height, color):
""" Constructor function """
# Call the parent's constructor
super().__init__()
# Make a BLUE wall, of the size specified in the parameters
self.image = pygame.Surface([width, height])
self.image.fill(color)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
class Player(pygame.sprite.Sprite):
""" This class represents the bar at the bottom that the
player controls """
# Set speed vector
change_x = 0
change_y = 0
def __init__(self, x, y):
""" Constructor function """
# Call the parent's constructor
super().__init__()
# Set height, width
self.image = pygame.Surface([15, 15])
self.image.fill(WHITE)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
def changespeed(self, x, y):
""" Change the speed of the player. Called with a keypress. """
self.change_x += x
self.change_y += y
def move(self, walls):
""" Find a new position for the player """
# Move left/right
self.rect.x += self.change_x
# Did this update cause us to hit a wall?
block_hit_list = pygame.sprite.spritecollide(self, walls, False)
for block in block_hit_list:
# If we are moving right, set our right side to the left side of
# the item we hit
if self.change_x > 0:
self.rect.right = block.rect.left
else:
# Otherwise if we are moving left, do the opposite.
self.rect.left = block.rect.right
# Move up/down
self.rect.y += self.change_y
# Check and see if we hit anything
block_hit_list = pygame.sprite.spritecollide(self, walls, False)
for block in block_hit_list:
# Reset our position based on the top/bottom of the object.
if self.change_y > 0:
self.rect.bottom = block.rect.top
else:
self.rect.top = block.rect.bottom
class Room(object):
""" Base class for all rooms. """
# Each room has a list of walls, and of enemy sprites.
wall_list = None
enemy_sprites = None
def __init__(self):
""" Constructor, create our lists. """
self.wall_list = pygame.sprite.Group()
self.enemy_sprites = pygame.sprite.Group()
class Room1(Room):
"""This creates all the walls in room 1"""
def __init__(self):
super().__init__()
# Make the walls. (x_pos, y_pos, width, height)
# This is a list of walls. Each is in the form [x, y, width, height]
walls = [[0, 0, 20, 250, WHITE],
[0, 350, 20, 250, WHITE],
[780, 0, 20, 250, WHITE],
[780, 350, 20, 250, WHITE],
[20, 0, 760, 20, WHITE],
[20, 580, 760, 20, WHITE],
[390, 50, 20, 500, BLUE],
[200, 50, 20, 500, PURPLE],
[295, 50, 20, 500, GREEN],
[485, 50, 20, 500, RED],
[100, 300, 500, 20, PURPLE],
[240, 50, 20, 300, BLUE]
]
# Loop through the list. Create the wall, add it to the list
for item in walls:
wall = Wall(item[0], item[1], item[2], item[3], item[4])
self.wall_list.add(wall)
class Room2(Room):
"""This creates all the walls in room 2"""
def __init__(self):
super().__init__()
walls = [[0, 0, 20, 250, RED],
[0, 350, 20, 250, RED],
[780, 0, 20, 250, RED],
[780, 350, 20, 250, RED],
[20, 0, 760, 20, RED],
[20, 580, 760, 20, RED],
[190, 50, 20, 500, GREEN],
[590, 50, 20, 500, GREEN],
[150, 300, 500, 20, BLUE],
[150, 200, 250, 20, PURPLE],
[500, 200, 250, 20, PURPLE]
]
for item in walls:
wall = Wall(item[0], item[1], item[2], item[3], item[4])
self.wall_list.add(wall)
class Room3(Room):
"""This creates all the walls in room 3"""
def __init__(self):
super().__init__()
walls = [[0, 0, 20, 250, PURPLE],
[0, 350, 20, 250, PURPLE],
[780, 0, 20, 250, PURPLE],
[780, 350, 20, 250, PURPLE],
[20, 0, 760, 20, PURPLE],
[20, 580, 760, 20, PURPLE],
[100, 300, 600, 20, BLUE]
]
for item in walls:
wall = Wall(item[0], item[1], item[2], item[3], item[4])
self.wall_list.add(wall)
for x in range(100, 800, 100):
for y in range(50, 451, 300):
wall = Wall(x, y, 20, 200, RED)
self.wall_list.add(wall)
for x in range(150, 700, 100):
wall = Wall(x, 200, 20, 200, WHITE)
self.wall_list.add(wall)
def main():
""" Main Program """
# Call this function so the Pygame library can initialize itself
pygame.init()
# Create a 800x600 sized screen
screen = pygame.display.set_mode([800, 600])
# Set the title of the window
pygame.display.set_caption('Maze Runner')
# Create the player paddle object
player = Player(50, 50)
movingsprites = pygame.sprite.Group()
movingsprites.add(player)
rooms = []
room = Room1()
rooms.append(room)
room = Room2()
rooms.append(room)
room = Room3()
rooms.append(room)
current_room_no = 0
current_room = rooms[current_room_no]
clock = pygame.time.Clock()
done = False
while not done:
# --- Event Processing ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changespeed(-5, 0)
if event.key == pygame.K_RIGHT:
player.changespeed(5, 0)
if event.key == pygame.K_UP:
player.changespeed(0, -5)
if event.key == pygame.K_DOWN:
player.changespeed(0, 5)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.changespeed(5, 0)
if event.key == pygame.K_RIGHT:
player.changespeed(-5, 0)
if event.key == pygame.K_UP:
player.changespeed(0, 5)
if event.key == pygame.K_DOWN:
player.changespeed(0, -5)
# --- Game Logic ---
player.move(current_room.wall_list)
if player.rect.x < -15:
if current_room_no == 0:
current_room_no = 2
current_room = rooms[current_room_no]
player.rect.x = 790
elif current_room_no == 2:
current_room_no = 1
current_room = rooms[current_room_no]
player.rect.x = 790
else:
current_room_no = 0
current_room = rooms[current_room_no]
player.rect.x = 790
if player.rect.x > 801:
if current_room_no == 0:
current_room_no = 1
current_room = rooms[current_room_no]
player.rect.x = 0
elif current_room_no == 1:
current_room_no = 2
current_room = rooms[current_room_no]
player.rect.x = 0
else:
current_room_no = 0
current_room = rooms[current_room_no]
player.rect.x = 0
# --- Drawing ---
screen.fill(BLACK)
movingsprites.draw(screen)
current_room.wall_list.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
if __name__ == "__main__":
main()
def fortuneteller():
# fortune teller game
import random
active = True
while active:
fortune = random.randint(1, 10)
fortune = int(fortune)
roll = input("\nWelcome to your fortune teller! I know how good your future will be, based on your luck! \n\
If you're lucky, you'll have a lucky outcome. \n\
Tell me to 'roll' when you're ready to roll the dice for your future.")
if roll == 'roll':
print('You have rolled a ' + str(fortune) + '. ')
if fortune < 4:
print('You have really bad luck today.')
elif fortune < 7:
print('You have average luck today.')
else:
print('You have extremely good luck! Might be a good time to bet something. :)')
else:
print('Tell me to roll when you are ready, nothing else!')
def showcase():
print("Choose a project below to play according to it's order number starting from 1.")
print('1. Tic Tac Toe\n2. Caesar Cypher\n3. Guessing Game\n4. Maze Runner\n5. Fortune Teller')
pot = input()
if pot == 1:
tictactoe()
elif pot == 2:
caesarcypher()
elif pot == 3:
guessinggame()
elif pot == 4:
mazerunner()
elif pot == 5:
fortuneteller()
elif pot == 'quit':
active = False
else:
print('Please enter a valid number or enter quit to exit the program.')
active = True
print("Welcome to the hub, please enter the password.\nThe hint is my favorite animal in plural lowercase.")
while active:
input = input('Enter the password here:')
if input == 'cats':
print('Welcome valid user.')
print('Enter quit at any time to exit the program.')
showcase()
else:
print('Incorrect, please try again.')
continue
