enemy randomly can shoot

This commit is contained in:
2025-07-27 20:10:59 +02:00
parent 7fca9e7885
commit 428e7e83f0
2 changed files with 31 additions and 11 deletions

20
main.py
View File

@@ -2,6 +2,7 @@ import pygame
import sys import sys
from constants import * from constants import *
from player import * from player import *
import copy
def print_board(board): def print_board(board):
for i in range(BOARD_SIZE): for i in range(BOARD_SIZE):
@@ -32,18 +33,19 @@ def main():
while good_inp == False: while good_inp == False:
inp = input() inp = input()
good_inp, player_board, ship_pos = check_valid_input(inp) good_inp, player_board, ship_pos = check_valid_input(inp)
enemy_board = player_board.copy() enemy_board = copy.deepcopy(player_board)
player = Player(player_board, ship_pos) player = Player(player_board, ship_pos.copy())
enemy = Player(enemy_board, ship_pos) enemy = Player(enemy_board, ship_pos.copy())
print_board(player_board) print_board(player_board)
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
time = pygame.time.Clock() time = pygame.time.Clock()
dt = 0 turn_counter = 0
first_iteration = True first_iteration = True
is_enemy_turn = False
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
@@ -54,7 +56,12 @@ def main():
pygame.Surface.fill(screen, (255,255,255)) pygame.Surface.fill(screen, (255,255,255))
if first_iteration == False: if first_iteration == False:
player.update(enemy) if is_enemy_turn == False:
player.update(enemy)
is_enemy_turn = True
else:
enemy.random_shoot(player)
is_enemy_turn = False
else: else:
first_iteration = False first_iteration = False
@@ -62,7 +69,8 @@ def main():
pygame.display.flip() #refresh screen pygame.display.flip() #refresh screen
dt = time.tick(60) / 1000 #converted to ms turn_counter += 1
print("turn: ", turn_counter)
def check_valid_input(inp): def check_valid_input(inp):

View File

@@ -1,6 +1,7 @@
import pygame import pygame
from pygame import Rect from pygame import Rect
from ship import * from ship import *
import random
class Player(): class Player():
def __init__(self, board, ships): def __init__(self, board, ships):
@@ -23,6 +24,7 @@ class Player():
if enemy.board[ord(position[0])-97][int(position[1])] == 0b00: if enemy.board[ord(position[0])-97][int(position[1])] == 0b00:
print("No ship at this position") print("No ship at this position")
self.shot_positions[ord(position[0])-97][int(position[1])] = 0b01
else: else:
enemy.get_hit(position) enemy.get_hit(position)
self.shot_positions[ord(position[0])-97][int(position[1])] = 0b11 self.shot_positions[ord(position[0])-97][int(position[1])] = 0b11
@@ -31,19 +33,22 @@ class Player():
if len(position) != 2 and position != None: if len(position) != 2 and position != None:
return False return False
if ord(position[0]) >= 97 and ord(position[0]) <= 107 and int(position[1]) <= 9 and int(position[1]) >= 0:
return True
if self.shot_positions[ord(position[0])-97][int(position[1])] == 0b11: if self.shot_positions[ord(position[0])-97][int(position[1])] == 0b11:
print("You already shot at this position!") print("You already shot at this position!")
return False return False
if ord(position[0]) >= 97 and ord(position[0]) <= 107 and int(position[1]) <= 9 and int(position[1]) >= 0:
return True
return False return False
def update(self, enemy): def update(self, enemy):
inp = input("Please input the position you want to fire.") inp = input("Please input the position you want to fire.")
while self.__check_valid_input(inp) == False: check = self.__check_valid_input(inp)
inp = input("Please input the position you want to fire.")
while check == False:
inp = input("Please input the position you want to fire. ")
check = self.__check_valid_input(inp)
self.shoot(inp, enemy) self.shoot(inp, enemy)
@@ -84,3 +89,10 @@ class Player():
start_pos_y += height - 5 start_pos_y += height - 5
start_pos_x = 125 start_pos_x = 125
def random_shoot(self, enemy):
position = chr(random.randint(97, 107))
position += str(random.randint(0,9))
print("rand pos: ", position)
self.shoot(position, enemy)