How do I implement random direction change on my pygame enemy? - python

So I currently have an enemy class that moves left, encounters the end of the screen, then flips direction 180 degrees and walks right. It does this constantly.
I wish to make it randomly change direction, so it's more unpredictable. The issue I'm encountering is when I implement a random number generator for the distance of direction, how would I then inherit it into a variable when I create the instance? Or is there a more efficient way to do this?
Entire enemy class:
class enemy(object):#need to use self to access these
walkRight = [pygame.image.load('Game/R1E.png'),pygame.image.load('Game/R2E.png'),pygame.image.load('Game/R3E.png'),pygame.image.load('Game/R4E.png'),pygame.image.load('Game/R5E.png'),pygame.image.load('Game/R6E.png'),pygame.image.load('Game/R7E.png'),pygame.image.load('Game/R8E.png'),pygame.image.load('Game/R9E.png'),pygame.image.load('Game/R10E.png'),pygame.image.load('Game/R11E.png')]
walkLeft = [pygame.image.load('Game/L1E.png'),pygame.image.load('Game/L2E.png'),pygame.image.load('Game/L3E.png'),pygame.image.load('Game/L4E.png'),pygame.image.load('Game/L5E.png'),pygame.image.load('Game/L6E.png'),pygame.image.load('Game/L7E.png'),pygame.image.load('Game/L8E.png'),pygame.image.load('Game/L9E.png'),pygame.image.load('Game/L10E.png'),pygame.image.load('Game/L11E.png')]
def __init__(self,x,y,width,height,end): #get R3E png
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.path = [self.x,self.end]
self.walkCount = 0
self.vel = 3
def draw(self,window): #enemy is gonna move from left, to right, left, to right etc between 2 co ordinate points
self.move()
if self.walkCount + 1 >= 33: #if sign is changed, as walkcount would always be less than 33, it wouldn't change
self.walkCount = 0
#rather than using self.left, sel.right, we can use out velocity
if self.vel > 0: #this means we're moving right, integer division 3 so it doesn't look like we going too fast
window.blit(self.walkRight[self.walkCount //3],(self.x,self.y))
self.walkCount += 1
else:
window.blit(self.walkLeft[self.walkCount //3],(self.x,self.y))
self.walkCount += 1 #if not moving right, we're moving left
#check if we're drawing an image to the left or right
def move(self): #move method #to change directions, he needs to change velocity (multiply by -1 etc)
if self.vel > 0:
if self.x < self.path[1] + self.vel:#check if he's about to move past the point on screen, we're accessing the self.end eleme
self.x += self.vel
else: #e.g. if it's greater than, we change direction
self.vel = self.vel * -1 #flipped 180 degrees, so goes other direction
self.x += self.vel
self.walkCount = 0
else: #if our vel is negative
if self.x > self.path[0] - self.vel:
self.x += self.vel #vel is gonna be negative already if we've changed directions
else:
self.vel = self.vel * -1
self.x += self.vel
self.walkCount = 0
Movement function:
def move(self): #move method #to change directions, he needs to change velocity (multiply by -1 etc)
if self.vel > 0:
if self.x < self.path[1] + self.vel:#check if he's about to move past the point on screen, we're accessing the self.end eleme
self.x += self.vel
else: #e.g. if it's greater than, we change direction
self.vel = self.vel * -1 #flipped 180 degrees, so goes other direction
self.x += self.vel
self.walkCount = 0
else: #if our vel is negative
if self.x > self.path[0] - self.vel:
self.x += self.vel #vel is gonna be negative already if we've changed directions
else:
self.vel = self.vel * -1
self.x += self.vel
self.walkCount = 0
Creating the instance just before the main loop(x,y,height,width,walk distance):
goblin = enemy(0,440,64,64,450)
My attempt - still only takes one random number & permanently uses it whilst the game runs.
def timerthing():
pathenemy = random.randint(0,450)
return pathenemy
#now to write main loop which checks for collisions, mouse events etc
#make path randomly change
man = player(200,444,64,66) #creates instance of the player / object
goblin = enemy(0,440,64,64,timerthing())#need to create instance of enemy so he appears on the screen, path ends at timerthing
run = True
bullets = []
while run:
clock.tick(15) #frame rate
timerthing()
Instead of a constant of 450, I'm after a constantly changing variable instead, so his movement is unpredictable. Any other suggestions would be appreciated - thanks.

import random
def timerthing():
pathenemy = random.randint(0,450)
time.sleep(1)
return pathenemy
But I suspect that's not really your problem. It sounds like you have a configuration problem rather than a source problem, and to solve that we'd need an entirely different kind of information from you.

If I had to do code this randomly moving enemy, I would implement two methods first:
One method that returns the possible directions my enemy can go through, given the state of the board. Yet I can't help you with that, since we don't have the "board" part of your code. Let's call this method getPossibleDirections()
One method move, with one argument direction, representing the direction taken by my enemy. Let's call this method move(direction)
Based on your code, I guess you can implement these two methods by yourself. And maybe you want your ennemy to move in the y direction too?
Like #Mark Storer said, you could use the random package, but I will use the random part of the numpy library (already included in python) for my answer:
# you will need to import numpy in the beginning of your file
import numpy as np
class enemy(...)
# your code goes there
def getPossibleDirections(self):
#your implementation
pass
def move(self, direction):
#your implementation
pass
def moveRandomly(self):
possibleDirections = self.getPossibleDirections()
chosenDirection = np.random.randint(0,len(possibleDirections))
# returns one number between 0 and len(possibleDirections) (not included) that can represent your direction
self.move(possibleDirection[chosenDirection])
To move your ennemy randomly, you will just have to call enemy.moveRandomly().

Related

Is there any way I can make my code easier to understand and simpler? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I am wondering if by any chance there is a way to optimise this portion of my code. Regarding the pathing system and the redraw section. I find it really difficult to follow through it. Even changing the whole thing into a more understanding format will suit me. Thank you very much.
class wolf(object): #creating the wolf class
wolf_right = ['pics/WR.png'] + ['pics/WR' + str(i) + '.png' for i in range(2, 18)] #identifies the right wolf pics
wolf_left = ['pics/WL.png'] + ['pics/WL' + str(i) + '.png' for i in range(2, 18)] #identifies the left wolf pics
run_right = [pg.transform.smoothscale(pg.image.load(img), (280,160)) for img in wolf_right] #loads the right wolf pics
run_left = [pg.transform.smoothscale(pg.image.load(img), (280,160)) for img in wolf_left] #load the left wolf pics
def __init__(self, x, y, width, height, finish): #initialising the object wolf
self.x = x #x coord of wolf
self.y = y #y coord of wolf
self.width = width #width of wolf
self.height = height #height of wolf
self.path = [x, finish] # This part here determines the movement limits of the wolf, back and forth
self.run_distance = 0 #wolf is initially did not move
self.velocity = 9 #speed of wolf moving
self.collision_box = (self.x + 60, self.y, 280, 160)#the arguments inside are the coordinates designating the sides of the box
#and then the width and the height of the box
def pathing(self): #pathing system
if self.velocity > 0: # If wolf moving to the right
if self.x < self.path[1] + self.velocity: #ensures that it keeps moving if wolf is not at finish
self.x += self.velocity #allows wolf to move
else: #if the finish is reached then go backwards
self.velocity = self.velocity * -1 #where velocity goes negative
#according to displacement, a particle with -ve velocity goes backwards
self.x += self.velocity #allows wolf to move
else: # If wold is going to the left
if self.x > self.path[0] - self.velocity: #ensures that it keeps moving if the wolf is not at finish
self.x += self.velocity #allows wolf to move
else: #if the finish is reached then go backwards
self.velocity = self.velocity * -1 #where velocity goes negative
#according to displacement, a particle with -ve velocity goes backwards
self.x += self.velocity #allows wolf to move
def got_hit(self): #function if the wolf takes damage from the ninja
print("Congrats, you have hit the wolf!")
def redraw(self, win):#just like for the ninja we do the same steps
self.pathing()
if self.run_distance + 1 >= 51: #This time I am running 17 sprites thus, 17 * 3 (where 3 sprites per second)
self.run_distance = 0
if self.velocity < 0: #if velocity is increasing meaning movement, links left images with left movement
win.blit(self.run_left[self.run_distance//3], (self.x,self.y))
self.run_distance += 1
else: #else linking right images with right movement
win.blit(self.run_right[self.run_distance//3], (self.x,self.y))
self.run_distance += 1
#pg.draw.rect(win, (0,200,0), self.collision_box,2) #this will draw a green box around the wolf of lines thickness 2
self.collision_box = (self.x + 50 , self.y, 200, 150) # ensures the box is drawn and is updated alongside motion
The code is not that complicated, but that's my opinion. I read it once through and it was easy to follow. Just one thing, self.x += self.velocity is done at the end of each of the 4 cases in pathing. It is sufficient to do it once at the end of pathing, instead of separately in each case. Something similar can be done for self.run_distance += 1 in redraw:
class wolf(object): #creating the wolf class
# [...]
def pathing(self): #pathing system
if self.velocity > 0 and self.x >= self.path[1] + self.velocity or \
self.velocity < 0 and self.x <= self.path[0] - self.velocity:
self.velocity = self.velocity * -1
self.x += self.velocity
# [...]
def redraw(self, win):#just like for the ninja we do the same steps
self.pathing()
if self.run_distance + 1 >= 51: #This time I am running 17 sprites thus, 17 * 3 (where 3 sprites per second)
self.run_distance = 0
run_surf = self.run_left if self.velocity < 0 else self.run_right
win.blit(run_surf[self.run_distance//3], (self.x,self.y))
self.run_distance += 1
#pg.draw.rect(win, (0,200,0), self.collision_box,2) #this will draw a green box around the wolf of lines thickness 2
self.collision_box = (self.x + 50 , self.y, 200, 150) # ensures the box is drawn and is updated alongside motion
Anyway in pygame it is intended to use pygame.Rect, pygame.sprite.Sprite and pygame.sprite.Group.
Each Sprite should have the attributes .rect and .image and the method update(). The Sprites should be contained in Groups. The Groups can be drawn (draw()) and updated (update()).
That makes the code easy to read, short, comprehensible and extendable. e.g.:
(Class Names should normally use the CapWords convention.)
class Wolf(pygame.sprite.Sprite):
wolf_right = ['pics/WR.png'] + ['pics/WR' + str(i) + '.png' for i in range(2, 18)] #identifies the right wolf pics
wolf_left = ['pics/WL.png'] + ['pics/WL' + str(i) + '.png' for i in range(2, 18)] #identifies the left wolf pics
run_right = [pg.transform.smoothscale(pg.image.load(img), (280,160)) for img in wolf_right] #loads the right wolf pics
run_left = [pg.transform.smoothscale(pg.image.load(img), (280,160)) for img in wolf_left] #load the left wolf pics
def __init__(self, x, y, finish):
super().__init__():
self.image = run_left[0]
self.rect = pygame.Rect(x, y, 280, 160)
self.path = [x, finish] # This part here determines the movement limits of the wolf, back and forth
self.run_distance = 0 #wolf is initially did not move
self.velocity = 9 #speed of wolf moving
self.collision_box = (self.rect.x + 60, self.rect.y, 280, 160) #the arguments inside are the coordinates designating the sides of the box
#and then the width and the height of the box
def update(self, angle):
if self.velocity > 0 and self.rect.x >= self.path[1] + self.velocity or \
self.velocity < 0 and self.rect.x <= self.path[0] - self.velocity:
self.velocity = self.velocity * -1
self.rect.x += self.velocity
if self.run_distance + 1 >= 51: #This time I am running 17 sprites thus, 17 * 3 (where 3 sprites per second)
self.run_distance = 0
run_surf = self.run_left if self.velocity < 0 else self.run_right
if self.run_distance//3 > len(run_surf):
self.run_distance = 0
self.image = run_surf[self.run_distance//3]
self.run_distance += 1
self.collision_box = (self.rect.x + 50 , self.rect.y, 200, 150) # ensures the box is drawn and is updated alongside motion
wolf = Wolf(........)
all_sprites = pygame.sprite.Group()
all_sprites.add(wolf)
while True:
# [...]
all_sprites.update(win)
# [...]
all_sprites.draw(win)
pygame.display.flip()

Making an opponent object using a class not working properly; they don't display on the screen for some reason?

So a while ago I learned how to create a class for which had the purpose of creating an "opponent" which would basically fight the player and etc. It was a good tutorial and while I did learn how to create this type of class, I also got issues from the code itself when testing it out.
One of the reasons it didn't work properly was because of flipping my sprites horizontally; I have 2 sprite variables, 1 that loads the images and the other which is supposed to contain a list. A loop then "flips" all the images from the original and stores it inside the empty list. This, however, caused an issue and started to make my sprite "flash" on the screen in both directions so I removed the loop, tried again and it worked but this time it only had one sprite(facing the left).
I also tried to remove the variables and the loop outside the class but that ended up not displaying the image at all.
#Goku Black
walkLeftGB = [ pygame.image.load("GB1.png"), pygame.image.load("GB2.png"), pygame.image.load("GB3.png"), pygame.image.load("GB4.png") ]
walkRightGB = []
for l in walkLeftGB:
walkRightGB.append(pygame.transform.flip(l, True, False))
for x in range(len(walkLeftGB)):
walkLeftGB[x] = pygame.transform.smoothscale(walkLeftGB[x], (372, 493))
for x in range(len(walkRightGB)):
walkRightGB[x] = pygame.transform.smoothscale(walkRightGB[x], (372, 493))
# === CLASSES === (CamelCase names)
class Enemy(object):
global vel
global walkCount1
global walkRightGB, walkLeftGB
def __init__(self, x, y, width, height, end):
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.path = [self.x, self.end]
self.walkCount1 = 0
self.vel = 3
self.walkLeftGB = walkLeftGB
self.walkRightGB = walkRightGB
def draw(self, DS):
self.move()
global walkCount1
if self.walkCount1 + 1 <= 33:
self.walkCount1 = 0
if self.vel > 0:
DS.blit(self.walkRightGB[self.walkCount1 //4], (self.x, self.y))
self.walkCount1 += 1
else:
DS.blit(self.walkLeftGB[self.walkCount1 //4], (self.x, self.y))
self.walkCount1 += 1
def move(self):
global walkCount1
if self.vel > 0:
if self.x + self.vel < self.path[1]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount1 = 0
else:
if self.x - self.vel > self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount1 = 0
man = Enemy(1600, 407, 96, 136, 22)
def redrawGameWindow():
global walkCount
pygame.display.update()
man.draw(DS)
DS.blit(canyon,(0,0))
lastMoved = "left"
if walkCount + 1 >= 27:
walkCount = 0
if left:
DS.blit(walkLeft[walkCount//3],(x,y))
walkCount +=1
lastMoved = "left"
elif right:
DS.blit(walkRight[walkCount//3], (x,y))
walkCount +=1
lastMoved = "right"
else: #this is when its moving neither left or right
if lastMoved == "left":
DS.blit(char2, (x, y))
else:
DS.blit(char, (x, y))
#The "redrawGameWindow" is then called in a loop inside a function.
After I made those changes mentioned above, the output received was now the image not appearing at all, I expected the output for the image to appear(and maybe move)
This does not appear to do what you want it to:
if self.walkCount1 + 1 <= 33:
self.walkCount1 = 0
As a detail, it's weird to add one and compare to 33, when if self.walkCount1 <= 32 would suffice.
More importantly, it looks like you wanted >= there.
You have some hard-coded magic number divisors: // 3 and // 4.
Rather than e.g. 4, it would be much better to
refer to len(self.walkRightGB).
Then you could choose to insert new interpolated walking images,
or delete some, without having to worry about correctness of
other code that may be affected.
Numbers like 27 should also be expressed in more meaningful terms.
It's not clear to me you want // integer division.
Possibly you were looking for % modulo instead.
As written, it looks like there's a danger of the code
trying to access past the end of an image array.
Printing out the counts would help you to debug this,
for example by verifying that incremented count is preserved
across function calls.

Sprite not moving in the same direction it is facing

I have an issue with my sprite not moving in the same direction as it is facing. It is top view of a beetle, 10 images animating its leg movements. The sprite animation works fine, choosing new 'random' direction by rotating the image and re-centering to previous center is working too.
What I can't get to work is the sprite to move 'forward', that is to move in the new direction it chooses/faces every second or so. The new direction is simply a small 10-15 degrees rotation left or right from previous position. Instead it moves in what seem like random movements every time the sprite chooses a new direction. For example it will move southwest while facing east, or move north while facing south etc.
I suspect the problem is in the move() method where the movement isn't properly translated via trig values. I am adding to the rect.x values because it is the same as the cartesian coords system while subtracting from rect.y values because it is inverted in pygame compared to cartesian. Going down is increasing positively, up - decreasing y values, increasing negatively.
The class for the beetle sprite is below:
class Foe():
def __init__(self, location):
self.sprites = []
for i in range(1, 11):
file = pg.image.load("beetleprac1/000"+str(i)+"a.png").convert() # 10 sprite files named "0001a-10a.png" last 1 is idle stance
#file.set_colorkey((0, 255, 255))
self.sprites.append(file)
self.move_anim_index = 9
self.image = self.sprites[self.move_anim_index]
self.rotated_image = self.image.copy()
self.rect = self.image.get_rect(center=location)
self.move_rect_coords = [self.rect.x, self.rect.y]
self.angle = 90
self.speed = 3
self.time = 0
self.frames_counter = 0
def move_anim(self, rate=2): # rate is in frames
if self.frames_counter % rate == 0:
self.move_anim_index += 1
if self.move_anim_index >= 9:
self.move_anim_index = 0
self.image = self.sprites[self.move_anim_index]
#self.rect = self.image.get_rect(center=self.rect.center)
self.rotated_image = self.image.copy()
self.rotated_image = pg.transform.rotate(self.image, self.angle - 90)
self.rect = self.rotated_image.get_rect(center=self.rect.center)
def move(self, last_tick):
self.move_anim()
self.rect.x += int(self.speed * math.cos(self.angle))
self.rect.y -= int(self.speed * math.sin(self.angle))
self.move_rect_coords = [self.rect.x, self.rect.y]
def change_direction(self, change_time): # change_time in frames
if self.frames_counter % change_time == 0:
a = random.choice([-1, 1])
self.angle += 15
if self.angle < 0: self.angle += 360
if self.angle > 360: self.angle -=360
#if self.rect.x <
def track_time(self, last_tick, amount):
self.time += last_tick
self.frames_counter += 1
if self.time >= last_tick * amount:
self.time = 0
self.counter = 0
def think(self, last_tick):
self.track_time(last_tick, FPS*2)
self.change_direction(FPS)
def update(self, last_tick, screen_rect):
self.think(last_tick)
self.move(last_tick)
def draw(self, screen):
screen.blit(self.rotated_image, self.move_rect_coords)
And here is the full code and repository at github: https://github.com/fn88/buganimprac2
sin(), cos() and other trigonometric functions use radians -> cos(math.radians(angle))

Pygame spritecollide hitboxes

I am working on a simple 2D platformer, but I am having some trouble with the hitboxes of my sprites. I use the pygame.sprite.spritecollide function to generate a list of blocks(platforms) touching my player sprite.
Here is my player class:
class Player( pygame.sprite.Sprite ):
def __init__(self,x,y,image):
super( Player, self ).__init__()
self.vel_x = 0
self.vel_y = 0
self.moveSpeed = 3
self.jumpPower = 7
self.direction = idle
self.grounded = False
self.falling = True
self.jumping = False
self.climbing = False
self.image = pygame.Surface((50,50))#pygame.transform.scale( image, (50,50))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.width = 50
self.height = 50
self.rect.bottom = self.rect.y + self.height
def set_position( self,x,y ):
self.rect.x = x
self.rect.y = y
def experience_gravity(self, gravity = 0.3):
if not self.grounded:
self.vel_y += gravity
self.falling = True
else:
self.vel_y = 0
self.grounded = True
self.falling = False
def jump(self):
self.grounded = False
self.vel_y -= self.jumpPower
def update(self, collidable = pygame.sprite.Group() ):
global collision
self.experience_gravity()
self.rect.x += self.vel_x
self.rect.y += self.vel_y
collision_list = pygame.sprite.spritecollide( self, collidable, False )
for p in collision_list:
if ( self.vel_y > 0 ):
self.vel_y = 0
self.grounded = True
In my the update method, I check for collisions between a sprite group holding all of my platforms (parameter collidable) and the player.
Here's my block class:
class Block( pygame.sprite.Sprite ):
def __init__( self, x, y, width, height):
super( Block, self ).__init__()
self.image = pygame.Surface( ( width, height ) )
self.image.fill( black )
self.rect = self.image.get_rect()
self.type = platform
self.rect.x = x
self.rect.y = y
self.width = width
self.height = height
Pretty self-explanatory block class. The rest of my code is to update and blit everything onto a white background. The problem I run into is that when the player lands on a platform, it only stops falling (becomes grounded) when it is already in the platform. Even stranger, the depth the block sinks intot he platform is not consistent. Sometimes it will fall in 10 pixels, other ties 20 pixels. Here's a screenshot of the player getting stuck:
The player block is stuck inside the platform block
So this is really baffling me, especially since the amount the block falls in is inconsistent. If anyone could give me an idea on how to fix this, I'll really appreciate it.
With kindest regards,
Derek
It's hard to diagnose without any debugging trace, but I have an idea or two.
First of all, dump a debugging trace: print the salient values at each time step: position of the player block, positions of colliding blocks, and applicable velocities. This should give you a table-like snapshot at each time step of the involved objects.
Now, look at the values in the last time step before the collision. I suspect that you might have something like a player velocity of -20, but landing on a platform only 10 pixels below. Your update logic needs to handle the landing between the two time steps, and stop the player at the platform height. It looks to me as if the player is allowed to fall for the entire time step, which will embed his tender lower extremities 10 pixels into the platform.
Second, check that you're comparing the nearer edges: in this case, the player's lower edge with the platform's upper edge. You will later need to handle the case where a player's lower-right corner contacts the upper-left corner of a block, and you have to determine whether the player first hits the block's top (landing) or side (change horizontal velocity and drop).
I wouldn't alter the computed velocity; I would think it's easier just to adjust the final position. The last block of code you posted is where you already deal with a collision as a special case. When you detect a collision, get the top surface (y-value) of the object. If that's above the current position of the bottom of the player, assign that top surface as the correct position. This will leave your player sitting on top of whatever is the highest object on the collision list.
Note that this works only as long as your entire collision list is from falling; for lateral collisions as well, you'll need to separate the lists and adjust each boundary as needed. This can get complicated if there are collisions from several sides on the same time step -- for instance, a goombah slams into the player from the left just as the player hits both a wall and a platform.

Method body not executing?

I'm running this python code and having a problem with the accel function. The rotate method works fine when left and right are pressed however when up is pressed nothing happens. I've stepped through the code in a debugger and the my_ship.accel line is executed but it doesn't go to method body, it just continues as if that line isn't there. Idk what's wrong please help. Also my_ship is the name of a Ship object and it is defined properly lower in my code.
import simplegui
WIDTH = 800
HEIGHT = 600
class ImageInfo:
def __init__(self, center, size, radius = 0, lifespan = None, animated = False):
self.center = center
self.size = size
self.radius = radius
if lifespan:
self.lifespan = lifespan
else:
self.lifespan = float('inf')
self.animated = animated
def get_center(self):
return self.center
def get_size(self):
return self.size
def get_radius(self):
return self.radius
def get_lifespan(self):
return self.lifespan
def get_animated(self):
return self.animated
def change_center(self, new_center):
self.center = new_center
# ship image
ship_info = ImageInfo([45, 45], [90, 90], 35)
ship_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/double_ship.png")
class Ship:
def __init__(self, pos, vel, angle, image, info):
self.pos = [pos[0],pos[1]]
self.vel = [vel[0],vel[1]]
self.thrust = False
self.angle = angle
self.angle_vel = 0
self.image = image
self.image_center = info.get_center()
self.image_size = info.get_size()
self.radius = info.get_radius()
self.info = info
self.accel = 10
self.angle_accel = .1
def draw(self,canvas):
if not self.thrust:
self.info.change_center(ship_center)
canvas.draw_image(self.image, self.image_center, self.image_size, self.pos, self.image_size, self.angle)
else:
self.info.change_center(thrust_ship_center)
canvas.draw_image(self.image, self.image_center, self.image_size, self.pos, self.image_size, self.angle)
def update(self):
self.pos[0] += self.vel[0]
self.pos[1] += self.vel[1]
self.angle += self.angle_vel
def accel(self):
self.thrust = True
self.vel[0] += self.accel
self.vel[1] += self.accel
def rotate(self, direction):
if direction == "left":
self.angle_vel -= self.angle_accel
elif direction == "right":
self.angle_vel += self.angle_accel
else:
print "error"
def keydown_handler(key):
if key == simplegui.KEY_MAP['left']:
my_ship.rotate("left")
elif key == simplegui.KEY_MAP['right']:
my_ship.rotate("right")
elif key == simplegui.KEY_MAP['up']:
my_ship.accel
elif key == simplegui.KEY_MAP['space']:
self.angle_vel += self.angle_accel
def keyup_handler(key):
if key == simplegui.KEY_MAP['left']:
my_ship.rotate("right")
elif key == simplegui.KEY_MAP['right']:
my_ship.rotate("left")
my_ship = Ship([WIDTH / 2, HEIGHT / 2], [0, 0], 1, ship_image, ship_info)
This:
my_ship.accel
Doesn't call the method my_ship.accel, any more than 2 calls the number 2. To call something in Python, you need parentheses. So:
my_ship.accel()
(If you're wondering why Python does it this way when other languages, like Ruby, don't… well, this means that you can use the method object my_ship.accel as a value—store it to call later, pass it to map, etc.)
But you've got another problem on top of that.
You define a method accel on Ship objects. But you also assign an integer value 10 to self.accel on Ship objects. There's no way self.accel can mean two different things at once, both the method and the number. So, which one "wins"? In this case, the self.accel = 10 happens at the time you constructed your Ship, which is later, so it wins.
So, when you write my_ship.accel, you're just referring to the number 10. And when you write my_ship.accel(), you're trying to call the number 10 as if it were a function. Hence the TypeError.
The solution is to not reuse the same name for two different things. Often, naming functions after verbs and attributes after nouns is a good way to avoid this problem—although you also have to avoid gratuitous abbreviations, because otherwise you're probably going to abbreviate acceleration and accelerate to the same accel, as you did here.

Categories