My recreation of Pong

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.

My recreation of Pong

Postby Donald8274 » Sat Jun 08, 2013 1:37 am

Hello, this is my first (and best) attempt at making a game with PyGame. I decided to go simple with it and do Pong. There is no AI only 2 players. (P1 = w (up) s (Down) P2 = up key / down key) The goal is 5. I created the music for it which I know pong doesn't have music I just wanted to also make Midi's for the game. If anyone notices anything with the code that could be changed or even made an easier way. Don't let me know exactly how to do it because I still want to try on my own. Thanks for everyone that views this and gives feedback.

Source: http://www.mediafire.com/?x0pbrjt0bb164dg

Includes: .py / .wav / .bmp files needed.

-Donald8274
Donald8274
 
Posts: 9
Joined: Thu Jun 06, 2013 8:00 am

Re: My recreation of Pong

Postby micseydel » Sat Jun 08, 2013 5:04 am

I highly recommend a git repo such as Bitbucket or Github for hosting your code, rather than something like Mediafire (as awesome as it is).
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1387
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: My recreation of Pong

Postby metulburr » Sat Jun 08, 2013 5:19 am

If anyone notices anything with the code that could be changed or even made an easier way.


aside from that getting a github or bitbucket repo, because that advertisement is so annoying:
you are flooding the namespace with
Code: Select all
from pygame import *

and then getting some classes/methods, and/or functions in there to organize the code more.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: My recreation of Pong

Postby Donald8274 » Sat Jun 08, 2013 5:40 am

metulburr, I'm pretty new to Python in general with classes and everything but thank you for mentioning that. I will look into that for my next script which I am just now starting (Space invaders). I'm not sure how else I could use
Code: Select all
from pygame import *
I know it stands to import everything. I learned from this experience that when it includes everything from a certain module, it could mess up certain things because it could for example contain "time" and that slightly messed up my code for a certain part I wanted to do. I tried to do a certain thing with time and it was thinking it was "pygame.time" instead of "time.time". As for mediafire, I will, soon, get a different hosting website, I just knew it was a free / quick way to get it up there.
Donald8274
 
Posts: 9
Joined: Thu Jun 06, 2013 8:00 am

Re: My recreation of Pong

Postby hrs » Sat Jun 08, 2013 9:00 am

You can just use
Code: Select all
import pygame

and then prefix all your pygame functions with 'pygame.' If you think that's too long you can import it in a different namespace
Code: Select all
import pygame as pg


