write a code in pyqt5 that creates a gui for the game battleship in python

Here is a sample code that creates a GUI for the Battleship game using PyQt5 in Python:

main.py
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QWidget,
                             QGridLayout, QLabel, QLineEdit)
import random

class Battleship(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Battleship Game")
        self.init_ui()

    def init_ui(self):
        grid_layout = QGridLayout()
        grid_layout.setSpacing(10)

        self.turn_label = QLabel("Your Turn")
        self.player1_board = self.create_board("Player 1", grid_layout)
        self.player2_board = self.create_board("Player 2", grid_layout)

        grid_layout.addWidget(self.turn_label, 0, 0, 1, 3)
        self.setLayout(grid_layout)
        self.show()

    def create_board(self, name, grid_layout):
        board_label = QLabel(name + ":")
        grid_layout.addWidget(board_label, grid_layout.rowCount(), 0)
        board = Board()
        grid_layout.addWidget(board, grid_layout.rowCount(), 0, 1, 3)
        return board

class Board(QWidget):
    def __init__(self):
        super().__init__()
        self.cells = []
        self.selected_cell = None
        self.init_ui()

    def init_ui(self):
        grid_layout = QGridLayout()
        grid_layout.setSpacing(10)

        for row in range(10):
            row_cells = []
            for col in range(10):
                cell = Cell(row, col)
                row_cells.append(cell)
                grid_layout.addWidget(cell, row, col)
            self.cells.append(row_cells)

        self.setLayout(grid_layout)

    def select_cell(self, cell):
        if self.selected_cell:
            self.selected_cell.deselect()
        self.selected_cell = cell
        self.selected_cell.select()

    def deselect_cell(self):
        self.selected_cell = None

class Cell(QPushButton):
    def __init__(self, row, col):
        super().__init__()
        self.row = row
        self.col = col
        self.is_ship = False
        self.clicked.connect(self.click_handler)
        self.setStyleSheet("background-color: #e6e6e6; width: 30px; height: 30px;")

    def click_handler(self):
        self.parent().parent().select_cell(self)

    def select(self):
        self.setStyleSheet("background-color: #b3b3b3; width: 30px; height: 30px;")

    def deselect(self):
        self.setStyleSheet("background-color: #e6e6e6; width: 30px; height: 30px;")

def main():
    app = QApplication([])
    battleship = Battleship()
    app.exec_()

if __name__ == '__main__':
    main()

2516 chars
87 lines

This code defines three classes:

  • Battleship: This class represents the main window of the game. It contains player 1 and player 2 board, and displays whose turn it is.

  • Board: This class represents the game board of each player, which consists of 10x10 cells. The game board is a grid layout of 10x10 Cell objects.

  • Cell: This class represents a single cell in the game board. When a cell is clicked, it becomes selected, and its background color changes to gray. When another cell is clicked, the previously selected cell becomes deselected.

Note that this sample code does not include the game logic or ship placement. However, it provides a layout for the game board and allows the player to select cells by clicking them.

related categories

gistlibby LogSnag