How to Program Connect 4 in Python! (part 4) - Game Animation & Additional Features

preview_player
Показать описание
In this video we finish up our connect 4 graphics and add a couple additional features. If you've enjoyed this series, please consider subscribing to my channel :). Also AI video is out! Link below

Link to video on programming the AI:

Link to video on how a connect 4 AI works:

Source code:

Previous Video:

Link to Pygame Documentation:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

PYTHON TUTORIAL SERIES:

-------------------------
Follow me on social media!

-------------------------

Learn data skills with hands-on exercises & tutorials at Datacamp!

*I use affiliate links on the products that I recommend. I may earn a purchase commission or a referral bonus from the usage of these links.
Рекомендации по теме
Комментарии
Автор

In part 5 of this series, you should take a deep breath and revisit all the code you have written and rewrite it into really beautiful code. This means:

* eliminate duplicate code (event handling for player 1 and 2 is essentially the same)
* define player_color(piece): return (BLACK, RED, YELLOW)[piece]
* merge the circle drawing code into a single line, making use of player_color
* extract the event handling into a function
* define a main function
* turn = (turn + 1) % 2 (instead of the two lines)
* fix the indentation of pygame.display.update() below the MOUSEMOVE

That video would be beneficial to every beginner because it demonstrates that keeping your code clean and short makes it maintainable.

RolandIllig
Автор

Must say Keith, I really enjoyed this series of tutorials. I am an ageing chap in my mid 50’s just learning to code... and loving it too. Your tutorials here have helped enormously. In fact, you helped so much that you may be pleased to read that I managed to rewrite the code into a connect4 game for the iPad using the excellent Pythonista app. Still a few minor issues to sort out and improve upon, but it’s thanks to you that I managed to accomplish this. Thank you 👍

ReflectingMe
Автор

this series is great! I wanted something easy to try out graphics programming in python, and your video was no doubt helpful.



also an easier to resolve the upside down situation at 8:56 would be to simply add a 'return statement' to the print_board(board) function (this function was used to resolve upside board problem when in previous videos) so i used the same method as did it like this:


def print_board(board):
print(np.flip(board, 0))
return np.flip(board, 0)


and using this function inside the draw_board whenever it is called.



RAyLV
Автор

Thank you so much for this. This really helped me a lot for my college project. Your content is amazing.

satvikp.s
Автор

Thanks for this tutorial... Very helpful...

dasarocomusic
Автор

Thanks for your videos on this, i was wanting to code a Tic Tac Toe but lacked the GUI knoledge to do so, I learned the ideias from your video, coded alongside you, then i went back to the start and did it again, but this time doing my tic tac toe following the same steps and adapting my code to fit my game, so i had to think hard about coordinates, how to manage my array.
Keep up with the good work, i'm tackling your AI follow up on connect 4!
Cheers

aragaofernando
Автор

This was awesome!!! Pretty much saved my college project!!

antunesjorge
Автор

Hey, thanks for the video.
It was really simple and easy. Would love if you can make more.

muhammadusman
Автор

hello Kieth
hope you are well
just want to say thank you from the bottom of my heart
you was a big help to me and my friends
I am a computer science student from Saudi and i got much help in learning Python from this game and also alot of AI from the AI version
thank you for the second time Kieth

a_ayed_alghamdi
Автор

​ @Keith Galli Suggestion: Save the ending code for each video part as separate files, or branches.
This lets the audience be certain they did not miss typing anything in, or compare the differences between say parts 2 and 3.

thisisreallyme
Автор

Yes dude please make a video on making a connect 4 AI. Would really like to see the logic behind making an expert level AI. Love your videos!

DynestiGTI
Автор

Thanks you so much ! It's was awesome.

algyngom
Автор

9:00 Instead of modifying the whole "draw_board" function, I just fixed it adding 1 single statement before the for loop: board = np.flip(board, 0)

So it become:

def draw_board(board):
board = np.flip(board, 0)
for c in range(col_count):
for r in range(row_count):
pygame.draw.rect(screen, blue, (c * square_size, r * square_size + square_size, square_size, square_size))
if board[r][c] == 0:
pygame.draw.circle(screen, black, (int(c * square_size + square_size / 2), int(r * square_size + square_size + square_size / 2)), radius)
elif board[r][c] == 1:
pygame.draw.circle(screen, red, (int(c * square_size + square_size / 2), int(r * square_size + square_size + square_size / 2)), radius)
else:
pygame.draw.circle(screen, yellow, (int(c * square_size + square_size / 2), int(r * square_size + square_size + square_size / 2)), radius)
pygame.display.update()

ptortello
Автор

Hey, it's the first time I'm watching your videos, and I watched all 5 of them (the connect 4 series). I have to give it to you for your enthusiasm and all that for making these videos, but as a python developer myself I should tell you that your code is even messier than those of indians. You have to keep it clean and adhere to the pep8 rules. Refactor your code, multiple times if necessary. And optimize it. Good luck.

lionpersia
Автор

hey Keith, thanks for this great introductory video series!
You asked for suggestions for new videos. Do you have experience in creating games in python using the Unreal Engine? That would be extremely interesting to me
Also of course, creating an A.I. for 2D games is very cool! I'll check out your video on this topic next

tobyiy
Автор

hi, just finished your 4 tutorials and came back to add my vote for the AI version! i learned a bunch from this and am really looking forward to the AI version hint hint.

serenityrahn
Автор

You should make a video with all the
"pip install commands" you need for for the games..
If you dont have a video abou it.

raspberrypi
Автор

Thanks for tutorial. How Do I make the game Repeat itself after game over? I guess it has something to do with the reset method but whats the code exactly and where to put it?

gokusgames
Автор

Could you code a menu into connect four which allows you to either play with another person or play against an AI with varying difficulties?

rzentertainment
Автор

Hi, can you make tutorial for AI using minmax algorithm with alpha beta pruning, or using Monte Carlo Tree Search algorithm.
This tutorial is very helpfull and nicely done :)

markokljakovicgaspic
join shbcf.ru