- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Nibbles in Tkinter
In this part of the Tkinter tutorial, we create a Nibbles game clone.
Nibbles is an older classic video game. It was first created in late 70s. Later it was brought to PCs. In this game the player controls a snake. The objective is to eat as many apples as possible. Each time the snake eats an apple, its body grows. The snake must avoid the walls and its own body.
Development
The size of each of the joints of a snake is 10 px. The snake is controlled with the cursor keys. Initially, the snake has three joints. The game starts immediately. When the game is finished, we display "Game Over" message in the center of the window.
We use the Canvas
widget to create the game. The objects in the game are images. We use canvas methods to create image items. We use canvas methods to find items on the canvas using tags and to do collision detection.
nibbles.py
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode Tkinter tutorial This is a simple Nibbles game clone. Author: Jan Bodnar Website: zetcode.com Last edited: November 2015 """ import sys import random from PIL import Image, ImageTk from Tkinter import Tk, Frame, Canvas, ALL, NW WIDTH = 300 HEIGHT = 300 DELAY = 100 DOT_SIZE = 10 ALL_DOTS = WIDTH * HEIGHT / (DOT_SIZE * DOT_SIZE) RAND_POS = 27 x = [0] * ALL_DOTS y = [0] * ALL_DOTS class Board(Canvas): def __init__(self, parent): Canvas.__init__(self, width=WIDTH, height=HEIGHT, background="black", highlightthickness=0) self.parent = parent self.initGame() self.pack() def initGame(self): self.left = False self.right = True self.up = False self.down = False self.inGame = True self.dots = 3 self.apple_x = 100 self.apple_y = 190 for i in range(self.dots): x[i] = 50 - i * 10 y[i] = 50 try: self.idot = Image.open("dot.png") self.dot = ImageTk.PhotoImage(self.idot) self.ihead = Image.open("head.png") self.head = ImageTk.PhotoImage(self.ihead) self.iapple = Image.open("apple.png") self.apple = ImageTk.PhotoImage(self.iapple) except IOError, e: print e sys.exit(1) self.focus_get() self.createObjects() self.locateApple() self.bind_all("<Key>", self.onKeyPressed) self.after(DELAY, self.onTimer) def createObjects(self): self.create_image(self.apple_x, self.apple_y, image=self.apple, anchor=NW, tag="apple") self.create_image(50, 50, image=self.head, anchor=NW, tag="head") self.create_image(30, 50, image=self.dot, anchor=NW, tag="dot") self.create_image(40, 50, image=self.dot, anchor=NW, tag="dot") def checkApple(self): apple = self.find_withtag("apple") head = self.find_withtag("head") x1, y1, x2, y2 = self.bbox(head) overlap = self.find_overlapping(x1, y1, x2, y2) for ovr in overlap: if apple[0] == ovr: x, y = self.coords(apple) self.create_image(x, y, image=self.dot, anchor=NW, tag="dot") self.locateApple() def doMove(self): dots = self.find_withtag("dot") head = self.find_withtag("head") items = dots + head z = 0 while z < len(items)-1: c1 = self.coords(items[z]) c2 = self.coords(items[z+1]) self.move(items[z], c2[0]-c1[0], c2[1]-c1[1]) z += 1 if self.left: self.move(head, -DOT_SIZE, 0) if self.right: self.move(head, DOT_SIZE, 0) if self.up: self.move(head, 0, -DOT_SIZE) if self.down: self.move(head, 0, DOT_SIZE) def checkCollisions(self): dots = self.find_withtag("dot") head = self.find_withtag("head") x1, y1, x2, y2 = self.bbox(head) overlap = self.find_overlapping(x1, y1, x2, y2) for dot in dots: for over in overlap: if over == dot: self.inGame = False if x1 < 0: self.inGame = False if x1 > WIDTH - DOT_SIZE: self.inGame = False if y1 < 0: self.inGame = False if y1 > HEIGHT - DOT_SIZE: self.inGame = False def locateApple(self): apple = self.find_withtag("apple") self.delete(apple[0]) r = random.randint(0, RAND_POS) self.apple_x = r * DOT_SIZE r = random.randint(0, RAND_POS) self.apple_y = r * DOT_SIZE self.create_image(self.apple_x, self.apple_y, anchor=NW, image=self.apple, tag="apple") def onKeyPressed(self, e): key = e.keysym if key == "Left" and not self.right: self.left = True self.up = False self.down = False if key == "Right" and not self.left: self.right = True self.up = False self.down = False if key == "Up" and not self.down: self.up = True self.right = False self.left = False if key == "Down" and not self.up: self.down = True self.right = False self.left = False def onTimer(self): if self.inGame: self.checkCollisions() self.checkApple() self.doMove() self.after(DELAY, self.onTimer) else: self.gameOver() def gameOver(self): self.delete(ALL) self.create_text(self.winfo_width()/2, self.winfo_height()/2, text="Game Over", fill="white") class Nibbles(Frame): def __init__(self, parent): Frame.__init__(self, parent) parent.title('Nibbles') self.board = Board(parent) self.pack() def main(): root = Tk() nib = Nibbles(root) root.mainloop() if __name__ == '__main__': main()
First we will define some constants used in our game.
The WIDTH
and HEIGHT
constants determine the size of the Board. The DELAY
constant determines the speed of the game. The DOT_SIZE
is the size of the apple and the dot of the snake. The ALL_DOTS
constant defines the maximum number of possible dots on the Board. The RAND_POS
constant is used to calculate a random position of an apple.
x = [0] * ALL_DOTS y = [0] * ALL_DOTS
These two arrays store the x and y coordinates of all possible joints of a snake.
The initGame()
method initialises variables, loads images, and starts a timeout function.
try: self.idot = Image.open("dot.png") self.dot = ImageTk.PhotoImage(self.idot) self.ihead = Image.open("head.png") self.head = ImageTk.PhotoImage(self.ihead) self.iapple = Image.open("apple.png") self.apple = ImageTk.PhotoImage(self.iapple) except IOError, e: print e sys.exit(1)
In these lines, we load our images. There are three images in the Nibbles game: the head, the dot, and the apple.
self.createObjects() self.locateApple()
The createObjects()
method creates items on the canvas. The locateApple()
puts an apple randomly on the canvas.
self.bind_all("<Key>", self.onKeyPressed)
We bind the keyboard events to the onKeyPressed()
method. The game is controlled with keyboard cursor keys.
def createObjects(self): self.create_image(self.apple_x, self.apple_y, image=self.apple, anchor=NW, tag="apple") self.create_image(50, 50, image=self.head, anchor=NW, tag="head") self.create_image(30, 50, image=self.dot, anchor=NW, tag="dot") self.create_image(40, 50, image=self.dot, anchor=NW, tag="dot")
In the createObjects()
method, we create game objects on the canvas. These are canvas items. They are given initial x and y coordinates. The image
parameter provides the image to be displayed. The anchor
parameter is set to NW
; this way the coordinates of the canvas item are the top-left points of the items. This is important if we want to be able to display images next to the borders of the root window. Try to delete the anchor and see what happens. The tag
parameter is used to identify items on the canvas. One tag may be used for multiple canvas items.
The checkApple()
method checks if the snake has hit the apple object. If so, we add another snake joint and call the locateApple()
.
apple = self.find_withtag("apple") head = self.find_withtag("head")
The find_withtag()
method finds an item on the canvas using its tag. We need two items: the head of the snake and the apple. Note that even if there is only one item with a given tag, the method returns a tuple. This is a case for the apple item. And later the apple item is accessed the following way: apple[0]
.
x1, y1, x2, y2 = self.bbox(head) overlap = self.find_overlapping(x1, y1, x2, y2)
The bbox()
method returns the bounding box points of an item. The find_overlapping()
method finds colliding items for the given coordinates.
for ovr in overlap: if apple[0] == ovr: x, y = self.coords(apple) self.create_image(x, y, image=self.dot, anchor=NW, tag="dot") self.locateApple()
If the apple collides with the head, we create a new dot item at the coordinates of the apple object. We call the locateApple()
method, which deletes the old apple item from the canvas and creates and randomly positions a new one.
In the doMove()
method we have the key algorithm of the game. To understand it, look at how the snake is moving. We control the head of the snake. We can change its direction with the cursor keys. The rest of the joints move one position up the chain. The second joint moves where the first was, the third joint where the second was etc.
z = 0 while z < len(items)-1: c1 = self.coords(items[z]) c2 = self.coords(items[z+1]) self.move(items[z], c2[0]-c1[0], c2[1]-c1[1]) z += 1
This code moves the joints up the chain.
if self.left: self.move(head, -DOT_SIZE, 0)
Move the head to the left.
In the checkCollisions()
method, we determine if the snake has hit itself or one of the walls.
x1, y1, x2, y2 = self.bbox(head) overlap = self.find_overlapping(x1, y1, x2, y2) for dot in dots: for over in overlap: if over == dot: self.inGame = False
We finish the game if the snake hits one of its joints with the head.
if y1 > HEIGHT - DOT_SIZE: self.inGame = False
We finish the game if the snake hits the bottom of the Board
.
The locateApple()
method locates a new apple randomly on the board and deletes the old one.
apple = self.find_withtag("apple") self.delete(apple[0])
Here we find and delete the apple that was eaten by the snake.
r = random.randint(0, RAND_POS)
We get a random number from 0
to RAND_POS
- 1.
self.apple_x = r * DOT_SIZE ... self.apple_y = r * DOT_SIZE
These lines set the x and y coordinates of the apple object.
In the onKeyPressed()
method of the Board
class, we determine the keys that were pressed.
if key == "Left" and not self.right: self.left = True self.up = False self.down = False
If we hit the left cursor key, we set left variable to true. This variable is used in the doMove()
method to change coordinates of the snake object. Notice also that when the snake is heading to the right, we cannot turn immediately to the left.
def onTimer(self): if self.inGame: self.checkCollisions() self.checkApple() self.doMove() self.after(DELAY, self.onTimer) else: self.gameOver()
Every DELAY
ms, the onTimer()
method is called. If we are in the game, we call three methods that build the logic of the game. Otherwise the game is finished. The timer is based on the after()
method, which calls a method after DELAY
ms only once. To repeatedly call the timer, we recursively call the onTimer()
method.
def gameOver(self): self.delete(ALL) self.create_text(self.winfo_width()/2, self.winfo_height()/2, text="Game Over", fill="white")
If the game is over, we delete all items on the canvas. Then we draw "Game Over" in the center of the screen.

This was the Nibbles computer game created with the Tkinter library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论