Do you still remember the Windows games we played in those years? Speaking of the games that come with Windows, I believe many friends born in the 80s and 90s are familiar with them. In the early days when games were barren, games such as "Solitaire" and "Minesweeper" helped us bring a lot of joy to our microcomputer classes.
Happy time. But among these games, there is one game that not many people seem to understand, or even know how to play. this game
It's Hearts.
Recently, after the editor carefully studied it on a whim, I found that this game is indeed more enjoyable to play than other games.
Some. Because the gameplay of this game can be simply summed up in two words: "cheating" , and it's the kind of game that's obviously cheating.
1. Open the game: double-click hongxindazhan.py and the main program will run!
2. Start the game: Type in the player's name, create a new ID, and click Confirm to enter the game.
3. Game flow:
1) Change cards: Select three cards in your hand and click the button above to complete the exchange; 2) Play cards: When it is the player's turn to play cards, select the cards in your hand and click to play them. (See the appendix for specific card playing rules) Note: When this card cannot be played at this time, the prompt bar below will display the reason why it cannot be played.
4. Systematically divided into stages: After a round of the game, the score table will pop up, showing the points and total points of each round for the player and the three computers, and
The player's current ranking. (See the appendix for specific rules for determining victory or defeat)
5. Menu bar:
1) Game Bar: New Game (F2): Click to start a new game. Score (F4): Click to display the score table. Background music: Click to switch the music switch. Option (F5): Click to modify the computer playing speed. Exit: Click to exit the game. 2) Help bar: Rule introduction (F1): Click to display brief rules and winning conditions. Quote: Click to pop up a famous quote.
6. Background music: Replacement: You can use your own wav format music to rename it to 'm1.wav' to replace the file with the same name in the program directory.
1. Before playing the game, you need to decide on the dealer. On the computer, the banker sits south. 2. After getting a hand of cards (13 cards in total), the dealer must first select three cards and pass them to other opponents. In the first round, the cards are passed to the player on the left; in the second round, the cards are passed to the player on the right; in the third round, the cards are passed to the player sitting opposite; in the fourth round, no cards are passed, and so on. After receiving the pass from the dealer, you also need to pass back three cards to the dealer at will. On the computer, if you want to select a card, just click on the corresponding card. To deselect a card, click again. 3. The player who catches the 2 of clubs must play the 2 of clubs first, which is the first attack. 4. Then play the cards in a clockwise direction. Each player in turn must draw cards of the same suit. If there are no cards of the same suit as the dealt card, any card can be played. The only exception is that a Heart or Queen of Spades (commonly known as a "Pig") cannot be played on the first round. Note: The highest card among the cards of the same suit played will win this round, and the player who wins the card will play the card first in the next round. Only after the hearts have been played before, can the hearts be drawn out (unless there are only cards of one suit in the hand, hearts). 5. At the end of each round of the game, each red heart is worth 1 point, and the "Queen of Spades (Pig)" is worth 13 points. The game will continue until someone scores 100 points or more or the dealer exits the game. If all hearts and the "Queen of Spades" are won in a round (called a "catch"), the "catch" player gets zero points and the remaining players each get 26 points. The lower the score the better for this game.
▲Environment installation The running environment used in this article: Python3.7, Pycharm Community Edition 2020, tkinter module, part
The built-in module can be imported directly without installation. (If you need to install software, activate codes or encounter problems, you can send me a private message
ha! ) Module installation: pip install -i
https://pypi.douban.com/simple/ + module name
The most original green background. (Only the background is shown, the rest of the pictures are not shown) The reference is the second picture at the top
Only the main program source code is shown. Each line of code is commented so it can be shown directly! See the entire source code at the end of the article!
from Tkinter import * from tkFont import * import winsound from tkMessageBox import * from inner import * from dialog import * #Game main interface creation classGameFrame:def__init__(self,game,bgimg):#game, Game class object; bgimg, background image self .master = game.root #Create a canvas and draw the background self.c = Canvas(self.master,width = 1024,height = 640) self.c.create_image(514,322,image=bgimg) self.c.pack() # Create a status bar self.status = Label(self.master,text="Welcome to the Heart War!", bd=1,relief=SUNKEN,anchor=W) self.status.pack(fill = X) #Open the start dialog box startdialog = StartDialog(self.master,"Hearts") ifstartdialog.isCancel:game.cancel() else:self.gamemodetext = ['Pass to the left', 'Pass to the right', 'Cross-change'] self.name = [startdialog.name,'West','North','East'] self.handXY = [[346.5,490,1,0],[20,135,0,1], [586.5,20,-1,0 ],[913,375,0,-1]] self.nameXY = [[-20,130,SE],[0,-20,SW],[91+20,0,NW],[91,130+20,NE]] self.middleXY = [[466.5,330],[411,255],[466.5,180],[522,255]] self.img = 53* [''] fori inrange(52): self.img[i] = PhotoImage( file = 'card\%s.pgm'% (i)) self.img[52] = PhotoImage(file = 'back.pgm') self.scorelist = [] self.cards = [] self.gamemode = 0self .speed = 100self.wait = self.speed self.isChanging = False self.iswait = False #Draw the name fori inrange(4): self.c.create_text(self.handXY[i][0]+self.nameXY[i ][0], self.handXY[i][1]+self.nameXY[i][1], fill = 'white', text = self.name[i], anchor = self.nameXY[i][2 ], font = Font(size=15,weight="bold")) #Create a hand self.l = 52* [''] fori inrange(52): self.l[i] = Label(self.master, image=self.img[52],bd = -1) fori inrange(13): self.l[i]['text'] = str(i) self.l[i].bind("<Button-1 >",self.cardEvent) #Create the central card self.ml = 4* [''] fori inrange(4): self.ml[i] = Label(self.master,image=self.img[52],bd = -1) self.b = Button(self.master,width=15,command=self.buttonEvent) #A round of game starts self.oneGameStart() defoneGameStart(self): #Create Onegame object to obtain card information self.onegame = OneGame() #Get the player's hand and display the hand = self.onegame.getPlayerHand(0) fori inrange(13): self.l[i]['image'] = self.img[hand[i] .id] fori inrange(4): forj inrange(13): self.moveCard(i,j,0) #Enter the card changing stage when the game mode is 0, 1, 2 ifself.gamemode != 3: self.changeHands( ) else: self.onegame.changeCards([],3) self.isChanging = False self.leftCards = 13#Enter the card playing phase, the computer in front of the player plays cards self.playpreCards() defchangeHands(self): #Change card phase Corresponding initialization self.select = []#Selected cards self.isChanging = True self.isOK = False s = [1,3,2] self.status['text'] = 'Please select three cards to pass' +self.name[s[self.gamemode]]+'. '#The prompt button displays self.b['text'] = self.gamemodetext[self.gamemode] self.b.place(x = 460,y = 400) self.b['state'] = DISABLED defcardEvent(self, event): #Event of card #Get the position of the card i = int(event.widget['text']) ifnotself.isChanging: #Card event in the card playing stage #No card will be played while waiting ifself.iswait:return#Cannot be played The card is not played ifnotself.onegame.available(i): self.status['text'] = self.onegame.errorString returnself.onegame.playCard(i) #The played card is displayed in the center event.widget.place_forget() self .ml[self.turn]['image'] = event.widget['image'] self.ml[self.turn].place(x = self.middleXY[0][0],y = self.middleXY[ 0][1]) self.turn += 1self.leftCards -= 1#The computer behind the player plays cards self.iswait = True self.playlaterCards() self.status['text'] = 'Waiting...'self .wait += 500+ 5* self.speed #After waiting for a period of time, the central card is cleared, and the computer in front of the player plays the card self.master.after(self.wait,self.playpreCards) self.wait = self.speed else: #Card events in the card changing stage, bounced down, dropped bounced ifnotself.isOK:ifi inself.select:self.select.remove(i) self.moveCard(0,i,0) self.b['state '] = DISABLED else:iflen(self.select) < 3: self.select.append(i) self.moveCard(0,i,1) iflen(self.select) == 3: self.b['state' ] = NORMAL defbuttonEvent(self): #Prompt button event ifnotself.isOK: #Get the cards changed by the computer before changing the cards for exchange self.select = self.onegame.changeCards(self.select,self.gamemode) hand = self.onegame .getPlayerHand(0) fori inrange(13): self.l[i]['image'] = self.img[hand[i].id] self.moveCard(0,i,0) fori inself.select:self .moveCard(0,i,1) self.status['text'] = ' Please press "OK" to accept the passed card. 'self.b['text'] = 'OK' self.isOK = True else: #Confirm after changing the cards, enter the card playing stage, the computer in front of the player plays the cards fori inself.select:self.moveCard(0,i ,0) self.b.place_forget() self.isChanging = False self.leftCards = 13self.playpreCards() defplaypreCards(self): ifself.leftCards == 0: #After the cards are played, the scores will be aggregated and the score dialog box will be displayed fori inrange (4): hand = self.onegame.p[i].scoreHand forj inrange(len(hand)): self.l[i*13+j]['image'] = self.img[hand[j]. id] self.moveCard(i,j,0) score = self.onegame.getScore() self.scorelist.append(score) iflen(self.scorelist) != 1: fori inrange(4): self.scorelist[- 1][i] += self.scorelist[-2][i] self.status['text'] = 'Score'scoredialog = self.showScoreDialog() #Initialize after the score is confirmed and start a new round of the game ifscoredialog.isover :self.scorelist = [] self.gamemode = 0else:self.gamemode = (self.gamemode + 1) % 4fori inrange(52): self.l[i].place_forget() self.l[i]['image '] = self.img[52] fori inrange(4): self.ml[i].place_forget() self.oneGameStart() else: #Get the player's previous computer card and display it fori inrange(4): self .ml[i].place_forget() self.turn = 0preCards = self.onegame.preCard iflen(preCards) == 0: self.end() else:fori inrange(len(preCards)): p,j = preCards[ i][0],preCards[i][1] self.cards.append([self.turn,p,j]) self.master.after(self.wait,self.showMiddleCard) ifp == 3: self. master.after(self.wait,self.end) self.wait = self.speed else:self.wait += self.speed self.turn += 1defplaylaterCards(self): #Get the player’s computer cards and display them laterCards = self.onegame.laterCard fori inrange(len(laterCards)): p,j = laterCards[i][0],laterCards[i][1] self.cards.append([self.turn,p,j] ) self.master.after(self.wait,self.showMiddleCard) self.wait += self.speed self.turn += 1defmoveCard(self,i,j,state):#i, player; j, which card ;state, bounce or put down # card movement self.l[i*13+j].place_forget() x0= self.handXY[i][0] + self.handXY[i][2]*j*20y0 = self.handXY[i][1] + self.handXY[i][3]*j*20self.l[i*13+j].place(x = x0,y = y0- state * 20) defnewGame( self): #New game self.scorelist = [] self.gamemode = 0fori inrange(52): self.l[i].place_forget() self.l[i]['image'] = self.img[52] fori inrange(4): self.ml[i].place_forget() self.oneGameStart() defshowScoreDialog(self): #Display score dialog scoredialog = ScoreDialog(self.master,self.scorelist,self.name) returnscoredialog defshowMiddleCard( self): #Central card display i = self.cards[0][0] p = self.cards[0][1] j = self.cards[0][2] del self.cards[0] self.l [p*13+j].place_forget() hand = self.onegame.getPlayerHand(p) self.ml[i]['image'] = self.img[hand[j].id] self.ml[i] .place(x = self.middleXY[p][0], y = self.middleXY[p][1]) defend(self): #Switching from when the computer finishes playing cards to when the player plays cards self.iswait = False self. status['text'] = 'Please play a card. '#Create a game window and create a game menu item classGame:def__init__(self): #Create a root window and set self.root = Tk() self.root.title("Hearts") self.root.geometry('+ 150+10') self.root.resizable(False, False) #Play background music self.s = winsound.PlaySound('m1.wav', winsound.SND_ASYNC+winsound.SND_LOOP) #Create menu m = Menu(self. root) self.root['menu'] = m gamemenu = Menu(m) helpmenu = Menu(m) m.add_cascade(label = 'Game',menu = gamemenu) m.add_cascade(label = 'Help',menu = helpmenu) gamemenu.add_command(label="New Game F2",command = self.gameEvent1) gamemenu.add_separator() gamemenu.add_command(label="Score... F4",command = self.gameEvent2) gamemenu.add_command(label ="Option... F5", command = self.gameEvent4) self.v = IntVar() self.v.set(1) gamemenu.add_checkbutton(label="Background music", variable = self.v, command = self .gameEvent3) gamemenu.add_separator() gamemenu.add_command(label="Exit",command = self.cancel) helpmenu.add_command(label="Introduction to rules... F1",command = self.helpEvent1) helpmenu.add_command(label ="Quotation...",command = self.helpEvent2) self.root.bind('<F2>',self.gameEvent1) self.root.bind('<F4>',self.gameEvent2) self.root. bind('<F5>',self.gameEvent4) self.root.bind('<F1>',self.helpEvent1) self.root.protocol("WM_DELETE_WINDOW",self.cancel) #Import background image bgimg = PhotoImage( file = 'bg.gif') #Create the main interface self.frame = GameFrame(self,bgimg) #Main loop self.root.mainloop() defgameEvent1(self,event=None): #Game menu item "New Game" flag = askokcancel('New Game', 'Are you sure you want to abandon the current game and start a new one? ') ifflag:self.frame.newGame() defgameEvent2(self,event=None): #Game menu item "Score" self.frame.showScoreDialog() defgameEvent3(self): #Game menu item "Background music" ifself.v .get() == 0: winsound.PlaySound(self.s,winsound.SND_PURGE) else:self.s = winsound.PlaySound('m1.wav', winsound.SND_ASYNC+winsound.SND_LOOP) defgameEvent4(self,event= None): #Game menu item "option" optionDialog = OptionDialog(self.root,self.frame.speed / 100- 1) ifnotoptionDialog.isCancel:self.frame.speed = 100+ optionDialog.v.get() * 100defhelpEvent1( self, event=None): #Help menu item" rule introduction "HelpDialog(self.root) defhelpEvent2(self): #Help menu item" famous saying "SayDialog(self.root) defcancel(self): #Turn off the music and exit the game winsound.PlaySound(self.s,winsound.SND_PURGE) self.root.destroy() defmain(): Game() if__name__== '__main__': main()
Technology is changing with each passing day. Desktop "big bricks" have turned into portable tablets in an instant. People who secretly played these built-in games back then
Either becoming a new person in the workplace or becoming a parent, they have all started their own new stages, and these games have also completed their missions!
Which game was your favorite when you were a teenager? By the way, I have written about many games before. If you need the source code, remember to get it from me!