Related
Good evening, my dear stackoverflowers.
I'm creating a game in Pygame. Until now I wasn't really having any problems, but now I would appreciate some help. I'm trying to create function that will automatically generate new level every time I call it (after defeating every enemy in current level to be more specific). For now I'm generating only images that you don't collide with, only to make levels more nice but I will be using the function for generating objects aswell so it's kinda important for me. So the problem is that I need to continuously update the game window obviously but the function will keep generating everything too as you can see in the video bellow. I have no idea how to make level generate only once and make the game to remember all the images generated (theirs x and y positions) and how to keep them displayed until I call the function again when I come to another level. Any ideas?
Here is the video: https://vimeo.com/532903523
Here is the function and everything you need to know for my problem. It may be harder for you to understand because its not in English but translation would be irritaing so I at least translated the most important things in these two functions.
vytvor_level = create_level
sirka_okna = ´width of the game window´
vyska_okna = ´height of the game window´
trava = grass
kytka = flower
aktulizace_okna = ´updating the game window´
okno = window
hrac.animace_hrace = player.player_animation
hrac.animace_dashe = player.dash_animation
def vytvor_level():
for objekt in range(200):
x = random.randint(0, sirka_okna)
y = random.randint(0, vyska_okna)
trava = random.choice(travy)
okno.blit(trava, (x,y))
x = random.randint(0, sirka_okna)
y = random.randint(0, vyska_okna)
kytka = random.choice(kytky)
okno.blit(kytka, (x,y))
def aktulizace_okna():
okno.blit(pozadi, (0,0))
hrac.animace_hrace(okno)
hrac.animace_dashe(okno)
vytvor_level()
py.display.update()
Here is the full code if needed.
import pygame as py
import random
py.init()
sirka_okna = 1000
vyska_okna = 600
okno = py.display.set_mode((sirka_okna, vyska_okna))
py.display.set_caption("Little Hero")
bezi_program = True
# Animace hráče
animaceVpravo = [py.image.load('WR1.png'), py.image.load('WR2.png'), py.image.load('WR3.png'), py.image.load('WR4.png'), py.image.load('WR5.png'), py.image.load('WR6.png')]
animaceVlevo = [py.image.load('WL1.png'), py.image.load('WL2.png'), py.image.load('WL3.png'), py.image.load('WL4.png'), py.image.load('WL5.png'), py.image.load('WL6.png')]
animaceNahoru = [py.image.load('WU1.png'), py.image.load('WU2.png'), py.image.load('WU3.png'), py.image.load('WU4.png'), py.image.load('WU5.png'), py.image.load('WU6.png')]
animaceDolu = [py.image.load('WD1.png'), py.image.load('WD2.png'), py.image.load('WD3.png'), py.image.load('WD4.png'), py.image.load('WD5.png'), py.image.load('WD6.png')]
animaceDash = [py.image.load('DASH1.png'), py.image.load('DASH2.png'), py.image.load('DASH3.png'), py.image.load('DASH4.png'), py.image.load('DASH5.png'), py.image.load('DASH6.png'), py.image.load('DASH7.png')]
animaceStoji = [py.image.load('IDLE1.png'), py.image.load('IDLE2.png'), py.image.load('IDLE3.png'), py.image.load('IDLE4.png'), py.image.load('IDLE5.png'), py.image.load('IDLE6.png')]
# Prostředí
travy = [py.image.load('GRASS1.png'), py.image.load('GRASS2.png'), py.image.load('GRASS3.png'), py.image.load('GRASS4.png'), py.image.load('GRASS5.png'), py.image.load('GRASS6.png')]
kytky = [py.image.load('FLOWERS1.png'), py.image.load('FLOWERS2.png'), py.image.load('FLOWERS3.png'), py.image.load('FLOWERS4.png')]
pozadi = py.image.load('Little_Hero_Pozadi.png')
#------------------------------------------------------
class Hrac:
def __init__(self, x, y, sirka, vyska, rychlost, sila_dashe):
self.x = y
self.y = y
self.sirka = sirka
self.vyska = vyska
self.rychlost = rychlost
self.sila_dashe = sila_dashe
self.jde_vpravo = False
self.jde_vlevo = False
self.jde_nahoru = False
self.jde_dolu = False
self.stoji = True
self.dashuje = False
self.pocet_kroku = 0
self.snimek_dashe = 0
def animace_hrace(self, okno):
if self.pocet_kroku + 1 >= 18:
self.pocet_kroku = 0
if not self.stoji:
if self.jde_vlevo:
okno.blit(animaceVlevo[hrac.pocet_kroku//3], (round(hrac.x),round(hrac.y)))
self.pocet_kroku += 1
elif self.jde_vpravo:
okno.blit(animaceVpravo[hrac.pocet_kroku//3], (round(hrac.x),round(hrac.y)))
self.pocet_kroku += 1
elif hrac.jde_nahoru:
okno.blit(animaceNahoru[hrac.pocet_kroku//3], (round(hrac.x),round(hrac.y)))
self.pocet_kroku += 1
else:
okno.blit(animaceDolu[hrac.pocet_kroku//3], (round(hrac.x),round(hrac.y)))
self.pocet_kroku += 1
else:
okno.blit(animaceStoji[hrac.pocet_kroku//3], (round(hrac.x),round(hrac.y)))
self.pocet_kroku += 1
def animace_dashe(self, okno):
x = self.x + self.sirka
y = self.y + self.vyska * 0.25
if self.snimek_dashe + 1 >= 7:
self.snimek_dashe = 0
if self.dashuje:
self.snimek_dashe += 0.25
okno.blit(animaceDash[int(self.snimek_dashe)], (round(x),round(y)))
def pohyb_hrace(self):
if (klavesa[py.K_a] or klavesa[py.K_LEFT]) and self.x > self.rychlost:
self.jde_vlevo = True
self.jde_vpravo = False
self.jde_nahoru = False
self.jde_dolu = False
self.stoji = False
self.x -= self.rychlost
if (klavesa[py.K_e]) and self.x > self.sila_dashe:
self.dashuje = True
self.x -= self.sila_dashe
if (klavesa[py.K_w] or klavesa[py.K_UP]) and self.y > self.rychlost:
hrac.y -= self.rychlost
if (klavesa[py.K_s] or klavesa[py.K_DOWN]) and self.y > self.rychlost:
hrac.y += self.rychlost
elif (klavesa[py.K_d] or klavesa[py.K_RIGHT]) and self.x < sirka_okna - self.sirka - self.rychlost:
self.jde_vlevo = False
self.jde_vpravo = True
self.jde_nahoru = False
self.jde_dolu = False
self.stoji = False
self.x += self.rychlost
if (klavesa[py.K_e]) and self.x < sirka_okna - self.sila_dashe - self.sirka:
self.dashuje = True
self.x += self.sila_dashe
if (klavesa[py.K_w] or klavesa[py.K_UP]) and self.y > self.rychlost:
hrac.y -= self.rychlost
if (klavesa[py.K_s] or klavesa[py.K_DOWN]) and self.y > self.rychlost:
hrac.y += self.rychlost
elif (klavesa[py.K_w] or klavesa[py.K_UP]) and self.y > self.rychlost:
self.jde_vlevo = False
self.jde_vpravo = False
self.jde_nahoru = True
self.jde_dolu = False
self.stoji = False
self.y -= hrac.rychlost
if (klavesa[py.K_e]) and self.y > self.sila_dashe:
self.dashuje = True
self.y -= self.sila_dashe
elif (klavesa[py.K_s] or klavesa[py.K_DOWN]) and self.y < vyska_okna - self.vyska - self.rychlost:
self.jde_vlevo = False
self.jde_vpravo = False
self.jde_nahoru = False
self.jde_dolu = True
self.stoji = False
self.y += self.rychlost
if (klavesa[py.K_e]) and self.y < vyska_okna - self.sila_dashe - self.sirka:
self.dashuje = True
self.y += self.sila_dashe
else:
self.stoji = True
self.pocet_kroku = 0
self.dashuje = False
self.snimek_dashe = 0
def vytvor_level():
for objekt in range(200):
x = random.randint(0, sirka_okna)
y = random.randint(0, vyska_okna)
trava = random.choice(travy)
okno.blit(trava, (x,y))
x = random.randint(0, sirka_okna)
y = random.randint(0, vyska_okna)
kytka = random.choice(kytky)
okno.blit(kytka, (x,y))
def aktulizace_okna():
okno.blit(pozadi, (0,0))
hrac.animace_hrace(okno)
hrac.animace_dashe(okno)
vytvor_level()
py.display.update()
hrac = Hrac(10, 458, 40, 40, 3, 20)
tik = py.time.Clock()
while bezi_program:
tik.tick(60)
for event in py.event.get():
if event.type == py.QUIT:
bezi_program = False
klavesa = py.key.get_pressed()
hrac.pohyb_hrace()
#if hrac.x < enemy.x + enemy.sirka and hrac.x + hrac.sirka > enemy.x and hrac.y < enemy.y + enemy.vyska and hrac.y + hrac.vyska > enemy.y:
#print("Kolize detekována")
aktulizace_okna()
py.quit()
Ok i managed to do it. I used Pillow to paste all the generated images to my background and then keep displaying that one background.
I'm trying to code a little Jump´n´Run game in tkinter with canvas and so far its working pretty well, but I have a problem that I cant´t wrap my head around.
Look at these three pictures: on the first the collision works fine - I can jump from one paddle to the other.
On the second picture, you can see that whenever I get under the paddle it doesn't fall down and jumping up is also not possible, probably because i have self.y = 0 in the self.brick collision detection. How could I get this working such that even when its under the paddle it bounces off, because that's important, for example when I start to add the second line of paddles.
My Collision code:
def brick_hit(self, pos):
for brick_line in self.bricks:
for brick in brick_line:
brick_pos = self.gamerootCanvas.coords(brick.id)
try:
if pos[3] > brick_pos[1]:
if pos[2] > brick_pos[0] and pos[0] < brick_pos[2]:
return True
except:
continue
return False
My Full code:
def jump_and_run():
gameroot = Toplevel()
gameroot.title("Game Root")
gameroot.resizable(0, 0)
gameroot.wm_attributes("-topmost", 1)
gamerootCanvas = Canvas(gameroot, width=1800, height=800, bd=0, highlightthickness=0)
gameroot_Background = PhotoImage(file="jumpnrunbackground.png")
gamerootCanvas.create_image(500, 250, image=gameroot_Background)
gamerootCanvas.pack()
gamerootCanvas.update()
class Player:
def __init__(self, gamerootCanvas, bricks, color):
self.gamerootCanvas = gamerootCanvas
self.id = gamerootCanvas.create_rectangle(25,25,0,0, fill=color)
self.gamerootCanvas.move(self.id, 5, 650)
self.bricks = bricks
self.x = 0
self.y = 0
self.gravity = 0.1
self.gamerootCanvas_height = gamerootCanvas.winfo_height()
self.gamerootCanvas_width = gamerootCanvas.winfo_width()
self.gamerootCanvas.bind_all("<KeyPress-Right>", self.move_right)
self.gamerootCanvas.bind_all("<KeyRelease-Right>", self.move_right_stop)
self.gamerootCanvas.bind_all("<KeyPress-Left>", self.move_left)
self.gamerootCanvas.bind_all("<KeyRelease-Left>", self.move_left_stop)
self.gamerootCanvas.bind_all("<KeyPress-Up>", self.jump_)
self.gamerootCanvas.bind_all("<KeyRelease-Up>", self.jump_stop)
self.jump_counter = 0
self.move_counter = 0
def move_player(self):
self.gamerootCanvas.move(self.id, self.x, self.y)
pos = self.gamerootCanvas.coords(self.id)
self.y += self.gravity
if pos[0] <= 0:
self.x = 1
elif pos[2] >= self.gamerootCanvas_width:
self.x = -1
elif pos[1] <= 0:
self.y = 1
elif pos[3] >= self.gamerootCanvas_height:
self.y = 0
elif self.brick_hit(pos) == True:
self.y = 0
def move_right(self, evt):
self.x = 2
def move_right_stop(self, evt):
self.x = 0
def move_left(self, evt):
self.x = -2
def move_left_stop(self, evt):
self.x = 0
def jump_(self, evt):
if self.jump_counter < 2:
self.y = -6
self.jump_counter += 2
def jump_stop(self, evt):
self.y = 0
self.jump_counter = 0
def brick_hit(self, pos):
for brick_line in self.bricks:
for brick in brick_line:
brick_pos = self.gamerootCanvas.coords(brick.id)
try:
if pos[3] > brick_pos[1]:
if pos[2] > brick_pos[0] and pos[0] < brick_pos[2]:
return True
except:
continue
return False
class Bricks1:
def __init__(self, gamerootCanvas, color):
self.gamerootCanvas = gamerootCanvas
self.id = gamerootCanvas.create_rectangle(50, 15, 0, 0, fill=color, width=2)
self.gamerootCanvas.move(self.id, 5, 700)
def generate_bricks():
global bricks
bricks = []
for i in range(0, 1):
b = []
for j in range(0, 14):
Bricks_1 = Bricks1(gamerootCanvas, "Blue")
b.append(Bricks_1)
bricks.append(b)
for i in range(0, 1):
for j in range(0, 14):
gamerootCanvas.move(bricks[i][j].id, 158.2 * j, 40 * i)
generate_bricks()
player1 = Player(gamerootCanvas, bricks, "Red")
while True:
gameroot.update_idletasks()
player1.move_player()
gameroot.update()
gameroot.after(5)
play_gameloop_sound()
gameUI.mainloop()
You should offset yourself from the brick to prevent becoming stuck. When resting on a brick, you should also have an altered state to prevent jittering.
Trying to code the classic arcade game 'Pong', I've gotten stuck trying to reset the 'ball' into it's original position after the computer scores.
class Pong:
def __init__(self, width, height, x,y, color, screenw, screenh):
self.width = width
self.height = height
self.x = x
self.y = y
self.point = (self.x,self.y)
self.color = color
self.speed = random.randint(3,5)
self.screenw = screenw
self.screenh = screenh
self.dx = random.choice([-2,-1,1,2])
self.dy = random.choice([1,-1])
self.compscore = 0
self.playerscore = 0
self.score = False
def game_logic(self):
x,y = self.point
x += self.speed*self.dx
y += self.speed*self.dy
if x + self.width >= self.screenw:
self.dx = -1
self.color = GREEN
self.playerpoint()
print(str(self.playerscore)+" : "+str(self.compscore))
if x <= 100:
self.dx = 1
self.color = WHITE
self.comppoint()
print(str(self.playerscore)+" : "+str(self.compscore))
if y + self.height >= self.screenh:
self.dy = -1
self.color = ORANGE
if y <= 0:
self.dy = 1
self.color = SALMON
self.point = (x,y)
return
def resetpong(self):
self.point = (200,200)
self.dx = random.choice([-2,-1,1,2])
self.dy = random.choice([1,-1])
return self.point
def comppoint(self):
self.compscore += 1
print("The computer has scored a point.")
self.resetpong()
return self.compscore
def playerpoint(self):
self.playerscore += 1
print("Nice! You've scored a point.")
self.resetpong()
return self.playerscore
I've created the reset method and no matter where I've put it, whether in an if statement in the game_logic method in my pygame starter, or inside the game_logic in the Pong class. It does work though if I set it to a keybinding?
Am I an idiot?
The function resetpong changes the value of self.point. This function gets called by playerpoint or comppoint. The call to playerpoint or comppoint occurs in the function game_logic. This line at the end of game_logic:
self.point = (x,y)
therefore clobbers the new value of self.point. A similar problem affects the variable self.dx which gets set in game_logic and then clobbered by a call to playerpoint or comppoint.
Change the function game_logic as follows to fix both of these:
def game_logic(self):
x,y = self.point
x += self.speed*self.dx
y += self.speed*self.dy
self.point = x, y # parenthesis not needed here
if x + self.width >= self.screenw:
self.color = GREEN
self.playerpoint()
print(str(self.playerscore)+" : "+str(self.compscore))
elif x <= 100: # elif here: only test this if previous if is false
self.color = WHITE
self.comppoint()
print(str(self.playerscore)+" : "+str(self.compscore))
if y + self.height >= self.screenh:
self.dy = -1
self.color = ORANGE
elif y <= 0: # elif here: only test this if previous if is false
self.dy = 1
self.color = SALMON
# return not needed here
I also recommend removing the variables self.x and self.yfrom the Pong constructor since they are never used. The variable self.point contains those numbers and it's a violation of a basic principle to keep the same information in two different places.
I'm working on a 2D python game project for my CS class, and I've hit a bump, I don't know what the problem is:
The project is a large part of my grade, and up until now I've had an A+
This project is incredibly frustrating
NEW
ok so i've got everything working so far, except for some reason My protaganist() is stuck at the top left corner of the game screen !
Also, i need ideas on how to create a jump action
If anyone could help I would be incredibly grateful!
I am importing a game engine my teacher made available from his book website, but i it is too long for me to add but i will try to add some of it at the bottom
Here is all my code:
import gameEngine
import pygame
import math
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.mixer.init()
sndAtk = pygame.mixer.Sound("OOT_AdultLink_Attack1.wav")
#goal is to create a game
#must have menu to start game
#menu should have a start and quit button.. start runs gaming operations and quit exits program
#sprites for character and enemies and bullets maybe, use one large image and simply move visibiliy
#this saves memory as 1 image is loaded instead of many
"""
class game(gameEngine.scene):
def __init__(self, scene):
self.background()
self.sprites["spawn.gif", "badguys.gif"]
"""
"""
protaganist is our hero sprite
should run left and right, jump left and right
and attack left and right...
I might add in the bow and jump attack
"""
class scrollinggrass(gameEngine.SuperSprite):
def __init__(self, scene):
gameEngine.SuperSprite.__init__(self, scene)
self.setImage("gamebackground.jpg")
self.rect.centerx = 20
self.rect.centery = 500
self.rect = self.image.get_rect()
self.dx = 10
self.dy = 0
self.checkKeys()
def checkKeys(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
print("working")
self.forward(3)
run.play()
if keys[pygame.K_LEFT]:
self.forward(-3)
class hearts(gameEngine.SuperSprite):
def __init__(self, scene):
gameEngine.SuperSprite.__init__(self, scene)
self.setImage("heart.png")
self.setTransparentColor = self.imageMaster.get_at((1,1))
self.imageMaster.set_colorkey(self.setTransparentColor)
self.setPosition((550 , 30))
class badguy(gameEngine.SuperSprite):
def __init__(self, scene):
gameEngine.SuperSprite.__init__(self, scene)
self.setImage("badguy1.png")
self.rect = self.imageMaster.get_rect()
self.health = 2
self.DEAD = 1
self.state = 0
class protaganist(gameEngine.SuperSprite):
def __init__(self, scene):
gameEngine.SuperSprite.__init__(self, scene)
self.imageList = []
self.rect = self.imageMaster.get_rect()
self.STANDING = 0
self.RUNNING = 1
self.ATTACKING = 2
self.JUMPING = 3
self.DEAD = 10
self.imageFrame = 0
self.state = self.STANDING
self.hearts = 1
self.heartPts = self.hearts * 3
self.stats()
self.loadImages()
# self.image = self.imageList[0]
self.checkKeys()
def stats(self):
#sets it up so each heart is essentially 3 hit points
if self.heartPts >= 3:
self.hearts = 1
elif self.heartPts >= 6:
self.hearts = 2
elif self.heartPts == 9:
self.hearts = 3
elif self.heartPts > 9:
self.heartPts = 9
# changes state to dead if hp == 0
if self.heartPts == 0:
self.state = DEAD
def loadImages(self):
self.setPosition((320 , 380))
self.setImage("heroSTANDING.gif")
self.setTransparentColor = self.imageMaster.get_at((1,1))
self.imageMaster.set_colorkey(self.setTransparentColor)
def checkKeys(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
self.state = runRight
self.frame += 1
if self.frame >= len(self.imageList):
self.frame = 1
self.image = self.imageList[self.frame]
# self.image = self.image.get_rect()
# self.rect.center = (320, 240)
if keys[pygame.K_LEFT]:
self.state = 1
while keys[pygame.K_g]:
self.state = Attacking
sndAtk.play()
if self.state == self.DEAD:
self.image = self.deadImgList[0]
self.frame += 1
self.image = self.deadImgList[self.frame]
#self.image = self.image.get_rect()
#self.rect.center = (320, 240)
class game(gameEngine.Scene):
def __init__ (self):
gameEngine.Scene.__init__(self)
pygame.display.set_caption("Link's Mediocre Adventure")
background = pygame.Surface(screen.get_size())
background.fill((0, 0, 0))
screen.blit(background, (0, 0))
pro = protaganist(self)
baddy = badguy(self)
baddy1 = badguy(self)
heart = hearts(self)
grass = scrollinggrass(self)
goodlySprites = self.makeSpriteGroup((grass, pro, heart))
baddySprites = self.makeSpriteGroup((baddy, baddy1))
# self.addSpriteGroup(goodlySprites)
self.addGroup((baddySprites))
clock = pygame.time.Clock()
keepGoing = True
while keepGoing:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
if pro.state == pro.ATTACKING:
if pro.collidesGroup(baddySprites):
baddy.health -= 1
baddy1.health -= 1
if baddy.health == 0:
baddy.reset()
elif baddy1.health == 0:
baddy.reset()
elif pro.state != pro.ATTACKING:
if pro.collideGroup(baddySprites):
pro.heartPts -= 1
goodlySprites.update()
baddySprites.update()
goodlySprites.draw(screen)
baddySprites.draw(screen)
pygame.display.flip()
def main():
game.start()
if __name__ == "__main__":
game()
game engine
class SuperSprite(pygame.sprite.Sprite):
""" An enhanced Sprite class
expects a gameEngine.Scene class as its one parameter
Use methods to change image, direction, speed
Will automatically travel in direction and speed indicated
Automatically rotates to point in indicated direction
Five kinds of boundary collision
"""
def __init__(self, scene):
pygame.sprite.Sprite.__init__(self)
self.scene = scene
self.screen = scene.screen
#create constants
self.WRAP = 0
self.BOUNCE = 1
self.STOP = 2
self.HIDE = 3
self.CONTINUE = 4
#create a default text image as a placeholder
#This will usually be changed by a setImage call
self.font = pygame.font.Font("freesansbold.ttf", 30)
self.imageMaster = self.font.render(">sprite>", True, (0, 0,0), (0xFF, 0xFF, 0xFF))
self.image = self.imageMaster
self.rect = self.image.get_rect()
#create properties
#most will be changed through method calls
self.x = 200
self.y = 200
self.dx = 0
self.dy = 0
self.dir = 0
self.rotation = 0
self.speed = 0
self.maxSpeed = 10
self.minSpeed = -3
self.boundAction = self.WRAP
self.pressed = False
self.oldCenter = (100, 100)
self.states = {}
self.currentState = "default"
def update(self):
self.oldCenter = self.rect.center
self.checkEvents()
self.__rotate()
self.__calcVector()
self.__calcPosition()
self.checkBounds()
self.rect.center = (self.x, self.y)
def checkEvents(self):
""" overwrite this method to add your own event code """
pass
def __rotate(self):
""" PRIVATE METHOD
change visual orientation based on
rotation property.
automatically called in update.
change rotation property directly or with
rotateBy(), setAngle() methods
"""
oldCenter = self.rect.center
self.oldCenter = oldCenter
self.image = pygame.transform.rotate(self.imageMaster, self.rotation)
self.rect = self.image.get_rect()
self.rect.center = oldCenter
def __calcVector(self):
""" calculates dx and dy based on speed, dir
automatically called in update
"""
theta = self.dir / 180.0 * math.pi
self.dx = math.cos(theta) * self.speed
self.dy = math.sin(theta) * self.speed
self.dy *= -1
def __calcPosition(self):
""" calculates the sprites position adding
dx and dy to x and y.
automatically called in update
"""
self.x += self.dx
self.y += self.dy
def checkBounds(self):
""" checks boundary and acts based on
self.BoundAction.
WRAP: wrap around screen (default)
BOUNCE: bounce off screen
STOP: stop at edge of screen
HIDE: move off stage and wait
CONTINUE: keep going at present course and speed
automatically called by update
"""
scrWidth = self.screen.get_width()
scrHeight = self.screen.get_height()
#create variables to simplify checking
offRight = offLeft = offTop = offBottom = offScreen = False
if self.x > scrWidth:
offRight = True
if self.x < 0:
offLeft = True
if self.y > scrHeight:
offBottom = True
if self.y < 0:
offTop = True
if offRight or offLeft or offTop or offBottom:
offScreen = True
if self.boundAction == self.WRAP:
if offRight:
self.x = 0
if offLeft:
self.x = scrWidth
if offBottom:
self.y = 0
if offTop:
self.y = scrHeight
elif self.boundAction == self.BOUNCE:
if offLeft or offRight:
self.dx *= -1
if offTop or offBottom:
self.dy *= -1
self.updateVector()
self.rotation = self.dir
elif self.boundAction == self.STOP:
if offScreen:
self.speed = 0
elif self.boundAction == self.HIDE:
if offScreen:
self.speed = 0
self.setPosition((-1000, -1000))
elif self.boundAction == self.CONTINUE:
pass
else:
# assume it's continue - keep going forever
pass
def setSpeed(self, speed):
""" immediately sets the objects speed to the
given value.
"""
self.speed = speed
def speedUp(self, amount):
""" changes speed by the given amount
Use a negative value to slow down
"""
self.speed += amount
if self.speed < self.minSpeed:
self.speed = self.minSpeed
if self.speed > self.maxSpeed:
self.speed = self.maxSpeed
def setAngle(self, dir):
""" sets both the direction of motion
and visual rotation to the given angle
If you want to set one or the other,
set them directly. Angle measured in degrees
"""
self.dir = dir
self.rotation = dir
def turnBy (self, amt):
""" turn by given number of degrees. Changes
both motion and visual rotation. Positive is
counter-clockwise, negative is clockwise
"""
self.dir += amt
if self.dir > 360:
self.dir = amt
if self.dir < 0:
self.dir = 360 - amt
self.rotation = self.dir
def rotateBy(self, amt):
""" change visual orientation by given
number of degrees. Does not change direction
of travel.
"""
self.rotation += amt
if self.rotation > 360:
self.rotation = amt
if self.rotation < 0:
self.rotation = 360 - amt
def setImage (self, image):
""" loads the given file name as the master image
default setting should be facing east. Image
will be rotated automatically """
self.imageMaster = pygame.image.load(image)
self.imageMaster = self.imageMaster.convert()
def setDX(self, dx):
""" changes dx value and updates vector """
self.dx = dx
self.updateVector()
def addDX(self, amt):
""" adds amt to dx, updates vector """
self.dx += amt
self.updateVector()
def setDY(self, dy):
""" changes dy value and updates vector """
self.dy = dy
self.updateVector()
def addDY(self, amt):
""" adds amt to dy and updates vector """
self.dy += amt
self.updateVector()
def setComponents(self, components):
""" expects (dx, dy) for components
change speed and angle according to dx, dy values """
(self.dx, self.dy) = components
self.updateVector()
def setBoundAction (self, action):
""" sets action for boundary. Values are
self.WRAP (wrap around edge - default)
self.BOUNCE (bounce off screen changing direction)
self.STOP (stop at edge of screen)
self.HIDE (move off-stage and stop)
self.CONTINUE (move on forever)
Any other value allows the sprite to move on forever
"""
self.boundAction = action
def setPosition (self, position):
""" place the sprite directly at the given position
expects an (x, y) tuple
"""
(self.x, self.y) = position
def moveBy (self, vector):
""" move the sprite by the (dx, dy) values in vector
automatically calls checkBounds. Doesn't change
speed or angle settings.
"""
(dx, dy) = vector
self.x += dx
self.y += dy
self.checkBounds()
def forward(self, amt):
""" move amt pixels in the current direction
of travel
"""
#calculate dx dy based on current direction
radians = self.dir * math.pi / 180
dx = amt * math.cos(radians)
dy = amt * math.sin(radians) * -1
self.x += dx
self.y += dy
def addForce(self, amt, angle):
""" apply amt of thrust in angle.
change speed and dir accordingly
add a force straight down to simulate gravity
in rotation direction to simulate spacecraft thrust
in dir direction to accelerate forward
at an angle for retro-rockets, etc.
"""
#calculate dx dy based on angle
radians = angle * math.pi / 180
dx = amt * math.cos(radians)
dy = amt * math.sin(radians) * -1
self.dx += dx
self.dy += dy
self.updateVector()
def updateVector(self):
#calculate new speed and angle based on dx, dy
#call this any time you change dx or dy
self.speed = math.sqrt((self.dx * self.dx) + (self.dy * self.dy))
dy = self.dy * -1
dx = self.dx
radians = math.atan2(dy, dx)
self.dir = radians / math.pi * 180
def setSpeedLimits(self, max, min):
""" determines maximum and minimum
speeds you will allow through
speedUp() method. You can still
directly set any speed you want
with setSpeed() Default values:
max: 10
min: -3
"""
self.maxSpeed = max
self.minSpeed = min
def dataTrace(self):
""" utility method for debugging
print major properties
extend to add your own properties
"""
print "x: %d, y: %d, speed: %.2f, dir: %.f, dx: %.2f, dy: %.2f" % \
(self.x, self.y, self.speed, self.dir, self.dx, self.dy)
def mouseDown(self):
""" boolean function. Returns True if the mouse is
clicked over the sprite, False otherwise
"""
self.pressed = False
if pygame.mouse.get_pressed() == (1, 0, 0):
if self.rect.collidepoint(pygame.mouse.get_pos()):
self.pressed = True
return self.pressed
def clicked(self):
""" Boolean function. Returns True only if mouse
is pressed and released over sprite
"""
released = False
if self.pressed:
if pygame.mouse.get_pressed() == (0, 0, 0):
if self.rect.collidepoint(pygame.mouse.get_pos()):
released = True
return released
def collidesWith(self, target):
""" boolean function. Returns True if the sprite
is currently colliding with the target sprite,
False otherwise
"""
collision = False
if self.rect.colliderect(target.rect):
collision = True
return collision
def collidesGroup(self, target):
""" wrapper for pygame.sprite.collideany
simplifies checking sprite - group collisions
returns result of collision check (sprite from group
that was hit or None)
"""
collision = pygame.sprite.spritecollideany(self, target)
return collision
def distanceTo(self, point):
""" returns distance to any point in pixels
can be used in circular collision detection
"""
(pointx, pointy) = point
dx = self.x - pointx
dy = self.y - pointy
dist = math.sqrt((dx * dx) + (dy * dy))
return dist
def dirTo(self, point):
""" returns direction (in degrees) to
a point """
(pointx, pointy) = point
dx = self.x - pointx
dy = self.y - pointy
dy *= -1
radians = math.atan2(dy, dx)
dir = radians * 180 / math.pi
dir += 180
return dir
def drawTrace(self, color=(0x00, 0x00, 0x00)):
""" traces a line between previous position
and current position of object
"""
pygame.draw.line(self.scene.background, color, self.oldCenter,
self.rect.center, 3)
self.screen.blit(self.scene.background, (0, 0))
def addState(self, stateName, stateImageFile):
""" Creates a new sprite state with the associated name
and image. Useful to build multi-state sprites.
"""
#load the image
tempImage = pygame.image.load(stateImageFile)
tempImage.convert()
self.states[stateName] = tempImage
def setState(self, stateName):
""" attempts to set the sprite to the indicated state
(image)
"""
self.imageMaster = self.states[stateName]
self.rect = self.imageMaster.get_rect()
self.currentState = stateName
def getState(self):
""" returns the current state name
(default if no states have been explicitly set)
"""
return self.currentState
if pro.state == pro.ATTACKING:
if pro.collidesWith(baddySprites):
baddy.health -= 1
if baddy.health == 0:
baddy.reset()
elif pro.state != pro.ATTACKING:
if pro.collidesWith(baddySprites):
pro.heartPts -= 1
baddySprites is a sprite group, so I bet you have to use collidesGroup instead of collidesWith.
if pro.state == pro.ATTACKING:
if pro.collidesGroup(baddySprites):
baddy.health -= 1
if baddy.health == 0:
baddy.reset()
elif pro.state != pro.ATTACKING:
if pro.collidesGroup(baddySprites):
pro.heartPts -= 1
But even if you do this, it seems like you'll still have problems. Namely, this code only ever deducts health from baddy and not baddy1. I'm assuming sprite groups support iteration. If so, you should perform collision detection independently on each enemy.
for enemy in baddySprites:
if pro.state == pro.ATTACKING:
if pro.collidesWith(enemy):
enemy.health -= 1
if enemy.health == 0:
enemy.reset()
elif pro.state != pro.ATTACKING:
if pro.collidesWith(enemy):
pro.heartPts -= 1
When I was programing a basic UFO game in python 2.7 and with pygame I came accross an error, However it didn't come up in the consol. What happend was:
I was programing
Made a little change in the code
The Menu Screen worked fine and I go to play the game
It then keeps restarting.
I edited a few more bits of code so it'd be in a while loop until it would call a command correctly.
And It still wouldent call the command.
If you want to have the Images you can download them and the Sorcecode here:
http://dl.dropbox.com/u/33487921/A%20Problem.zip
Here is the code:
import sys, pygame, random
from pygame.locals import *
from time import gmtime, strftime
pygame.init()
width = int(500*2)
height = int(300*2)
width2 = width + (width/5)
height2 = height + ((height/3)/2)
screen = pygame.display.set_mode((width, height2))
pygame.display.set_caption("MoeTM's UFO Game")
class FlyingObject():
def Restart(self):
self.amount = 0
self.array = []
def New(self, FPS):
self.amount += 1
for i in range(self.amount):
if i == self.amount-1:
self.array.append([])
Strength = random.randrange(1, 101, 1)
if Strength in range(1, 51, 1):
Type = 1
if Strength in range(51, 76, 1):
Type = 2
if Strength in range(76, 88, 1):
Type = 3
if Strength in range(88, 94, 1):
Type = 4
if Strength in range(94, 101, 1):
Type = 5
X = random.randrange(0,1,1)
if X == 0:
XMove = random.randrange(1,6,1)
if X == 1:
XMove = random.randrange(1,6,1)
XMove /= 5.0
Y = random.randrange(0,2,1)
if Y == 0:
YMove = random.randrange(1,6,1)
if Y == 1:
YMove = random.randrange(1,6,1)
YMove /= 5.0
XMove *= 10
YMove *= 10
XMove *= FPS
YMove *= FPS
TLBR = random.randrange(0,4,1)
if TLBR == 0:#top
XIntercept = random.randrange(0,width+1,1)
YIntercept = -5
if TLBR == 1:#left
XIntercept = -5
YIntercept = random.randrange(0,height+1,1)
if TLBR == 2:#bottom
XIntercept = random.randrange(0,width+1,1)
YIntercept = height + 5
if TLBR == 3:#right
XIntercept = width + 5
YIntercept = random.randrange(0,height+1,1)
if XIntercept in range((width/4)*3,width+1,1):
XMove = -XMove
if YIntercept in range((height/4)*3,height+1,1):
YMove = -YMove
if XIntercept in range((width/4),(width/4)*3, 1):
chance = random.randrange(0,2,1)
if chance == 1:
XMove=-XMove
if YIntercept in range((height/4),(height/4)*3, 1):
chance = random.randrange(0,2,1)
if chance == 1:
YMove=-YMove
if XMove < 1 and YMove >= 1:
XMove *= 3
YMove *= 3
if YMove < 1 and XMove >= 1:
XMove *= 3
YMove *= 3
if YMove < 1 and XMove <1:
XMove *= 3
YMove *= 3
self.array[i] = [XMove,YMove,XIntercept,YIntercept,False,False, Type, "image"]
def Move(self, PX, PY, IX, IY):
for i in range(self.amount):
self.array.append([])
if self.array[i][2] > width +10 or self.array[i][2] < -10:
self.array[i][4] = False
self.array[i][5] = True
if self.array[i][3] > height +10 or self.array[i][3] < -10:
self.array[i][4] = False
self.array[i][5] = True
Check = True
if self.array[i][4] == False and self.array[i][5] == True:
Check = False
if Check == True:
self.array[i][2] += self.array[i][0]#XIntercept+=XMove
self.array[i][3] += self.array[i][1]#YIntercept+=Ymove
if self.array[i][2] in range(0,width+1,1) and self.array[i][3] in range(0,height+1,1):
self.array[i][4] = True
self.array[i][5] = True
else:
self.array[i][4] = False
if int(self.array[i][2]+5) in range(PX, PX+IX, 1) and int(self.array[i][3]+5) in range(PY, PY+IY, 1):
self.colide(self.array[i][6])
self.array[i][4] = False
self.array[i][5] = True
def Blit(self):
for i in range(self.amount):
self.array.append([])
Check = True
if self.array[i][4] == False and self.array[i][5] == True:
Check = False
if Check == True:
screen.blit(self.array[i][7], (int(self.array[i][2]), int(self.array[i][3])))
class Bullits(FlyingObject):
def colide(self, damage=1):
Play.Game.Player.Hit(damage)
def Convert(self):
for i in range(self.amount):
self.array.append([])
self.array[i][7]=self.Image = pygame.image.load("images\Bullit"+str(self.array[i][6])+".png")
class Orbs(FlyingObject):
def colide(self, ammount=1):
Play.Game.Player.GotPoint(ammount)
def Convert(self):
for i in range(self.amount):
self.array.append([])
self.array[i][7]=self.Image = pygame.image.load("images\Orb"+str(self.array[i][6])+".png")
class UFO():
def __init__(self):
self.health = 10
self.maxhealth = 50
self.startinghealth = 10
self.maxspeed = 20
self.Image = pygame.image.load("images\UFO.png")
self.Heart0 = pygame.transform.scale(pygame.image.load("images\Heart0.png"), (width/50,height/30))
self.Heart1 = pygame.transform.scale(pygame.image.load("images\Heart1.png"), (width/50,height/30))
self.Heart2 = pygame.transform.scale(pygame.image.load("images\Heart2.png"), (width/50,height/30))
self.Heart3 = pygame.transform.scale(pygame.image.load("images\Heart3.png"), (width/50,height/30))
self.Heart4 = pygame.transform.scale(pygame.image.load("images\Heart4.png"), (width/50,height/30))
self.Heart5 =pygame.transform.scale( pygame.image.load("images\Heart5.png"), (width/50,height/30))
self.ix = 100
self.iy = 40
self.points = 0
self.collection = 0
self.x = (width-self.ix)/2
self.y = (height-self.iy)/2
self.HeartCol= []
self.DA = "New"
for i in range(0,10,1):
self.HeartCol.append([])
dif = int((width/2.5)/10)
higt = int(((height2-height)/2)+height)
self.HeartCol[i] = [dif*i, higt]
def New(self):
print "Starting"
self.health = self.startinghealth
self.x = (width-self.ix)/2
self.y = (height-self.iy)/2
self.DA = "Alive"
print Alive
def Hit(self, damage=1):
self.health -= damage
if self.health <= 0:
self.DA = "Dead"
def GotPoint(self, amount=1):
self.points += amount
if self.points >= 10:
self.points -= 10
self.collection += 1
self.health += 1
if self.health >= self.maxhealth:
self.health = self.maxhealth
def Move(self,mx,my):
if mx >= width - (self.ix/2):
mx = width - self.ix
elif mx <= self.ix/2:
mx = 0
else:
mx = mx - (self.ix/2)
if my >= height - (self.iy/2):
my = height - self.iy
elif my <= self.iy/2:
my = 0
else:
my = my - (self.iy/2)
if mx > self.x:
if mx-self.x > self.maxspeed:
self.x += self.maxspeed
else:
self.x = mx
if mx < self.x:
if self.x-mx > self.maxspeed:
self.x -= self.maxspeed
else:
self.x = mx
if my > self.y:
if my-self.y > self.maxspeed:
self.y += self.maxspeed
else:
self.y = my
if my < self.y:
if self.y-my > self.maxspeed:
self.y -= self.maxspeed
else:
self.y = my
def Blit(self):
screen.blit(self.Image, (self.x, self.y))
def ForBlit(self):
return self.x, self.y, self. ix, self.iy
def Toolbar(self):
pygame.draw.rect(screen, [127,127,127], pygame.Rect(0, height, width, height2))
for i in range(0,10,1):
if self.health >= 41+i:
screen.blit(self.Heart5, (self.HeartCol[i][0], self.HeartCol[i][1]))
elif self.health >= 31+i:
screen.blit(self.Heart4, (self.HeartCol[i][0], self.HeartCol[i][1]))
elif self.health >= 21+i:
screen.blit(self.Heart3, (self.HeartCol[i][0], self.HeartCol[i][1]))
elif self.health >= 11+i:
screen.blit(self.Heart2, (self.HeartCol[i][0], self.HeartCol[i][1]))
elif self.health >= 1+i:
screen.blit(self.Heart1, (self.HeartCol[i][0], self.HeartCol[i][1]))
else:
screen.blit(self.Heart0, (self.HeartCol[i][0], self.HeartCol[i][1]))
def DOA(self):
return self.DA
def New(self):
pass
class Background():
def __init__(self):
self.sr = 5
self.bg = [0,0,0]
def New(self):
self.amountOfStars = random.randrange(10,51,1)
self.stars=[]
for i in range(self.amountOfStars):
x = random.randrange(self.sr*2,width-(self.sr*2)+1,1)
y = random.randrange(self.sr*2,height-(self.sr*2)+1,1)
coulerr = random.randrange(200,256,1)
coulerg = random.randrange(200,256,1)
coulerb = random.randrange(0,56,1)
size = random.randrange(5, 10, 1)
self.stars.append([])
self.stars[i] = [[x,y],[coulerr,coulerg,coulerb],size]
def Blit(self):
screen.fill(self.bg)
for i in range(self.amountOfStars):
self.stars.append([])
pygame.draw.circle(screen,self.stars[i][1],self.stars[i][0],self.stars[i][2])
class Text():
def Text(self, Text, TextSize, Couler):
self.Couler = Couler
self.TextSize = int(TextSize)
self.Text = str(Text)
self.font = pygame.font.Font(None, self.TextSize)
self.text = self.font.render(self.Text, 1, self.Couler)
self.Size = self.text.get_rect()
self.ctext = self.text
self.cCouler = self.Couler
def Position(self, X, Y):
self.X = X
self.Y = Y
self.Position = self.X,self.Y
def Blit(self):
screen.blit(self.ctext, [self.X,self.Y])
def IsItClicked(self, MouseX, MouseY):
if MouseX in range(self.X,self.X+self.Size[2]+1,1) and MouseY in range(self.Y,self.Y+self.Size[3]+1,1):
return True
else:
return False
def Hover(self, MouseX=0, MouseY=0, couler=-1):
if couler == -1:
couler = self.Couler
hover = self.IsItClicked(MouseX, MouseY)
if hover == True:
self.cCouler = couler
self.ctext = self.font.render(self.Text, 1, self.cCouler)
else:
self.cCouler = self.Couler
self.ctext = self.font.render(self.Text, 1, self.cCouler)
class Menu():
def __init__(self):
self.Move = (height2/4)/2
self.move = self.Move*2
self.Menu = Text()
self.Menu.Text("MoeTM's UFO Game", 50, [255,255,0])
self.Menu.Position((width - self.Menu.Size[2])/2,self.Menu.Size[0])
self.Play = Text()
self.Play.Text("Play The Game!", 30, [255,255,0])
self.Play.Position(self.Play.Size[0]+20,self.Play.Size[0]+self.Move)
self.Shop = Text()
self.Shop.Text("Enter the Shop, Why?", 30, [255,255,0])
self.Shop.Position(self.Shop.Size[0]+20,self.Shop.Size[0]+self.move*2-self.Move)
self.Saves = Text()
self.Saves.Text("Save the game, ;P", 30, [255,255,0])
self.Saves.Position(self.Saves.Size[0]+20,self.Saves.Size[0]+self.move*3-self.Move)
self.Options = Text()
self.Options.Text("Options...", 30, [255,255,0])
self.Options.Position(self.Options.Size[0]+20,self.Options.Size[0]+self.move*4-self.Move)
self.Area = "Menu"
def Blit(self, MouseX=0, MouseY=0):
if self.Area == "Menu":
self.Play.Hover(MouseX, MouseY, [192,255,0])
self.Shop.Hover(MouseX, MouseY, [192,255,0])
self.Saves.Hover(MouseX, MouseY, [192,255,0])
self.Options.Hover(MouseX, MouseY, [192,255,0])
self.Play.Blit()
self.Shop.Blit()
self.Saves.Blit()
self.Options.Blit()
if self.Area == "Shop":
pass
if self.Area == "Saves":
pass
if self.Area == "Options":
pass
if self.Area != "Play":
self.Menu.Hover(MouseX, MouseY, [192,255,0])
self.Menu.Blit()
def IsClicked(self, MouseX, MouseY):
if self.Area == "Menu":
play = self.Play.IsItClicked(MouseX, MouseY)
if play == True:
self.Area = "Play"
Shop = self.Shop.IsItClicked(MouseX, MouseY)
if Shop == True:
self.Area = "Shop"
Saves = self.Saves.IsItClicked(MouseX, MouseY)
if Saves == True:
self.Area = "Saves"
Options = self.Options.IsItClicked(MouseX, MouseY)
if Options == True:
self.Area = "Options"
menu = self.Menu.IsItClicked(MouseX, MouseY)
if menu == True:
self.Area = "Menu"
def getArea(self):
return self.Area
class Game():
def __init__(self):
self.Player = UFO()
self.Background = Background()
self.Orbs = Orbs()
self.Bullits = Bullits()
self.Death = pygame.image.load("images\Death.png")
self.Death = pygame.transform.scale(self.Death, (width2, height2))
self.death = 0
def New(self):
while True:
self.Player.New()
Place = self.Player.DOA()
if Place != "New":
self.TotalTime = 0
self.Background.New()
self.Bullits.Restart()
self.Orbs.Restart()
def Blit(self):
self.Bullits.Convert()
self.Orbs.Convert()
self.Background.Blit()
self.Bullits.Blit()
self.Orbs.Blit()
self.Player.Blit()
def Move(self, MX, MY):
self.Player.Move(MX, MY)
PX, PY, IX, IY = self.Player.ForBlit()
self.Bullits.Move(PX, PY, IX, IY)
self.Orbs.Move(PX, PY, IX, IY)
def Updater(self, time):
self.TotalTime += time
def Death(self):
if self.death >= 1: return 0
self.death += 1
time = float(self.TotalTime) * 10
time = int(time)
time /= 10.0
time = str(time)
self.DeadText = Text()
text = "You died in "+time+" seconds"
self.DeadText.Text(text,50,[0,0,0])
self.DeadText.Position((width-self.DeadText.Size[2])/2,(height2-self.DeadText.Size[3])/2)
def DeadBlit(self):
screen.blit(self.Death, (0, 0))
self.DeadText.Blit()
class Main():
def __init__(self):
self.Game = Game()
self.Menu = Menu()
self.DA= ""
self.seconds = 0
self.clock = pygame.time.Clock()
self.FPS = 0.1
self.QUIT = False
self.Place = ["","New"]
self.difficalty1 = 10
self.difficalty2 = 5
def Main(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.QUIT = True
if event.type == KEYDOWN:
if event.key == K_p:
time = strftime("%a, %d %b %Y %H:%M:%S", gmtime())
pygame.image.save(screen, time+".png")
#if event.key == K_DOWN:
#if event.key == K_UP:
if event.type == MOUSEBUTTONDOWN:
self.Menu.IsClicked(MouseX, MouseY)
if self.QUIT == True:
pygame.quit()
break
self.seconds += self.clock.tick()/1000.0
if self.seconds >= self.FPS:
MouseX,MouseY = pygame.mouse.get_pos()
self.Place[0] = self.Menu.getArea()
if self.Place[0] == "Play" and self.Place[1] == "New":
self.Game.New()
screen.fill([0,0,0])
if self.Place[0] != "Play":
self.Menu.Blit(MouseX, MouseY)
if self.Place[0] == "Play":
if self.Place[1] != "Dead":
self.Game.Move(MouseX,MouseY)
self.Game.Updater(self.seconds)
randomChance = random.randrange(0,self.difficalty1,1)
if randomChance in range(0, self.difficalty2, 1):
self.Game.Orbs.New(self.FPS)
randomChance = random.randrange(0,self.difficalty1,1)
if randomChance in range(0, self.difficalty2, 1):
self.Game.Bullits.New(self.FPS)
self.Game.Blit()
self.Place[1] = self.Game.Player.DOA()
if self.Place[1] == "Dead":
self.Game.Death()
self.Game.DeadBlit()
pygame.display.flip()
self.seconds -= self.FPS
Play = Main()
Play.Main()
pygame.quit()
here is the two bits of code that have broke. (I put the class abouve the def's so you know what class there in)
class UFO():
def New(self):
print "Starting"
self.health = self.startinghealth
self.x = (width-self.ix)/2
self.y = (height-self.iy)/2
self.DA = "Alive"
print Alive
class Game():
def New(self):
while True:
self.Player.New()
Place = self.Player.DOA()
if Place != "New":
self.TotalTime = 0
self.Background.New()
self.Bullits.Restart()
self.Orbs.Restart()
if you use the code in IDLE then you will know that it dousent even call self.Player.New() because the Shell dousn't print "Starting" or give an error for me putting print Alive, it just continuosly loops the while loop.
Sorry for the Miles of code, :( But Hopefully it helps, mostly because without some of it you wouldent understand it. Also Thankyou for taking the time to read and try to answer this. And Sorry for all the grammer and spalling mistakes, I can't spell.
Your issues is the While True in your Game.New method, also i have found other issues that make the game to not run.
Below is the diff with the fixed code:
--- ../A Problem-1/Game.pyw 2012-01-25 13:32:00.000000000 -0300
+++ Game.pyw 2012-01-25 11:55:56.000000000 -0300
## -11,6 +11,9 ##
pygame.display.set_caption("MoeTM's UFO Game")
class FlyingObject():
+ def __init__(self):
+ self.amount = 0
+ self.array = []
def Restart(self):
self.amount = 0
self.array = []
## -233,6 +236,7 ##
def __init__(self):
self.sr = 5
self.bg = [0,0,0]
+ self.amountOfStars = 0
def New(self):
self.amountOfStars = random.randrange(10,51,1)
self.stars=[]
## -352,15 +356,15 ##
self.Death = pygame.image.load("images\Death.png")
self.Death = pygame.transform.scale(self.Death, (width2, height2))
self.death = 0
+ self.TotalTime = 0
def New(self):
- while True:
- self.Player.New()
- Place = self.Player.DOA()
- if Place != "New":
- self.TotalTime = 0
- self.Background.New()
- self.Bullits.Restart()
- self.Orbs.Restart()
+ self.Player.New()
+ Place = self.Player.DOA()
+ if Place != "New":
+ self.TotalTime = 0
+ self.Background.New()
+ self.Bullits.Restart()
+ self.Orbs.Restart()
def Blit(self):
self.Bullits.Convert()
self.Orbs.Convert()