I have just made a simple script which spawns an alien that chases the player. I want to move as much of the script into functions so as to minimize the amount of code, to make it run better when my game gets a lot bigger. I am new to functions and classes, and I want to know what can be turned into a class in another script and how to go about doing it. I do not want just code, I want to be able to understand what I'm doing with it as well.
import pygame, sys, random, time, math
from pygame.locals import *
pygame.init()
bifl = 'screeing.jpg'
milf = 'character.png'
alien = 'alien_1.png'
screen = pygame.display.set_mode((640, 480))
background = pygame.image.load(bifl).convert()
mouse_c = pygame.image.load(milf).convert_alpha()
nPc = pygame.image.load(alien).convert_alpha()
x, y = 0, 0
movex, movey = 0, 0
z, w = random.randint(10, 480), random.randint(10, 640)
movez, movew = 0, 0
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_w:
movey = - 4
elif event.key == K_s:
movey = + 4
elif event.key == K_a:
movex = - 4
elif event.key == K_d:
movex = + 4
if event.type == KEYUP:
if event.key == K_w:
movey = 0
elif event.key == K_s:
movey = 0
elif event.key == K_a:
movex = 0
elif event.key == K_d:
movex = 0
if w < x:
movew =+ 0.4
if w > x:
movew =- 0.4
if z < y:
movez =+ 0.4
if z > y:
movez =- 0.4
x += movex
y += movey
w += movew
z += movez
print('charecter pos: ' + str(x) + str(y))
print('alien pos: ' + str(w) + str(z))
chpos = x + y
alpos = w + z
print(alpos, chpos)
if chpos == alpos:
pygame.quit()
sys.exit()
screen.blit(background, (0, 0))
screen.blit(mouse_c, (x, y))
screen.blit(nPc, (w, z))
pygame.display.update()
-
\$\begingroup\$ Not into Python but if you can put a name on a concept and picture that concept having properties and/or exposing methods or functions, you have a candidate for a class. As for functions, they should be doing as little as possible, which makes them easier to name and reuse. \$\endgroup\$Mathieu Guindon– Mathieu Guindon2013年09月13日 13:07:25 +00:00Commented Sep 13, 2013 at 13:07
2 Answers 2
Long multiple choice decisions in python work well as dictionaries, rather than elifs.
def handle_event(event):
OnKeyDown = {K_w:(0, 4), K_s:(0,-4), K_a:(-4, 0), K_d:(4,0)}
OnKeyUp = {K_w:(False, True), K_s:(False, True), K_a:(True, False), K_d:(True, False)}
if event.type == KEYUP:
delta = OnKeyDown[event]
movex += delta[0]
movey += delta[1]
elif event.type == KEYDOWN:
delta = OnKeyUp[event]
if delta[0]: movex = 0
if delta[1]: movey = 0
x += movex
y += movey
-
\$\begingroup\$ this is very good, but what would sort of code would i need to use these funtions to make the player move \$\endgroup\$testing4theLOLs4theWIN3– testing4theLOLs4theWIN32013年09月14日 09:52:42 +00:00Commented Sep 14, 2013 at 9:52
-
\$\begingroup\$ This function would replace the core if/elif block in your code above. You could probably just replace that whole section with this function . \$\endgroup\$theodox– theodox2013年09月14日 20:47:08 +00:00Commented Sep 14, 2013 at 20:47
You can create a class called AlienGame(object)
.
Put all the initialization code in a
def __init__
function.Put all creation of characters in a
def create_characters
function.Put the chase part of the code in a
def chase_player
function.
This segregation of your code will make it more readable.