Compare
Code: Select all
Python 2.7.5 (default, May 15 2013, 20:30:03)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import Image
>>> import Tkinter
>>> dir()
['Image', 'Tkinter', '__builtins__', '__doc__', '__name__', '__package__', 'os']
>>> dir(Image)
['ADAPTIVE', 'AFFINE', 'ANTIALIAS', 'BICUBIC', 'BILINEAR', 'CONTAINER', 'CUBIC', 'DEBUG', 'EXTENSION', 'EXTENT', 'FLIP_LEFT_RIGHT', 'FLIP_TOP_BOTTOM', 'FLOYDSTEINBERG', 'ID', 'Image', 'ImageMode', 'ImagePalette', 'ImagePointHandler', 'ImageTransformHandler', 'IntType', 'LINEAR', 'MESH', 'MIME', 'MODES', 'NEAREST', 'NONE', 'NORMAL', 'OPEN', 'ORDERED', 'PERSPECTIVE', 'QUAD', 'RASTERIZE', 'ROTATE_180', 'ROTATE_270', 'ROTATE_90', 'SAVE', 'SEQUENCE', 'StringType', 'TupleType', 'UnicodeStringType', 'VERSION', 'WEB', '_E', '_ENDIAN', '_ImageCrop', '_MAPMODES', '_MODEINFO', '_MODE_CONV', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_conv_type_shape', '_fromarray_typemap', '_getdecoder', '_getencoder', '_getscaleoffset', '_imaging_not_installed', '_initialized', '_show', '_showxv', '_wedge', 'blend', 'byteorder', 'composite', 'core', 'eval', 'fromarray', 'frombuffer', 'fromstring', 'getmodebandnames', 'getmodebands', 'getmodebase', 'getmodetype', 'init', 'isDirectory', 'isImageType', 'isNumberType', 'isSequenceType', 'isStringType', 'isTupleType', 'merge', 'new', 'open', 'os', 'preinit', 'register_extension', 'register_mime', 'register_open', 'register_save', 'string', 'sys', 'warnings']

with
Code: Select all
Python 2.7.5 (default, May 15 2013, 20:30:03)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from os import *
>>> from Image import *
>>> from Tkinter import *
>>> dir()
['ACTIVE', 'ADAPTIVE', 'AFFINE', 'ALL', 'ANCHOR', 'ANTIALIAS', 'ARC', 'At', 'AtEnd', 'AtInsert', 'AtSelFirst', 'AtSelLast', 'BASELINE', 'BEVEL', 'BICUBIC', 'BILINEAR', 'BOTH', 'BOTTOM', 'BROWSE', 'BUTT', 'BaseWidget', 'BitmapImage', 'BooleanType', 'BooleanVar', 'BufferType', 'BuiltinFunctionType', 'BuiltinMethodType', 'Button', 'CASCADE', 'CENTER', 'CHAR', 'CHECKBUTTON', 'CHORD', 'COMMAND', 'CONTAINER', 'CUBIC', 'CURRENT', 'CallWrapper', 'Canvas', 'Checkbutton', 'ClassType', 'CodeType', 'ComplexType', 'DEBUG', 'DISABLED', 'DOTBOX', 'DictProxyType', 'DictType', 'DictionaryType', 'DoubleVar', 'E', 'END', 'EW', 'EXCEPTION', 'EXTENDED', 'EXTENSION', 'EXTENT', 'EX_CANTCREAT', 'EX_CONFIG', 'EX_DATAERR', 'EX_IOERR', 'EX_NOHOST', 'EX_NOINPUT', 'EX_NOPERM', 'EX_NOUSER', 'EX_OK', 'EX_OSERR', 'EX_OSFILE', 'EX_PROTOCOL', 'EX_SOFTWARE', 'EX_TEMPFAIL', 'EX_UNAVAILABLE', 'EX_USAGE', 'EllipsisType', 'Entry', 'Event', 'FALSE', 'FIRST', 'FLAT', 'FLIP_LEFT_RIGHT', 'FLIP_TOP_BOTTOM', 'FLOYDSTEINBERG', 'F_OK', 'FileType', 'FloatType', 'Frame', 'FrameType', 'FunctionType', 'GROOVE', 'GeneratorType', 'GetSetDescriptorType', 'Grid', 'HIDDEN', 'HORIZONTAL', 'ID', 'INSERT', 'INSIDE', 'Image', 'ImageMode', 'ImagePalette', 'ImagePointHandler', 'ImageTransformHandler', 'InstanceType', 'IntType', 'IntVar', 'LAST', 'LEFT', 'LINEAR', 'Label', 'LabelFrame', 'LambdaType', 'ListType', 'Listbox', 'LongType', 'MESH', 'MIME', 'MITER', 'MODES', 'MOVETO', 'MULTIPLE', 'MemberDescriptorType', 'Menu', 'Menubutton', 'Message', 'MethodType', 'Misc', 'ModuleType', 'N', 'NE', 'NEAREST', 'NGROUPS_MAX', 'NO', 'NONE', 'NORMAL', 'NS', 'NSEW', 'NUMERIC', 'NW', 'NoDefaultRoot', 'NoneType', 'NotImplementedType', 'OFF', 'ON', 'OPEN', 'ORDERED', 'OUTSIDE', 'O_APPEND', 'O_ASYNC', 'O_CREAT', 'O_DIRECT', 'O_DIRECTORY', 'O_DSYNC', 'O_EXCL', 'O_LARGEFILE', 'O_NDELAY', 'O_NOATIME', 'O_NOCTTY', 'O_NOFOLLOW', 'O_NONBLOCK', 'O_RDONLY', 'O_RDWR', 'O_RSYNC', 'O_SYNC', 'O_TRUNC', 'O_WRONLY', 'ObjectType', 'OptionMenu', 'PAGES', 'PERSPECTIVE', 'PIESLICE', 'PROJECTING', 'Pack', 'PanedWindow', 'PhotoImage', 'Place', 'QUAD', 'RADIOBUTTON', 'RAISED', 'RASTERIZE', 'READABLE', 'RIDGE', 'RIGHT', 'ROTATE_180', 'ROTATE_270', 'ROTATE_90', 'ROUND', 'R_OK', 'Radiobutton', 'S', 'SAVE', 'SCROLL', 'SE', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'SEL', 'SEL_FIRST', 'SEL_LAST', 'SEPARATOR', 'SEQUENCE', 'SINGLE', 'SOLID', 'ST_APPEND', 'ST_MANDLOCK', 'ST_NOATIME', 'ST_NODEV', 'ST_NODIRATIME', 'ST_NOEXEC', 'ST_NOSUID', 'ST_RDONLY', 'ST_RELATIME', 'ST_SYNCHRONOUS', 'ST_WRITE', 'SUNKEN', 'SW', 'Scale', 'Scrollbar', 'SliceType', 'Spinbox', 'StringType', 'StringTypes', 'StringVar', 'Studbutton', 'TMP_MAX', 'TOP', 'TRUE', 'Tcl', 'TclError', 'TclVersion', 'Text', 'Tk', 'TkVersion', 'Toplevel', 'TracebackType', 'Tributton', 'TupleType', 'TypeType', 'UNDERLINE', 'UNITS', 'UnboundMethodType', 'UnicodeStringType', 'UnicodeType', 'VERSION', 'VERTICAL', 'Variable', 'W', 'WCONTINUED', 'WCOREDUMP', 'WEB', 'WEXITSTATUS', 'WIFCONTINUED', 'WIFEXITED', 'WIFSIGNALED', 'WIFSTOPPED', 'WNOHANG', 'WORD', 'WRITABLE', 'WSTOPSIG', 'WTERMSIG', 'WUNTRACED', 'W_OK', 'Widget', 'Wm', 'X', 'XRangeType', 'XView', 'X_OK', 'Y', 'YES', 'YView', '__builtins__', '__doc__', '__name__', '__package__', 'abort', 'access', 'altsep', 'blend', 'byteorder', 'chdir', 'chmod', 'chown', 'chroot', 'close', 'closerange', 'composite', 'confstr', 'confstr_names', 'core', 'ctermid', 'curdir', 'defpath', 'devnull', 'dup', 'dup2', 'environ', 'error', 'eval', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fchdir', 'fchmod', 'fchown', 'fdatasync', 'fdopen', 'fork', 'forkpty', 'fpathconf', 'fromarray', 'frombuffer', 'fromstring', 'fstat', 'fstatvfs', 'fsync', 'ftruncate', 'getboolean', 'getcwd', 'getcwdu', 'getdouble', 'getegid', 'getenv', 'geteuid', 'getgid', 'getgroups', 'getint', 'getloadavg', 'getlogin', 'getmodebandnames', 'getmodebands', 'getmodebase', 'getmodetype', 'getpgid', 'getpgrp', 'getpid', 'getppid', 'getresgid', 'getresuid', 'getsid', 'getuid', 'image_names', 'image_types', 'init', 'initgroups', 'isDirectory', 'isImageType', 'isNumberType', 'isSequenceType', 'isStringType', 'isTupleType', 'isatty', 'kill', 'killpg', 'lchown', 'linesep', 'link', 'listdir', 'lseek', 'lstat', 'mainloop', 'major', 'makedev', 'makedirs', 'merge', 'minor', 'mkdir', 'mkfifo', 'mknod', 'name', 'new', 'nice', 'open', 'openpty', 'os', 'pardir', 'path', 'pathconf', 'pathconf_names', 'pathsep', 'pipe', 'popen', 'popen2', 'popen3', 'popen4', 'preinit', 'putenv', 're', 'read', 'readlink', 'register_extension', 'register_mime', 'register_open', 'register_save', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'sep', 'setegid', 'seteuid', 'setgid', 'setgroups', 'setpgid', 'setpgrp', 'setregid', 'setresgid', 'setresuid', 'setreuid', 'setsid', 'setuid', 'spawnl', 'spawnle', 'spawnlp', 'spawnlpe', 'spawnv', 'spawnve', 'spawnvp', 'spawnvpe', 'stat', 'stat_float_times', 'stat_result', 'statvfs', 'statvfs_result', 'strerror', 'string', 'symlink', 'sys', 'sysconf', 'sysconf_names', 'system', 'tcgetpgrp', 'tcsetpgrp', 'tempnam', 'times', 'tkinter', 'tmpfile', 'tmpnam', 'ttyname', 'umask', 'uname', 'unlink', 'unsetenv', 'urandom', 'utime', 'wait', 'wait3', 'wait4', 'waitpid', 'walk', 'wantobjects', 'warnings', 'write']
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

Re: My recreation of Pong

Postby Mekire » Sun Jun 09, 2013 2:50 pm

There are many many things I could recommend about this code but you really do need to take a step back first. As already said, don't use the star import. It is a bad foot to get off on if you are just starting programming.

As for functions and classes, you should really take this seriously. Not using classes is one thing, but coding your entire program in the global namespace really is unacceptable. I strongly suggest you soundly learn how to use functions before even starting your next game project. Game programming shouldn't even be attempted without functions. If you need suggestions on how to convert this program to functions I would love to help, but until you do that critiquing the code is out of the question.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 2 guests