Commit fd3f2006 by source_reader

lab1 start

parents
.idea
venv
__pycache__
*.pyc
# EW432 Battleship Game
Complete the lab assignments by adding code inside the markers (example shown below):
# --------- BEGIN YOUR CODE ----------
your code goes here!
# --------- END YOUR CODE ----------
Do **not** add any code outside of these markers, it may prevent you from merging subsequent labs.
Lab 1: Introduction to PyGame
-----------------------------
By the end of this lab you should have two empty battleship boards displayed side by side.
Code is required in the following locations:
| File Name | Function:Line | Description |
|-----------------|-------------------|------------------|
|``main.py`` |``main:36`` | draw board titles|
|``game_board.py``| ``initialize:50`` | draw game board |
File mode changed
No preview for this file type
# Game color palette
screen_bkgd = [200, 200, 200]
board_bkgd = [63, 72, 204]
foreground = [0, 0, 0]
header = [255, 215, 0]
import pygame
import string
from typing import Tuple
import colors
import utilities
NBLOCKS = 11
class GameBoard(pygame.sprite.Sprite):
"""
The Game Board is a 10x10 grid of spaces which may contain sprite images
Columns are indexed by number [0-9] and rows are indexed by letter [A-J]
0|1|2|3|4|5|6|7|8|9|
A|_|_|_|_|_|_|_|_|_|_|
B|_|_|_|_|_|_|_|_|_|_|
C|_|_|_|_|_|_|_|_|_|_|
D|_|_|_|_|_|_|_|_|_|_|
E|_|_|_|_|_|_|_|_|_|_|
F|_|_|_|_|_|_|_|_|_|_|
G|_|_|_|_|_|_|_|_|_|_|
H|_|_|_|_|_|_|_|_|_|_|
I|_|_|_|_|_|_|_|_|_|_|
J|_|_|_|_|_|_|_|_|_|_|
"""
def __init__(self, dimension, surface):
super().__init__()
self.image = pygame.Surface(dimension)
self.image.convert()
self.rect = self.image.get_rect()
self.width = self.image.get_width()
self.height = self.image.get_height()
# helper variables for spacing sprite images
self.x_step = self.width // NBLOCKS
self.y_step = self.height // NBLOCKS
# location where the board is drawn (the actual window)
self.surface = surface
def initialize(self):
# Draw board background use color [board_bkgd]
self.image.fill(colors.board_bkgd)
# --------- BEGIN YOUR CODE ----------
# Draw row and column header backgrounds
# Headers should be 1 block wide/tall and use color [header]
# Draw grid lines use color [foreground]
# Draw row labels [A-J] centered in each header block
# use color [foreground] and font [
# Draw column labels [0-9] centered in each header block
# use color [foreground]
# Draw border around the board use color [foreground]
# --------- END YOUR CODE ------------
def draw(self):
self.surface.blit(self.image, (self.rect.x, self.rect.y))
import pygame
from pygame.locals import *
import sys
import colors
import utilities
from game_board import GameBoard
BLOCK_SIZE = 30
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')
# size of the game board figure based on BLOCK SIZE pixels
board_dimension = (BLOCK_SIZE * NBLOCKS, BLOCK_SIZE * NBLOCKS)
# "my" game board displays my ships with their guesses
my_board: GameBoard = GameBoard(board_dimension, screen)
my_board.rect.top = TOP_MARGIN
my_board.rect.left = PADDING
# "their" game board displays my guesses with their *sunk* ships
their_board: GameBoard = GameBoard(board_dimension, screen)
their_board.rect.top = TOP_MARGIN
their_board.rect.left = PADDING * 2 + my_board.rect.width
# --------- BEGIN YOUR CODE ----------
# add titles above the game boards
# draw 'YOU' centered above my_board
# draw 'THEM' centered above their_board
# --------- END YOUR CODE ----------
my_board.initialize()
my_board.draw()
their_board.initialize()
their_board.draw()
pygame.display.update()
# wait until the user closes the game
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
import pygame
FONT_PATH = 'assets/FutilePro.ttf'
def create_text(text, size, color):
font = pygame.font.Font(FONT_PATH, size)
image = font.render(text, True, color)
return image
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