Games in Python – the easy way

Slides:



Advertisements
Similar presentations
Sprite-visual object (actor on the stage) Scripts- Tells actors (sprites) what to do.
Advertisements

2000 Prentice Hall, Inc. All rights reserved. 1 Outline 3.1Introduction 3.2Game Loop Components 3.3How to Implement in C# 3.4Adding Image to XNA Project.
Chapter 16 Graphical User Interfaces
Chapter 13 Graphics classes Bjarne Stroustrup
Copyright © 2003 Pearson Education, Inc. Slide 4-1 Created by Cheryl M. Hughes The Web Wizards Guide to XML by Cheryl M. Hughes.
GAME:IT Junior Paddle Ball Objectives: Review skills from Introduction Create a background Add simple object control (up and down) Add how to create a.
0 - 0.
GAME:IT Junior Ping Pong Objectives: Review skills from previous lessons Create a 2-player game Create a scoring display system Using old and new skills,
Create a Simple Game in Scratch
Prof. Yitzchak Rosenthal
View-Based Application Development Lecture 1 1. Flows of Lecture 1 Before Lab Introduction to the Game to be developed in this workshop Comparison between.
Introducing Macromedia FreeHand MX
PowerPoint Basics   Tutorial 5: Navigation
PowerPoint Basics Tutorial 4: Interactivity & Media PowerPoint can communicate with the outside world by linking to different applications, managing different.
Microsoft® Small Basic
Working with Tables for Page Design – Lesson 41 Working with Tables for Page Design Lesson 4.
1 More on Applets Overview l Changing Colors l Changing Fonts & Styles l Applet Life-Cycle l Input using Dialog Window l Input using HTML parameters l.
Chapter 16 Graphical User Interfaces John Keyser’s Modifications of Slides by Bjarne Stroustrup
Microsoft® Small Basic
This game is loosely based on the Whack-A- Mole arcade game.  Each game starts with 45 seconds of play.  Moles randomly pop out of holes on the landscape.
Week 9 Writing Games with Pygame Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
Week 10 Writing Games with Pygame Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Extending the Pong Example Barb Ericson Georgia Tech June 2011.
2 What is pyGame? A set of Python modules to make it easier to write games. –home page: –documentation:
Pygame Dick Steflik.
Guide to Programming with Python
Unit 9 pyGame Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise noted, this.
Week 10 Writing Games with Pygame, continued Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
SDL Programming Introduction. Initialization 2  The first thing you must do is initialize SDL  int SDL_Init( SDL_INIT_EVERYTHING )  This will return.
Computer Science 112 Fundamentals of Programming II Graphics Programming.
1 ball, 2 ball, red ball, blue ball By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Guide to Programming with Python Chapter Twelve Sound, Animation, and Program Development: The Astrocrash Game.
Guide to Programming with Python Week 15 Chapter Twelve Sound, Animation, and Program Development: The Astrocrash Game.
Video Games Writing Games with Pygame Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
CHAPTER 3 (P.49-53) Importing modules + pygame intro.
Creating visual interfaces in python
Instructions Go to the shared area and open a file Go to -> S:\ICT\My Teacher\Mr Crossan\Year 7\Catch the Clown Open the Catch the Clown file by double.
XNA ● Proprietary Microsoft framework ● C#. Interface.
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
PyGame - Unit 2 PyGame Unit – – Animation.
GAME:IT Paddle Ball Objectives: Review skills from Introduction Create a background Add simple object control (up and down) Add how to create a simple.
Intro to Pygame Lecture 05. What is Pygame? It is a set of Python modules designed for writing games. It makes writing games possible for beginners. import.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
11. Skier Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
3. Drawing Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
13. Sprites. Outline 1.Game Things in Pygame (again) 2.The Pygame sprite Module 3.The Sprite Class 4.Groups of Sprites 5.Types of Collision Detection.
Sprites (Images) and Sounds
Sound and more Animations
MOM! Phineas and Ferb are … Aims:
Pixels, Colors and Shapes
Catapult 2016.
PYGAME.
WITH PYGAME ON THE RASPBERRY PI
Let’s Learn 2. Installing Pygame
8. Installing Pygame
Week 8 Classes and Objects
Keyboard Input.
9. Drawing.
9. Drawing Let's Learn Python and Pygame
Catapult 2014 Session 10.
Importing modules + pygame intro
13. Sprites Let's Learn Python and Pygame
Learn… Create… Program
8. Starting Pygame Let's Learn Python and Pygame
Let’s Learn 7. Sprites Saengthong School, June – August 2016
Learn… Create… Program
CSC 221: Introduction to Programming Fall 2018
Presentation transcript:

Games in Python – the easy way Pygame Games in Python – the easy way

The pyGame Package A set of Python modules to help write games Deals with media nicely (pictures, sound) Interacts with user nicely (keyboard, joystick, mouse input) Lots of more advanced features for working with graphics, etc

Where to Start? The official pyGame website Search for tutorials The Application Programming Interface (API) specifies the classes and functions in package Experiment!

A Skeleton Tutorials basically all have the same setup -- let's use it! template.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 from pygame import * from pygame.sprite import * from random import * init() screen = display.set_mode((640, 480)) display.set_caption('Window name!') while True: e = event.poll() if e.type == QUIT: quit() break screen.fill(Color("white")) display.update()

Surface Most of the game elements you see are represented as Surfaces display.set_mode((x, y)) creates your canvas – it returns a Surface object Useful surface methods: fill("color") fills the surface object it's called on blit(surface, area) paints surface onto the object blit is called on in the rectangle bounded by the area tuple Example: screen.blit(ball, (50,50))

Rect Objects that store rectangular coordinates Call .get_rect()on a surface to get its bounding box Rectangle methods/variables: .center holds the object's center as a tuple .colliderect(target) returns True if the parameter overlaps with the object .collidepoint(target) returns True if the target point overlaps with the object

Media Loading an image: Getting a bounding rectangle: img = image.load("file.gif").convert() Getting a bounding rectangle: img_rect = img.get_rect() Loading and playing a sound file: mixer.Sound("file.wav").play()

Sprite Class visible game objects inherit from Ball.py 1 2 3 4 5 6 7 8 9 10 11 from pygame import * from pygame.sprite import * class Ball(Sprite): def __init__(self): Sprite.__init__(self) self.image = image.load("ball.png").convert() self.rect = self.image.get_rect() def update(self): self.rect.center = mouse.get_pos()

Using Sprites They're just objects: initialize them ball = Ball() Create a group of sprites in main sprites = RenderPlain(sprite1, sprite2) Groups know how to draw and update sprites.update() sprites.draw(surface)

Events User input such as clicking, moving mouse or key presses Add more branches to test the result of event.poll() Events to test for: QUIT MOUSEBUTTONDOWN JOYBUTTONDOWN Testing for the letter ‘d’ being pressed using KEYDOWN if e.type == KEYDOWN: if e.key == K_d: …

Adding Text f = font.Font(font, size) goes before your game loop Example: f = font.Font(None, 25) Usually, None is a good enough font! text = Font.render(text, antialias, color) Example: text = f.render("Hello!", True, Color("green")) Returns a surface Must be blit, just like any other surface Example: screen.blit(t, (320, 0))

Exercise: Whack-a-mole Clicking on the mole plays a sound makes the mole move The number of hits is displayed at the top of the screen

Or make your own You can make your own game but DESIGN IT CAREFULLY! You must have something to show by the last day of class It doesn’t need to be complete, but should be playable Requirements: At least two sprites Interaction between sprites User input from keyboard or mouse Some kind of score displayed Have fun!!