Differences between revisions 38 and 49 (spanning 11 versions)
Revision 38 as of 2010-06-07 08:37:25
Size: 4016
Editor: smp-82-165
Comment: good
Revision 49 as of 2020-12-03 10:36:07
Size: 4005
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
# Copyright (c) 2005 Nokia Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
= Game Programming With Python =
You can write whole games in Python using [[http://www.pygame.org/|PyGame]]. See a list of other PythonGameLibraries maintained in this Wiki, or [[http://www.devmaster.net/engines/list.php?fid=6&sid=11|this list maintained on DevMaster.net]]. A full tutorial can be found in the free book [[http://inventwithpython.com/pygame|"Making Games with Python & Pygame"]].
Line 15: Line 4:
import appuifw
from graphics import *
import e32
from key_codes import *
If you have an existing game and want to add a scripting engine to make it more flexible, Python is also a very good choice. But you'll have to learn about IntegratingPythonWithOtherLanguages.
Line 20: Line 6:
class Keyboard(object):
    def __init__(self,onevent=lambda:None):
        self._keyboard_state={}
        self._downs={}
        self._onevent=onevent
    def handle_event(self,event):
        if event['type'] == appuifw.EEventKeyDown:
            code=event['scancode']
            if not self.is_down(code):
                self._downs[code]=self._downs.get(code,0)+1
            self._keyboard_state[code]=1
        elif event['type'] == appuifw.EEventKeyUp:
            self._keyboard_state[event['scancode']]=0
        self._onevent()
    def is_down(self,scancode):
        return self._keyboard_state.get(scancode,0)
    def pressed(self,scancode):
        if self._downs.get(scancode,0):
            self._downs[scancode]-=1
            return True
        return False
keyboard=Keyboard()
== Websites ==
[[https://coderslegacy.com/python/python-pygame-tutorial/|Pygame - The Full Tutorial]] - A complete pygame tutorial that teaches it from the ground up. Several game projects are also available with step by step explanations and the source.
Line 43: Line 9:
appuifw.app.screen='full'
img=None
def handle_redraw(rect):
    if img:
        canvas.blit(img)
appuifw.app.body=canvas=appuifw.Canvas(
    event_callback=keyboard.handle_event,
    redraw_callback=handle_redraw)
img=Image.new(canvas.size)
[[https://web.archive.org/web/20121021155936/http://onlamp.com/pub/a/python/2002/07/11/pythonnews.html|Humongous Python]] for a 2002 case study on Pygame.
Line 53: Line 11:
running=1
def quit():
    global running
    running=0
appuifw.app.exit_key_handler=quit
[[https://coderslegacy.com/python/pygame-rpg-game-tutorial/|Pygame RPG Series]] - A series of short tutorials over the course of which a complete RPG styled game is created using Pygame.
Line 59: Line 13:
location=[img.size[0]/2,img.size[1]/2]
speed=[0.,0.]
blobsize=16
xs,ys=img.size[0]-blobsize,img.size[1]-blobsize
gravity=0.03
acceleration=0.05
[[https://pygametutorials.wikidot.com/|Pygame programming tutorials]] is a compilation of a few short tutorials for Pygame.
Line 66: Line 15:
import time
start_time=time.clock()
n_frames=0
# To speed things up, we prerender the text.
labeltext=u'Use arrows to move ball'
textrect=img.measure_text(labeltext, font='normal')[0]
text_img=Image.new((textrect[2]-textrect[0],textrect[3]-textrect[1]))
text_img.clear(0)
text_img.text((-textrect[0],-textrect[1]),labeltext,fill=0xffffff,font='normal')
[[https://pythonspot.com/game-development-with-pygame/|Game development with Pygame]] is a tutorial that is an introduction to Pygame. Also contains tutorials on how to make several basic games in Pygame.
Line 76: Line 17:
while running:
    img.clear(0)
    img.blit(text_img, (0,0))
    img.point((location[0]+blobsize/2,location[1]+blobsize/2),
              0x00ff00,width=blobsize)
    handle_redraw(())
    e32.ao_yield()
    speed[0]*=0.999
    speed[1]*=0.999
    speed[1]+=gravity
    location[0]+=speed[0]
    location[1]+=speed[1]
    if location[0]>xs:
        location[0]=xs-(location[0]-xs)
        speed[0]=-0.80*speed[0]
        speed[1]=0.90*speed[1]
    if location[0]<0:
        location[0]=-location[0]
        speed[0]=-0.80*speed[0]
        speed[1]=0.90*speed[1]
    if location[1]>ys:
        location[1]=ys-(location[1]-ys)
        speed[0]=0.90*speed[0]
        speed[1]=-0.80*speed[1]
    if location[1]<0:
        location[1]=-location[1]
        speed[0]=0.90*speed[0]
        speed[1]=-0.80*speed[1]
        
    if keyboard.is_down(EScancodeLeftArrow): speed[0] -= acceleration
    if keyboard.is_down(EScancodeRightArrow): speed[0] += acceleration
    if keyboard.is_down(EScancodeDownArrow): speed[1] += acceleration
    if keyboard.is_down(EScancodeUpArrow): speed[1] -= acceleration
    if keyboard.pressed(EScancodeHash):
        filename=u'e:\\screenshot.png'
        canvas.text((0,32),u'Saving screenshot to:',fill=0xffff00)
        canvas.text((0,48),filename,fill=0xffff00)
        img.save(filename)
If you're interested in learning how to use Pygame to create 3D games, two sites that are dedicated to 3D Python are [[http://www.py3d.org/|Python 3D(py3d.org)]] and [[http://www.vrplumber.com/py3d.py|Python 3D Software]]. You can find several 3D game projects available here.
Line 115: Line 19:
    n_frames+=1
end_time=time.clock()
total=end_time-start_time
[[http://www.pyweek.org/|PyWeek]] is a bi-annual programming challenge site that produces several great games.
Line 119: Line 21:
print "%d frames, %f seconds, %f FPS, %f ms/frame."%(n_frames,total,
                                                     n_frames/total,
                                                     total/n_frames*1000.)
== Books ==
There's also some books that specifically cover game programming in Python:

[[http://inventwithpython.com/|http://inventwithpython.com]]

 . [[http://inventwithpython.com/|"Invent Your Own Computer Games with Python"]] is a free, Creative Commons-licensed book on Python for complete beginners with no experience programming. Each chapter has the source code for a small game such as Tic Tac toe, Hangman, Reversi, and others. The final chapters provide an introduction to Pygame.

http://inventwithpython.com/pygame

 . [[http://inventwithpython.com/pygame|"Making Games with Python & Pygame"]] is also a free, Creative Commons-licensed book that assumes a small amount of Python programming experience. It goes into more detail with the Pygame library. There is the source code for games such as Tetris, Connect Four, Simon, Sokoban, and others.

http://www.charlesriver.com/titles/pythongame.html

 . "Game Programming with Python is about building games using Python. It deals with general concepts of game development and specifics that apply when using Python for game development. Some of the general topics include simulations, game architectures, graphics, networking, and user interfaces."

http://www.handysoftware.com/cpif

 . "The author set out to write a book like the one he used to teach himself programming at age 12. ... This book has been successfully used by homeschooling families and public school teachers." The library and example code supplied with the book is also available for download.

http://eu.wiley.com/WileyCDA/WileyTitle/productCd-0470068221.html

 . "Ever want to develop your own computer game? Learn the practical concepts of object-oriented programming for game design using Python in this easy-to-follow, content-filled guide. Whether you're a student, aspiring game developer, or veteran programmer, you'll gain skills as you progress from station to station in a series of clear-cut tutorials on different styles of games. The last stop will be a finished game program for you to show off."

Game Programming With Python

You can write whole games in Python using PyGame. See a list of other PythonGameLibraries maintained in this Wiki, or this list maintained on DevMaster.net. A full tutorial can be found in the free book "Making Games with Python & Pygame".

If you have an existing game and want to add a scripting engine to make it more flexible, Python is also a very good choice. But you'll have to learn about IntegratingPythonWithOtherLanguages.

Websites

Pygame - The Full Tutorial - A complete pygame tutorial that teaches it from the ground up. Several game projects are also available with step by step explanations and the source.

Humongous Python for a 2002 case study on Pygame.

Pygame RPG Series - A series of short tutorials over the course of which a complete RPG styled game is created using Pygame.

Pygame programming tutorials is a compilation of a few short tutorials for Pygame.

Game development with Pygame is a tutorial that is an introduction to Pygame. Also contains tutorials on how to make several basic games in Pygame.

If you're interested in learning how to use Pygame to create 3D games, two sites that are dedicated to 3D Python are Python 3D(py3d.org) and Python 3D Software. You can find several 3D game projects available here.

PyWeek is a bi-annual programming challenge site that produces several great games.

Books

There's also some books that specifically cover game programming in Python:

http://inventwithpython.com

  • "Invent Your Own Computer Games with Python" is a free, Creative Commons-licensed book on Python for complete beginners with no experience programming. Each chapter has the source code for a small game such as Tic Tac toe, Hangman, Reversi, and others. The final chapters provide an introduction to Pygame.

http://inventwithpython.com/pygame

  • "Making Games with Python & Pygame" is also a free, Creative Commons-licensed book that assumes a small amount of Python programming experience. It goes into more detail with the Pygame library. There is the source code for games such as Tetris, Connect Four, Simon, Sokoban, and others.

http://www.charlesriver.com/titles/pythongame.html

  • "Game Programming with Python is about building games using Python. It deals with general concepts of game development and specifics that apply when using Python for game development. Some of the general topics include simulations, game architectures, graphics, networking, and user interfaces."

http://www.handysoftware.com/cpif

  • "The author set out to write a book like the one he used to teach himself programming at age 12. ... This book has been successfully used by homeschooling families and public school teachers." The library and example code supplied with the book is also available for download.

http://eu.wiley.com/WileyCDA/WileyTitle/productCd-0470068221.html

  • "Ever want to develop your own computer game? Learn the practical concepts of object-oriented programming for game design using Python in this easy-to-follow, content-filled guide. Whether you're a student, aspiring game developer, or veteran programmer, you'll gain skills as you progress from station to station in a series of clear-cut tutorials on different styles of games. The last stop will be a finished game program for you to show off."

GameProgramming (last edited 2020-12-03 12:57:14 by ShadowClaw20017)

Unable to edit the page? See the FrontPage for instructions.