Commit 64f02887 by source_reader

lab2 start

parent fd3f2006
......@@ -21,3 +21,16 @@ Code is required in the following locations:
|``main.py`` |``main:36`` | draw board titles|
|``game_board.py``| ``initialize:50`` | draw game board |
Lab 2: Working with Objects
---------------------------
By the end of this lab you should have ships displayed on your battleship board.
When you close the game a message box with "Bye!" should display for one second.
Code is required in the following locations:
| File Name | Function:Line | Description |
|-------------------|-----------------------|-----------------------|
|``main.py`` |``display_message:73`` | show a message box |
|``utilities.py`` |``draw_ships:18`` | draw ships on a board |
|``player_state.py``| ``place_ships:43`` | randomly place ships |
|``sprites.py`` | ``--global--:28`` | create sprite objects |
\ No newline at end of file
......@@ -66,3 +66,13 @@ class GameBoard(pygame.sprite.Sprite):
def draw(self):
self.surface.blit(self.image, (self.rect.x, self.rect.y))
def add_sprite(self, sprite: pygame.Surface, loc: Tuple[int, int]):
"""
Place a sprite on the game board in location (row,col)
"""
row = loc[0]
col = loc[1]
x = self.x_step * (col + 1)
y = self.y_step * (row + 1)
self.image.blit(sprite, (x, y))
......@@ -2,6 +2,8 @@ import pygame
from pygame.locals import *
import sys
import players
import sprites
import colors
import utilities
from game_board import GameBoard
......@@ -11,12 +13,14 @@ NBLOCKS = 11
TOP_MARGIN = 30
PADDING = 10
def main():
pygame.init()
screen: pygame.Surface = pygame.display.set_mode(((BLOCK_SIZE * NBLOCKS) * 2 + PADDING * 3,
BLOCK_SIZE * NBLOCKS + TOP_MARGIN + PADDING))
screen.fill(colors.screen_bkgd)
pygame.display.set_caption('USNA Battleship')
sprites.initialize()
# size of the game board figure based on BLOCK SIZE pixels
board_dimension = (BLOCK_SIZE * NBLOCKS, BLOCK_SIZE * NBLOCKS)
......@@ -39,7 +43,9 @@ def main():
# --------- END YOUR CODE ----------
me = players.Human()
my_board.initialize()
utilities.draw_ships(my_board, me.state.all_ships())
my_board.draw()
their_board.initialize()
......@@ -51,9 +57,34 @@ def main():
while True:
for event in pygame.event.get():
if event.type == QUIT:
_display_message(screen, "Bye!")
pygame.display.update()
pygame.time.wait(1000)
pygame.quit()
sys.exit()
def _display_message(screen: pygame.Surface, msg: str):
"""
Display [msg] in the message box sprite in the center of the screen
"""
# make a copy of the msg_box sprite because we need to edit it
box = sprites.msg_box.copy()
# --------- BEGIN YOUR CODE ----------
# create a text object with size 42 font of [msg]
# blit the text onto the box surface
# blit the box onto the center of the screen
# remove this once you have implemented the drawing code
print(msg)
# --------- END YOUR CODE ----------
if __name__ == "__main__":
main()
from .human import Human
from .player import Player
import string
from typing import Tuple
from .player import Player
from .player_state import PlayerState
class Human(Player):
""" A human player"""
def __init__(self):
self.priority = 2
self.state = PlayerState()
# add ships to the board randomly
self.state.place_ships()
from typing import Tuple
from .player_state import PlayerState
class Player:
def get_state(self) -> PlayerState:
pass
def send_state(self, state: PlayerState, reveal=False):
pass
def get_guess(self) -> Tuple[int, int]:
pass
def send_guess(self, guess: Tuple[int, int]):
pass
def close(self):
pass
from random import randint
from typing import List, Optional, Tuple
from ship import Ship
SHIP_TYPES = [2, 3, 3, 4, 5]
class PlayerState:
def __init__(self):
""" Create a valid ship layout
This function populates
_my_ships and _board_matrix
Ship Type | Length
-----------|-------
Carrier | 5
Battleship | 4
Cruiser | 3
Submarine | 3
Destroyer | 2
* the ship type is just FYI, it is not used in the game *
"""
self._hits: List[Tuple[int, int]] = []
self._hit = False
self._misses: List[Tuple[int, int]] = []
self._ships: List[Ship] = []
self._hash = ''
def place_ships(self, debug=False):
# the board matrix is a 10x10 structure with
# pointers to ship objects. Initialize to all
# None values- no ships are on the board
board_matrix: List[List[Optional[Ship]]] = [[None] * 10 for _ in range(10)]
# place the ships on the board
for ship_length in SHIP_TYPES:
# --------- BEGIN YOUR CODE ----------
pass # <-- remove this!
# --------- END YOUR CODE ----------
"""
Print the board as text, useful for debugging
"""
if debug:
print("=" * 10)
for row in board_matrix:
for entry in row:
if entry is None:
print("_", end="")
else:
print(entry.length, end="")
print("")
print("=" * 10)
def all_ships(self):
return self._ships
class Ship:
def __init__(self, length: int, row: int,
col: int, is_vertical: bool):
""" Ships are specified by their length, the (row,col)
coordinate of the bow, and whether they are vertical or
horizontal. Two examples are illustrated on the board below:
X = Ship(5,2,4,False)
Y = Ship(3,3,1,True)
0|1|2|3|4|5|6|7|8|9|
A|_|_|_|_|_|_|_|_|_|_|
B|_|_|_|_|_|_|_|_|_|_|
C|_|_|_|_|X|X|X|X|X|_|
D|_|Y|_|_|_|_|_|_|_|_|
E|_|Y|_|_|_|_|_|_|_|_|
F|_|Y|_|_|_|_|_|_|_|_|
G|_|_|_|_|_|_|_|_|_|_|
H|_|_|_|_|_|_|_|_|_|_|
I|_|_|_|_|_|_|_|_|_|_|
J|_|_|_|_|_|_|_|_|_|_|
"""
self.length = length
self.row = row
self.col = col
self.is_vertical = is_vertical
import pygame
"""
'cut out' sprites from the sprite sheet
the first sprite is provided as an example
"""
# sprites are 30x30 tiles separated by 1 pixel margins
SPRITE_WIDTH = 30
SPRITE_HEIGHT = 30
SPRITE_MARGIN = 1
# an array of all the game sprites
sprites = []
# load the sprite sheet
sprite_sheet = pygame.image.load('assets/sprite_sheet.png')
# ---msg_box (250x122)---
# create an empty surface for the sprite
msg_box = pygame.Surface((250, 122))
# see blit documentation at
# https://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit
msg_box.blit(sprite_sheet, (0, 0), pygame.Rect(0, 93, 250, 122))
# add the sprite to the array
sprites.append(msg_box)
# --------- BEGIN YOUR CODE ----------
# ---ship_top (30x30)---
# ---ship_left (30x30)---
# ---ship_bottom (30x30)---
# ---ship_right (30x30)---
# ---ship_horizontal (30x30)---
# ---ship_vertical (30x30)---
# ---hit (30x30)---
# ---miss (30x30)---
# ---ship_sunk (30x30)---
# --------- END YOUR CODE ------------
# set alpha on all sprites to enable transparency
def initialize():
for sprite in sprites:
sprite.set_colorkey((255, 0, 255))
sprite.convert_alpha()
from typing import List
import pygame
import sprites
import ship
import game_board
FONT_PATH = 'assets/FutilePro.ttf'
......@@ -7,3 +12,12 @@ def create_text(text, size, color):
font = pygame.font.Font(FONT_PATH, size)
image = font.render(text, True, color)
return image
def draw_ships(board: game_board.GameBoard, ships: List[ship.Ship]):
# --------- BEGIN YOUR CODE ----------
# draw the ships by adding the appropriate sprites to the board
pass # <-- remove this
# --------- END YOUR CODE ----------
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment