mohon bantuannya kepada agan2 sekalian
ane sekarang lagi buat game battleship untuk 2 player. Interfacenya uda jadi sih, tapi untuk 1 player doang. Ini source codenya
Spoiler for "SOURCE CODE":
import pygame, sys
from pygame.locals import *
from random import randint
print "Welcome to Battleship"
totalpetak = input("Masukkan jumlah petak (5x5-9x9) : ")
jumlahkapal = 0
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
BLUE = ( 0, 0, 255)
gridwidth = 40
gridheight = 40
margin = 5
grid1 = []
for row in range(0,totalpetak):
# Add an empty array that will hold each cell
# in this row
grid1.append([])
for column in range(0,totalpetak):
grid1[row].append(0) # Append a cell
#Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while done == False:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
elif event.type == pygame.MOUSEBUTTONDOWN:
# User clicks the mouse. Get the position
pos1 = pygame.mouse.get_pos()
# Change the x/y screen coordinates to grid coordinates
column = pos1[0] // (gridwidth + margin)
row = pos1[1] // (gridheight + margin)
# Set that location to zero
grid1[row][column] = 1
print("Click ", pos, "Grid coordinates: ", row, column)
# Set the screen background
screen.fill(BLACK)
# Draw the grid
for row in range(0,totalpetak):
for column in range(0,totalpetak):
color = BLUE
if grid1[row][column] == 1:
color = RED
pygame.draw.rect(screen,
color,
[(margin+gridwidth)*column+margin,
(margin+gridheight)*row+margin,
gridwidth,
gridheight])
# Limit to 60 frames per second
clock.tick(60)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
yang jadi pertanyaan ane, gimana caranya biar interfacenya bisa jadi buat 2 player, sama untuk menaruh kapal di petaknya itu bagaimana caranya?
Diubah oleh callmederyan 01-05-2015 10:59
0
955
Kutip
4
Balasan
Guest
Tulis komentar menarik atau mention replykgpt untuk ngobrol seru
Urutan
Terbaru
Terlama
Guest
Tulis komentar menarik atau mention replykgpt untuk ngobrol seru