3
\$\begingroup\$

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()
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Sep 13, 2013 at 11:14
\$\endgroup\$
1
  • \$\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\$ Commented Sep 13, 2013 at 13:07

2 Answers 2

3
\$\begingroup\$

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
answered Sep 13, 2013 at 21:33
\$\endgroup\$
2
  • \$\begingroup\$ this is very good, but what would sort of code would i need to use these funtions to make the player move \$\endgroup\$ Commented 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\$ Commented Sep 14, 2013 at 20:47
1
\$\begingroup\$

You can create a class called AlienGame(object).

  1. Put all the initialization code in a def __init__ function.

  2. Put all creation of characters in a def create_characters function.

  3. Put the chase part of the code in a def chase_player function.

This segregation of your code will make it more readable.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
answered Sep 13, 2013 at 18:34
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.