Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

##The code below starting at def color_at needs to be edited or corrected. It should return an output when the user connects all 4 of

##The code below starting at def color_at needs to be edited or corrected. It should return an output when the user connects all 4 of either gren or red but I am unsure of what I am doing wrong. Could use some help(PYTHON)

from tkinter import *

window = Tk()

# -------------------------------------------------------------------------------------- # Get background image. port_image = PhotoImage(file='port_hole_14-80px.png') background_image = PhotoImage(file='square-01.png') green_ball_image = PhotoImage(file='glass_ball-77px-04.png') red_ball_image=PhotoImage(file='glass_ball-77px-01.png')

on_move_image=green_ball_image off_move_image=red_ball_image

image_width = background_image.width() # x coord of right box wall image_height = background_image.height() # y coord of box bottom

nrows = 6 ncols = 7 turn = 0 busy = False num_column=[] for column in range(ncols): num_column.append([]) print(num_column) value_count = [] for val in range(ncols): value_count.append([])

# -------------------------------------------------------------------------------------- # Create canvas, background and sprite objects.

canvas = Canvas(window, width = 7*image_width, height = 6*image_height, bg = 'black') canvas.create_image(2, 2, image = background_image, anchor = 'nw')

for rows in range(nrows+1): #increments +1 to fill the rows for col in range(ncols+1): #increments +1 to fill the columns last_square=canvas.create_image(2+image_width*rows, 2+image_height*col, image=background_image, anchor='nw') #multiply width by rows and height by columns

for rows in range(nrows+1): for col in range(ncols+1): canvas.create_image(2+image_width*rows, 2+image_height*col, image=port_image,anchor='nw')

canvas.pack() #divide x coordinate by image width def on_mouse_press(event): global turn global busy # Makes it so the balls are not able to drop until the ball sequence is finished if busy:return busy = True location= event.x column=(location//image_width) print('Mouse Pressed', location//image_width) if(turn==0): greenball=canvas.create_image(2+image_width * column, 0, image=on_move_image , anchor = 'nw', tag='GREEN')#Creates green ball canvas.lift(greenball,last_square) value_count[column].append(0) num_column[column].append(greenball) print(num_column) pixels= 480 - len(num_column[column]*image_height) for i in range(pixels): canvas.move(greenball,0,1) canvas.update() canvas.after(1) turn =1 elif turn == 1: redball=canvas.create_image(2+image_width * column, 0, image=off_move_image , anchor = 'nw', tag='RED')#Creates red ball canvas.lift(redball,last_square) value_count[column].append(1) num_column[column].append(redball) print(num_column) pixels= 480 - len(num_column[column]*image_height) for i in range(pixels): canvas.move(redball,0,1) canvas.update() canvas.after(1) turn = 0 busy = False return

def pop_out(event): #Creates the pop out method to drop the balls column = event.x //image_width global turn if value_count[column][0]==0 and turn %2==0: length = len(num_column[column]) for i in range(length): tags = num_column[column][i] for x in range(image_width): canvas.move(tags, 0,1) canvas.update() canvas.after(1) turn+=1 num_column[column].pop(0) value_count[column].pop(0) if turn % 2 == 1 and value_count[column][0]==1: length = len(num_column[column]) for i in range(length): tags = num_column[column][i] for x in range(image_width): canvas.move(tags,0,1) canvas.update() canvas.after(1) turn +=1 num_column[column].pop(0) value_count[column].pop(0) return

def color_at(row,col): row = nrows-row-1 if col= ncols:return None if row =len(value_count[col]): return None return canvas.gettags(value_count[col][row])[0]

def color_check(color): if color_at(row,col)==color\ and color_at(row,col+1)== color\ and color_at(row, col+2)==color\ and color_at(row, col+3)==color: print("You have won!") return row,col if color_at(row, col)==color \ and color_at(row+1, col)==color\ and color_at(row+2, col)==color\ and color_at(row+3, col)==color: print("You have won!") return row, col

def win_check(color): dr,dc =1,0 dr,dc=0,1 dr,dc=1,1 dr,dc=1,-1 for dr,dc in ((1,0), (1,1) (0,1), (1,-1)): if color_at(row,col)==color\ and color_at(row+dr, col+dc)==color\ and color_at(row+2*dr, col+2*dc)==color\ and color_at(row+3*dr, col+3*dc)==color: return row,col,dr,dc

window.bind('', on_mouse_press) window.bind('', pop_out) window.title('CONNECT 4') # do this before event thread is busy in loop

window.mainloop() image text in transcribed

File Edit Shell Debug Options Window Help Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>>> ============ RESTART: C:\Users\Lenworth\Downloads\connect4-00.py =========== [[], [], [], [], [], [], [1] Mouse Pressed 3 [[], [], [], [114], [], [], [1] Mouse Pressed 2 [I], [], [115], [114], [], [], [ll Mouse Pressed 3 [[], [], [115], [114, 116], [], [], []] ============ RESTART: C:\Users\Lenworth\Downloads\connect4-00.py ========= [[], [], [], [], [], [], []] Mouse Pressed 0 [[114], [], [], [], [], [], []] Mouse Pressed 1 [[114], [115], [], [], [], [], []] Mouse Pressed 0 [[114, 116], [115], [], [], [], [], []] Mouse Pressed 1 [[114, 116], [115, 117], [], [], [], [], [1] Tq114, 116, 118], [115, 117], [], [], [], [], [1] Mouse Pressed 1 [[114, 116, 118], [115, 117, 119], [], [], [], [], []] Mouse Pressed 0 [[114, 116, 118, 120], [115, 117, 119], [], [], [], [], [1] Mouse Pressed 1 [[114, 116, 118, 120], [115, 117, 119, 121], [], [], [], [], []] Mouse Pressed 4 [[114, 116, 118, 120], [115, 117, 119, 121], [], [], [122], [], []] Mouse Pressed 1 [[114, 116, 118, 120], [115, 117, 119, 121, 123], [], [], [122], [], [1]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Statistical And Scientific Database Management International Working Conference Ssdbm Rome Italy June 21 23 1988 Proceedings Lncs 339

Authors: Maurizio Rafanelli ,John C. Klensin ,Per Svensson

1st Edition

354050575X, 978-3540505754

More Books

Students also viewed these Databases questions