How to inherit specific variables from one class to another class? - python

I'm making a game in python using the pygame module and I wish to inherit one specific variable from one class to another class, but have no clue how. I have tried super() but it has not worked, however, me being a novice may just mean I did it wrong so please do not discount that as a possibility if it works.
I have listed which variable I wish inherited in the code below as well.
Here's my relevant code below:
Class I want to inherit FROM:
class player():
def __init__(self, x, y, width, height, walkCount):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 15
self.lastDirection = "right" or "left" or "up" or "down" #<--- variable I want to inherit
self.walkCount = walkCount
def drawCharacter(self, window):
dest = (self.x, self.y)
if self.walkCount + 1 >= 30:
self.walkCount = 0
if self.lastDirection == "left":
window.blit(protagL[0], dest)
elif self.lastDirection == "right":
window.blit(protagR[0], dest)
elif self.lastDirection == "up":
window.blit(protagU[0], dest)
elif self.lastDirection == "down":
window.blit(protagD[0], dest)
Class that I want to receive the inheritance:
class projectile(player):
def __init__(self, x, y, direction, travelCount):
self.x = x
self.y = y
self.direction = direction
self.vel = 20 * direction
self.travelCount = travelCount
def drawBullet(self, window):
dest = (self.x, self.y)
if self.travelCount + 1 >= 15:
self.walkCount = 0
if lastDirection == "right" or "left": # <--- I Want to call lastDirection variable here
if self.travelCount < 15:
window.blit(bulletsP[self.travelCount//3], dest) #dest)
self.travelCount += 1
self.x += self.vel
Any help would be greatly appreciated, thank you.

You have to invoke the constructor of the base class. See super():
class projectile(player):
def __init__(self, x, y, direction, travelCount):
super().__init__(x, y, 10, 10, travelCount)
# [...]
Note the arguments for width and height are unclear in you example, so I've exemplarily used (10, 10).

Related

Is there a way to move an object in pygame in random directions smoothly? [duplicate]

This question already has an answer here:
Pygame game help: Easing/Acceleration
(1 answer)
Closed 2 years ago.
I'm trying to make blobs move in a random direction for several frames rather than just once so that it appears less jerky and more smooth, but have been unable to do so. Is there any way to make each object move in the same direction for several ticks before choosing another random direction and doing the same?
My code (most is irrelevant):
import pygame
import random
import numpy as np
WIDTH = 1800
HEIGHT = 1000
BLUE = (15,15,180)
RED = (150,0,0)
class Blob:
def __init__(self, colour, x_boundary, y_boundary, size):
self.colour = colour
self.size = size
self.x_boundary = x_boundary
self.y_boundary = y_boundary
self.x = random.randrange(0, self.x_boundary)
self.y = random.randrange(0, self.y_boundary)
def move(self):
self.x += random.randrange(-6,7)
self.y += random.randrange(-6,7)
def limits(self):
if self.x < 0:
self.x = 0
elif self.x > self.x_boundary:
self.x = self.x_boundary
if self.y < 0:
self.y = 0
elif self.y > self.y_boundary:
self.y = self.y_boundary
def __add__(self, other_blob):
if other_blob.size > self.size:
other_blob.size += int(self.size * 0.5)
self.size = 0
class FastBlob(Blob):
def __init__(self, colour, x_boundary, y_boundary, size):
super().__init__(colour, x_boundary, y_boundary, size)
def move(self):
self.x += random.randrange(-20,21)
self.y += random.randrange(-20,21)
pygame.init()
game_display = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Blob world')
clock = pygame.time.Clock()
def is_touching(b1,b2):
return np.linalg.norm(np.array([b1.x,b1.y])-np.array([b2.x,b2.y])) < (b1.size + b2.size)
def handle_collisions(blob_list):
blues, reds, slow_reds = blob_list
for first_blobs in blues, reds, slow_reds:
for first_blob_id, first_blob in first_blobs.copy().items():
for other_blobs in blues, reds, slow_reds:
for other_blob_id, other_blob in other_blobs.copy().items():
if first_blob == other_blob:
pass
else:
if is_touching(first_blob, other_blob):
first_blob + other_blob
return blues, reds, slow_reds
def draw_environment(blob_list):
game_display.fill((210,210,210))
handle_collisions(blob_list)
for blob_dict in blob_list:
for blob_id in blob_dict:
blob = blob_dict[blob_id]
pygame.draw.circle(game_display, blob.colour, [blob.x, blob.y], blob.size)
blob.move()
blob.limits()
pygame.display.update()
def main():
blue_blobs = dict(enumerate([FastBlob(BLUE, WIDTH, HEIGHT, random.randrange(10,15)) for i in range(20)]))
red_blobs = dict(enumerate([FastBlob(RED, WIDTH, HEIGHT, random.randrange(5,10)) for i in range(30)]))
slow_red_blobs = dict(enumerate([Blob(RED, WIDTH, HEIGHT, random.randrange(20,30)) for i in range(5)]))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
draw_environment([blue_blobs, red_blobs, slow_red_blobs])
clock.tick(7)
if __name__ == '__main__':
main()
Here, I have similar problem in my game, when enemy has to randomly change directions so it is unpredictable to the player.
def goblin_move(): #Goblin auto (random) movement
if goblin.x < 0:
goblin.go_right()
elif goblin.x > 500:
goblin.go_left()
else:
if goblin.x > (rnd_1 * win_width) and goblin.move_flag == -1:
goblin.go_left()
goblin.move_flag = -1
else:
goblin.go_right()
goblin.move_flag = 1
if goblin.x > (rnd_2 * win_width):
goblin.move_flag = -1
def set_random(rnd_1, rnd_2): #Random function generator
rnd_1 = round(random.uniform(0, 0.45), 2)
rnd_2 = round(random.uniform(0.65, 0.95), 2)
return rnd_1, rnd_2
And this is how I set it in the main loop:
if round(pg.time.get_ticks()/1000) % 3 == 0: #Calling random function
(rnd_1, rnd_2) = set_random(rnd_1, rnd_2)
Hope you will find it useful.
Use pygame.math.Vector2 to do the computations. Store the coordinates of the blob to a Vector2 and define a maximum distance (self.maxdist), a velocity (self.speed), a random distance (self.dist) a nd a random direction (self.dir). The random direction is a vector with length 1 (Unit vector) and a random angel (rotate()):
class Blob:
def __init__(self, colour, x_boundary, y_boundary, size):
self.colour = colour
self.size = size
self.x_boundary = x_boundary
self.y_boundary = y_boundary
self.x = random.randrange(0, self.x_boundary)
self.y = random.randrange(0, self.y_boundary)
self.pos = pygame.math.Vector2(self.x, self.y)
self.maxdist = 7
self.speed = 1
self.dist = random.randrange(self.maxdist)
self.dir = pygame.math.Vector2(1, 0).rotate(random.randrange(360))
When the blob moves, then scale the direction by the speed and add it to the position (self.pos += self.dir * self.speed). Decrement the distance (self.dist -= self.speed) and update self.x, self.y by the rounded (round) position. If self.dist falls below 0, the create a new random direction and distance:
class Blob:
# [...]
def move(self):
self.pos += self.dir * self.speed
self.dist -= self.speed
self.x, self.y = round(self.pos.x), round(self.pos.y)
if self.dist <= 0:
self.dist = random.randrange(self.maxdist)
self.dir = pygame.math.Vector2(1, 0).rotate(random.randrange(360))
In the method limit you have to ensure that self.pos is in bounds. Finally you have to update self.x, self.y:
class Blob:
# [...]
def limits(self):
if self.pos.x < 0:
self.pos.x = 0
elif self.pos.x > self.x_boundary:
self.pos.x = self.x_boundary
if self.pos.y < 0:
self.pos.y = 0
elif self.pos.y > self.y_boundary:
self.pos.y = self.y_boundary
self.x, self.y = round(self.pos.x), round(self.pos.y)
The class FastBlob does not need its own move method. It is sufficient do define its own self.maxdist and self.speed:
class FastBlob(Blob):
def __init__(self, colour, x_boundary, y_boundary, size):
super().__init__(colour, x_boundary, y_boundary, size)
self.maxdist = 35
self.speed = 5

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.

How can i keep updating variables to class from outside of it?

I'm New to python/pygame classes and I'm making a game.
So my problem is that i have a class that draws spaceships to screen but i would need somehow add spacemove_x,spacemove_y to their x and y.
These spacemove_x,spacemove_y are defined when user moves he's own spaceship
(I use spacemove_x,spacemove_y for updating places for other stuff too like stars because this game is from top down view so when i move my spacecraft other things should get closer/farther away depending what direction my character is going)
So spacemove_x,spacemove_y is always updating it self but how can I give this info to class?
CODE:
import pygame
import random
class BaseClass(pygame.sprite.Sprite):
allsprites = pygame.sprite.Group()
def __init__(self, x, y, width, height,spacemove_x,spacemove_y):
pygame.sprite.Sprite.__init__(self)
BaseClass.allsprites.add(self)
self.shipDefaultUp = pygame.image.load("textures/ships/shipDefault/shipUP.png")
self.shipDefaultRight = pygame.image.load("textures/ships/shipDefault/shipRIGHT.png")
self.shipDefaultDown = pygame.image.load("textures/ships/shipDefault/shipDOWN.png")
self.shipDefaultLeft = pygame.image.load("textures/ships/shipDefault/shipLEFT.png")
self.shipCargo1Up = pygame.image.load("textures/ships/shipCargo1/shipCargo1UP.png")
self.shipCargo1Right = pygame.image.load("textures/ships/shipCargo1/shipCargo1RIGHT.png")
self.shipCargo1Down = pygame.image.load("textures/ships/shipCargo1/shipCargo1DOWN.png")
self.shipCargo1Left = pygame.image.load("textures/ships/shipCargo1/shipCargo1LEFT.png")
self.shipCargo2Up = pygame.image.load("textures/ships/shipCargo2/shipCargo2UP.png")
self.shipCargo2Right = pygame.image.load("textures/ships/shipCargo2/shipCargo2RIGHT.png")
self.shipCargo2Down = pygame.image.load("textures/ships/shipCargo2/shipCargo2DOWN.png")
self.shipCargo2Left = pygame.image.load("textures/ships/shipCargo2/shipCargo2LEFT.png")
self.image = pygame.image.load("textures/ships/shipDefault/shipUP.png")
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.width = width
self.height = height
self.spacemove_x = spacemove_x
self.spacemove_y = spacemove_y
#self.dirrection = random.randrange(1,5)
self.dirrection = 1
self.timer = random.randrange(10,50)
self.speed = random.randrange(2,10)
self.shiptype = 3#random.randrange(1,3)
#shiptypes#
#1 = shipDefault
#2 = shipCargo1
#3 = shipCargo2
self.move1 = ((1),(2),(4))
self.move2 = ((1),(2),(3))
self.move3 = ((2),(3),(4))
self.move4 = ((1),(3),(4))
class spacecraftBOT(BaseClass):
ships = pygame.sprite.Group()
def __init__(self, x, y, width, height,spacemove_x,spacemove_y):
BaseClass.__init__(self, x, y, width, height,spacemove_x,spacemove_y)
spacecraftBOT.ships.add(self)
def motion(self):
#1 = UP
#2 = RIGHT
#3 = DOWN
#4 = LEFT
self.timer -=1
if self.dirrection == 1: ##############SHIP UP
self.rect.y -=self.speed
self.rect.y -=self.spacemove_x
if self.shiptype == 1:
self.image = self.shipDefaultUp
if self.shiptype == 2:
self.image = self.shipCargo1Up
if self.dirrection == 2:################SHIP RIGHT
self.rect.x +=self.speed
self.rect.x +=self.spacemove_x
if self.shiptype == 1:
self.image = self.shipDefaultRight
if self.shiptype == 2:
self.image = self.shipCargo1Right
if self.shiptype == 3:
self.image = self.shipCargo2Right
if self.dirrection == 3: ##############SHIP DOWN
self.rect.y +=self.speed
self.rect.y +=self.spacemove_y
if self.shiptype == 1:
self.image = self.shipDefaultDown
if self.shiptype == 2:
self.image = self.shipCargo1Down
if self.shiptype == 3:
self.image = self.shipCargo2Down
if self.dirrection == 4: ################SHIP LEFT
self.rect.x -=self.speed
self.rect.x -=self.spacemove_x
if self.shiptype == 1:
self.image = self.shipDefaultLeft
if self.shiptype == 2:
self.image = self.shipCargo1Left
if self.shiptype == 3:
self.image = self.shipCargo2Left
if self.dirrection == 5:
print("loitter")
if self.timer < 0:
self.dirrection = random.randrange(1,6)
self.timer = random.randrange(10,50)
This is how I create spacecraft to my game:
spacecraft1 = spacecraftBOT(500,500,100,34,spacemove_x,spacemove_y)
So how can I give this class updated spacemove_x,spacemove_y?
So how can i give this class updated spacemove_x,spacemove_y?
Just assigning them should work:
spacecraft1.spacemove_x = 100
spacecraft1.spacemove_y = 200
BTW, spacecraft1 is an instance of class spacecraftBOT, not a class.
You access instance members with the dot operator. In fact, you already do this with self many times. The name self is not special; it is just a variable which refers to a class instance. Similarly, spacecraft1 is a variable which refers to a class instance. This means you can use the exact same notation:
spacecraft1.spacemove_x = 100
or
spacecraft1.spacemove_x += 50
Or basically anything you want.
Alternatively, you can define a move() method:
class spacecraftBOT(BaseClass):
# ... all of the code you already have ...
def move(self, x, y):
self.spacemove_x = x
self.spacemove_y = y
Now you can call the method with something like
spaceship1.move(100, 50)
Notice that this jumps directly to the given location. If you want to increment the x and y coordinates instead, you just need to implement the correct logic. (Hint: Use += instead of =.)

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.

Using pygame.Surface.blit inside class

I can call my class only one time.
My code is:
class fish:
def __init__(self, x, y, image, speed):
self.x = x
self.y = y
self.image = image
self.speed = speed
def be(self):
screen.blit(self.image, (self.x, self.y))
self.x -= self.speed
if boot.x+36 > self.x and boot.x < self.x+5:
if boot.y+34 > self.y and boot.y < self.y+5:
boot.live -= 1
boot.x = 100
boot.y = 460
fishes = []
fishes.append(fish(900, 300, fish_1, 1))
And when I call 'fish' object inside or outside the game loop(fishes.append(fish(900, 300, fish_1, 1))
I got Error:
TypeError: 'fish' object is not callable
My first guess is that you have a fish variable somewhere down in your code. Rename the class to Fish as well as the constructor call to Fish(900, 300, fish_1, 1) and it should be fine.

Categories