How to Make a game with Python

How to Make a game with Python



How to Make a game with Python
How to Make a game with Python



We update the retro classic Pong for the Linux generation with a new library called SimpleGUITk


The Raspberry Pi is a fantastic way to start learning how to code. One area that can be very rewarding for amateur coders is game programming,
allowing for a more interactive result and a greater sense of accomplishment. Game programming can also teach improvisation and advanced mathematics skills for code. We’ll be using the fantastic SimpleGUITk module in Python, a very straightforward way of creating graphical interfaces based on Tkinter.

01 Python module preparation

Head to the websites we’ve listed in ‘What you’ll need’ and download a zip of the source fi les from the GitHub pages. Update your Raspbian packages and then install the following: $ sudo apt-get install python-dev python-setuptools tk8.5-dev tcl8.5-dev

 02 Install the modules 


Open the terminal and use cd to move to the extracted Pillow folder. Once there, type:
$ sudo python setup.py install Once that’s complete, move to the simpleguitk folder and use the same command to install that as well.

03 Write your code




Launch IDLE 2, rather than IDLE 3, and open a new window. Use the code listing to create our game ‘Tux for Two’. Be careful to follow along with the code to make sure you know what you’re doing. This way, you can make your own changes to the game rules if you wish.

04 Set up the game

There’s nothing too groundbreaking to start the code: Tux’s and the paddles’ initial positions are set, along with the initial speed and direction of Tux. These are also used when a point is won and the playing field is reset. The direction and speed is set to random for each spawn.

05 The SimpleGUI code

The important parts in the draw function are the draw_line, draw_image and draw_text functions. These are specifically from SimpleGUI, and allow you to easily put these objects on the screen with a position, size and colour. You need to tie them to an object, though – in this case, canvas.

06 SimpleGUI setup code


The last parts are purely for the interface. We tell the code what to do when a key is depressed and then released, and give it a frame to work in. The frame is then told what functions handle the graphics, key functions etc. Finally, we give it frame.start() so it starts.


import simpleguitk as simplegui
import random
w = 600
h = 400
tux_r = 20
pad_w= 8
pad_h = 80
def tux_spawn(right):
global tux_pos, tux_vel
tux_pos = [0,0]
tux_vel = [0,0]
tux_pos[0] = w/2
tux_pos[1] = h/2
if right:
tux_vel[0] = random.randrange(2, 4)
else:
tux_vel[0] = -random.randrange(2, 4)
tux_vel[1] = -random.randrange(1, 3)
def start():
global paddle1_pos, paddle2_pos, paddle1_vel, paddle2_vel
global score1, score2
tux_spawn(random.choice([True, False]))
score1, score2 = 0,0
paddle1_vel, paddle2_vel = 0,0
paddle1_pos, paddle2_pos = h/2, h/2
def draw(canvas):
global score1, score2, paddle1_pos, paddle2_pos, 􀀛􀀃􀀃
tux_pos, tux_vel
if paddle1_pos > (h - (pad_h/2)):
paddle1_pos = (h - (pad_h/2))
elif paddle1_pos < (pad_h/2):
paddle1_pos = (pad_h/2)
else:
paddle1_pos += paddle1_vel
if paddle2_pos > (h - (pad_h/2)):
paddle2_pos = (h - (pad_h/2))
elif paddle2_pos < (pad_h/2):
paddle2_pos = (pad_h/2)
else:
paddle2_pos += paddle2_vel
canvas.draw_line([w / 2, 0],[w / 2, h], 4, “Green”)
canvas.draw_line([(pad_w/2), paddle1_pos + (pad_h/2)], 􀀛􀀃
[(pad_w/2), paddle1_pos - (pad_h/2)], pad_w, “Green”)
canvas.draw_line([w - (pad_w/2), paddle2_pos + (pad_h/2)], 􀀛􀀃
[w - (pad_w/2), paddle2_pos - (pad_h/2)], pad_w, “Green”)
tux_pos[0] += tux_vel[0]
tux_pos[1] += tux_vel[1]
if tux_pos[1] <= tux_r or tux_pos[1] >= h - tux_r:
tux_vel[1] = -tux_vel[1]*1.1
if tux_pos[0] <= pad_w + tux_r:

if (paddle1_pos+(pad_h/2)) >= tux_pos[1] 􀀛 >= (paddle1_
pos-(pad_h/2)):
tux_vel[0] = -tux_vel[0]*1.1
tux_vel[1] *= 1.1
else:
score2 += 1
tux_spawn(True)
elif tux_pos[0] >= w - pad_w - tux_r:
if (paddle2_pos+(pad_h/2)) >= tux_pos[1] >= 􀀛􀀃
(paddle2_pos-(pad_h/2)):
tux_vel[0] = -tux_vel[0]
tux_vel[1] *= 1.1
else:
score1 += 1
tux_spawn(False)
canvas.draw_image(tux, (265 / 2, 314 / 2), (265, 314), 􀀛􀀃
tux_pos, (45, 45))
canvas.draw_text(str(score1), [150, 100], 30, “Green”)
canvas.draw_text(str(score2), [450, 100], 30, “Green”)
def keydown(key):
global paddle1_vel, paddle2_vel
acc = 3
if key == simplegui.KEY_MAP[“w”]:
paddle1_vel -= acc
elif key == simplegui.KEY_MAP[“s”]:
paddle1_vel += acc
elif key==simplegui.KEY_MAP[“down”]:
paddle2_vel += acc
elif key==simplegui.KEY_MAP[“up”]:
paddle2_vel -= acc
def keyup(key):
global paddle1_vel, paddle2_vel
acc = 0
if key == simplegui.KEY_MAP[“w”]:
paddle1_vel = acc
elif key == simplegui.KEY_MAP[“s”]:
paddle1_vel = acc
elif key==simplegui.KEY_MAP[“down”]:
paddle2_vel = acc
elif key==simplegui.KEY_MAP[“up”]:
paddle2_vel = acc
frame = simplegui.create_frame(“Tux for Two”, w, h)
frame.set_draw_handler(draw)
frame.set_keydown_handler(keydown)
frame.set_keyup_handler(keyup)
tux = simplegui.load_image(‘http://upload.wikimedia.org/ 􀀛􀀃
wikipedia/commons/a/af/Tux.png’)
start()
frame.start()

Publier un commentaire

0 Commentaires