Quick debugging question [Python, pygame] - python

It's still an incomplete program, but for some reason the value of the textbox doesn't increase when it should... Why is this??
When the Pizza sprite overlaps with the Pan sprite, the score in the textbox is supposed to increase in value by 10. Why does this not occur?
Thanks!
'''
Created on Jul 1, 2011
#author: ******* Louis
'''
#Watch me do.
from livewires import games, color
import random
games.init (screen_width = 640, screen_height = 480, fps = 50)
#Pizza Class
class Pizza (games.Sprite):
pizzaimage = games.load_image ("pizza.bmp", transparent = True)
def __init__(self, x = random.randrange(640), y = 90, dy = 4):
super (Pizza, self).__init__(x = x,
y = y,
image = Pizza.pizzaimage,
dy = dy)
def handle_caught (self):
self.destroy()
class Pan (games.Sprite):
panimage = games.load_image ("pan.bmp", transparent = True)
def __init__ (self, x = games.mouse.x, y = games.mouse.y):
super (Pan, self).__init__(x = x,
y = y,
image = Pan.panimage)
self.score = 0
self.textbox = games.Text (value = str(self.score),
size = 20,
color = color.black,
x = 550,
y = 50)
games.screen.add(self.textbox)
def update (self): #WWWWOW There is actually an *update* method
self.x = games.mouse.x
self.y = games.mouse.y
if self.left < 0:
self.left = 0
if self.right >640:
self.right = 640
if self.top < 0:
self.top = 0
if self.bottom > 480:
self.bottom = 480
self.check_collision()
def check_collision (self):
for Pizza in self.overlapping_sprites:
self.score = self.score + 10
Pizza.handle_caught()
#main
def main():
wallbackground = games.load_image ("wall.jpg", transparent = False)
games.screen.background = wallbackground
games.screen.add(Pizza())
games.screen.add(Pan())
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
main()

The textbox takes a value that is a string. When you create the textbox, you create a string from the current value of score, and set the text to that string. No lasting connection between score and textbox is made.
The textbox probably has a method available to update its text; call that method with the value str(self.score) after you increment the score.

Related

How do I use a .jpg or .png as a background picture?

I found a nice image of space that I'd like sitting in the background of this tiny game I'm working on and can't figure out what and where to write it. It needs to be placed behind all classes to make sure that it doesn't block the screen. I thought it might be in class Window, but I'm not sure. I am brand new to python so any help is much appreciated! This is the entire project so far.
import sys, logging, os, random, math, open_color, arcade
#check to make sure we are running the right version of Python
version = (3,7)
assert sys.version_info >= version, "This script requires at least Python {0}.{1}".format(version[0],version[1])
#turn on logging, in case we have to leave ourselves debugging messages
logging.basicConfig(format='[%(filename)s:%(lineno)d] %(message)s', level=logging.DEBUG)
logger = logging.getLogger(__name__)
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
MARGIN = 30
SCREEN_TITLE = "Intergalactic slam"
NUM_ENEMIES = 5
STARTING_LOCATION = (400,100)
BULLET_DAMAGE = 10
ENEMY_HP = 10
HIT_SCORE = 10
KILL_SCORE = 100
PLAYER_HP = 100
class Bullet(arcade.Sprite):
def __init__(self, position, velocity, damage):
'''
initializes the bullet
Parameters: position: (x,y) tuple
velocity: (dx, dy) tuple
damage: int (or float)
'''
super().__init__("PNG/laserPink3.png", 0.5)
(self.center_x, self.center_y) = position
(self.dx, self.dy) = velocity
self.damage = damage
def update(self):
'''
Moves the bullet
'''
self.center_x += self.dx
self.center_y += self.dy
class Enemy_Bullet(arcade.Sprite):
def __init__(self, position, velocity, damage):
super().__init__("PNG/laserGreen1.png", 0.5)
(self.center_x, self.center_y) = position
(self.dx, self.dy) = velocity
self.damage = damage
def update(self):
self.center_x += self.dx
self.center_y += self.dy
class Player(arcade.Sprite):
def __init__(self):
super().__init__("PNG/shipYellow_manned.png", 0.5)
(self.center_x, self.center_y) = STARTING_LOCATION
self.hp = PLAYER_HP
class Enemy(arcade.Sprite):
def __init__(self, position):
'''
initializes an alien enemy
Parameter: position: (x,y) tuple
'''
super().__init__("PNG/shipGreen_manned.png", 0.5)
self.hp = ENEMY_HP
(self.center_x, self.center_y) = position
class Window(arcade.Window):
def __init__(self, width, height, title):
super().__init__(width, height, title)
file_path = os.path.dirname(os.path.abspath(__file__))
os.chdir(file_path)
self.set_mouse_visible(True)
arcade.set_background_color(open_color.black)
self.bullet_list = arcade.SpriteList()
self.enemy_list = arcade.SpriteList()
self.enemy_bullet_list = arcade.SpriteList()
self.player = Player()
self.score = 0
self.win = False
self.lose = False
def setup(self):
'''
Set up enemies
'''
for i in range(NUM_ENEMIES):
x = 120 * (i+1) + 40
y = 500
enemy = Enemy((x,y))
self.enemy_list.append(enemy)
def update(self, delta_time):
self.bullet_list.update()
self.enemy_bullet_list.update()
if (not (self.win or self.lose)):
for e in self.enemy_list:
for b in self.bullet_list:
if (abs(b.center_x - e.center_x) <= e.width / 2 and abs(b.center_y - e.center_y) <= e.height / 2):
self.score += HIT_SCORE
e.hp -= b.damage
b.kill()
if (e.hp <= 0):
e.kill()
self.score += KILL_SCORE
if (len(self.enemy_list) == 0):
self.win = True
if (random.randint(1, 75) == 1):
self.enemy_bullet_list.append(Enemy_Bullet((e.center_x, e.center_y - 15), (0, -10), BULLET_DAMAGE))
for b in self.enemy_bullet_list:
if (abs(b.center_x - self.player.center_x) <= self.player.width / 2 and abs(b.center_y - self.player.center_y) <= self.player.height / 2):
self.player.hp -= b.damage
b.kill()
if (self.player.hp <= 0):
self.lose = True
def on_draw(self):
arcade.start_render()
arcade.draw_text(str(self.score), 20, SCREEN_HEIGHT - 40, open_color.white, 16)
arcade.draw_text("HP: {}".format(self.player.hp), 20, 40, open_color.white, 16)
if (self.player.hp > 0):
self.player.draw()
self.bullet_list.draw()
self.enemy_bullet_list.draw()
self.enemy_list.draw()
if (self.lose):
self.draw_game_loss()
elif (self.win):
self.draw_game_won()
def draw_game_loss(self):
arcade.draw_text(str("LOSER!"), SCREEN_WIDTH / 2 - 90, SCREEN_HEIGHT / 2 - 10, open_color.white, 30)
def draw_game_won(self):
arcade.draw_text(str("WINNER!"), SCREEN_WIDTH / 2 - 90, SCREEN_HEIGHT / 2 - 10, open_color.white, 30)
def on_mouse_motion(self, x, y, dx, dy):
'''
The player moves left and right with the mouse
'''
self.player.center_x = x
def on_mouse_press(self, x, y, button, modifiers):
if button == arcade.MOUSE_BUTTON_LEFT:
x = self.player.center_x
y = self.player.center_y + 15
bullet = Bullet((x,y),(0,10),BULLET_DAMAGE)
self.bullet_list.append(bullet)
def main():
window = Window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
window.setup()
arcade.run()
if __name__ == "__main__":
main()
One way to do it would be to load the .jpg or .png as a texture, and draw that texture each frame, as big as the screen is (or bigger!).
I haven't tested this, but as an example, loading the texture could be done in Window.__init__, like so (reference):
self.background = arcade.load_texture('PNG/background.png')
And then in on_draw, just after you call start_render, you would draw it (reference), passing the required center coordinates, as well as width and height:
self.background.draw(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, SCREEN_WIDTH, SCREEN_HEIGHT)
The reason it needs to be the first thing is because everything is drawn back-to-front, like you would do in a painting.
If the image is not the exact same size as your screen/window, your background will probably be stretched/squished. If that's not what you want, the easiest fix would be to change the image so that it's the right size.
Yes, you should be able to add it to class window...
You could do something like this to add it:
def __init__(self, width, height, title):
super().__init__(width, height, title)
file_path = os.path.dirname(os.path.abspath(__file__))
os.chdir(file_path)
self.set_mouse_visible(True)
arcade.set_background_color(open_color.black)
self.bullet_list = arcade.SpriteList()
self.enemy_list = arcade.SpriteList()
self.enemy_bullet_list = arcade.SpriteList()
self.player = Player()
self.score = 0
self.win = False
self.lose = False
self.background = None
def setup(self):
'''
Set up enemies
'''
self.background = arcade.load_texture("images/background.jpg")
for i in range(NUM_ENEMIES):
x = 120 * (i+1) + 40
y = 500
enemy = Enemy((x,y))
self.enemy_list.append(enemy)

How to fix TypeError: unbound method must be called with Dropper instance as first argument

I am writing a game where a rock falls down and you use the mouse to have your chef not get crushed. When the rock falls off the screen, two additional rocks spawn and fall. This continues until your character gets smooshed.
However I got the error:
TypeError: unbound method additonal_drop() must be called with Dropper instance as first argument (got nothing instead)
I'm not sure what I should be putting in the (). Can someone explain this error to me?
In addition, how do I get the Dropper sprite to not be visible?
Here's my code:
from livewires import games, color
import random
games.init(screen_width = 640, screen_height = 480, fps = 50)
class Chef(games.Sprite):
image = games.load_image("chef.bmp")
def __init__(self):
super(Chef, self).__init__(image = Chef.image,
x = games.mouse.x,
bottom = games.screen.height)
def update(self):
""" Move to mouse x position. """
self.x = games.mouse.x
if self.left < 0:
self.left = 0
if self.right > games.screen.width:
self.right = games.screen.width
self.check()
def check(self):
""" Check if hit by rocks. """
for rock in self.overlapping_sprites:
rock.end_game()
class Rock(games.Sprite):
"""
A rock which falls to the ground.
"""
image = games.load_image("rock.bmp")
speed = 1
def __init__(self, x = 320, y = 90):
""" Initialize a rock object. """
super(Rock, self).__init__(image = Rock.image,
x = x, y = y,
dy = Rock.speed)
def end_game(self):
""" End the game. """
end_message = games.Message(value = "Game Over",
size = 90,
color = color.red,
x = games.screen.width/2,
y = games.screen.height/2,
lifetime = 2 * games.screen.fps,
after_death = games.screen.quit)
games.screen.add(end_message)
def update(self):
""" Check if bottom edge has reached screen bottom. """
if self.bottom > games.screen.height:
self.destroy()
Dropper.additonal_drop()
class Dropper(games.Sprite):
"""
A invisible sprite that drops the rocks.
"""
image = games.load_image("rock.bmp")
def __init__(self, y = 55, speed = 2, odds_change = 200):
""" Initialize the dropper object. """
super(Dropper, self).__init__(image = Dropper.image,
x = games.screen.width / 2, y = y,
dx = speed)
self.odds_change = odds_change
self.time_til_drop = 0
def update(self):
""" Determine if direction needs to be reversed. """
if self.left < 0 or self.right > games.screen.width:
self.dx = -self.dx
elif random.randrange(self.odds_change) == 0:
self.dx = -self.dx
def additonal_drop(self):
new_rock = Rock(x = self.x)
games.screen.add(new_rock)
new_rock = Rock(x = self.x)
games.screen.add(new_rock)
def main():
""" Play the game. """
wall_image = games.load_image("wall.jpg", transparent = False)
games.screen.background = wall_image
the_chef = Chef()
games.screen.add(the_chef)
the_rock = Rock()
games.screen.add(the_rock)
the_dropper = Dropper()
games.screen.add(the_dropper)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
# start it up!
main()
Try defining the Rock class with an additional parameter dropper:
def __init__(self, x = 320, y = 90, dropper=Dropper()):
dropper will be the Dropper instance. Then create Rock instances from inside Dropper as follows:
Rock(x=self.x, dropper=self)
This will pass the Dropper instance itself to each Rock instance that the Dropper instance creates. In Rock's __init__(), save a reference to the Dropper instance:
self.dropper = dropper
Call additional_drop() with:
self.dropper.additional_drop()

python 2.6, pygame, pong ball wont go on y corrodinate correctly

trying to get the ball to move along the y coordinate, it wont work, more explanation at bottom
from livewires import games, color
games.init(screen_width = 640, screen_height = 480, fps = 50)
points = games.Text(value = 0, size = 25, color = color.green,
bottom = games.screen.height - 5, left = 10)
games.screen.add(points)
class Paddle(games.Sprite):
image = games.load_image("paddle.bmp")
def __init__(self):
super(Paddle, self).__init__(image = Paddle.image, y = games.mouse.y, right = games.screen.width)
def update(self):
""" Move to mouse x position. """
self.y = games.mouse.y
if self.top < 0:
self.top = 0
if self.bottom > games.screen.height:
self.bottom = games.screen.height
self.check_bounce()
def check_bounce(self):
for bouncingBall in self.overlapping_sprites:
bouncingBall.handle_bounce()
class BouncingBall(games.Sprite):
image = games.load_image("ball.bmp")
def __init__(self, x, y, dx, dy):
super(BouncingBall, self).__init__(image = BouncingBall.image, x = x, y = y, dx = dx, dy = dy)
def update(self):
""" Check if bottom edge has reached screen bottom. """
if self.top > 0 or self.bottom < games.screen.height:
self.dy = -self.dy
if self.left < 0:
self.dx = -self.dx
if self.right > games.screen.width:
self.end_game()
def handle_bounce(self):
global points
points.value += 10
points.left = 10
self.dx = -self.dx
def end_game(self):
end_message = games.Message(value = "GAME OVER",
size = 90,
color = color.red,
x = games.screen.width/2,
y = games.screen.height/2,
lifetime = 10 * games.screen.fps,
after_death = games.screen.quit)
games.screen.add(end_message)
def main():
background_image = games.load_image("background.png", transparent = False)
games.screen.background = background_image
the_paddle = Paddle()
games.screen.add(the_paddle)
games.mouse.is_visible = False
new_ball = BouncingBall(x = games.screen.width/2, y = games.screen.height/2, dx = 2, dy = 2) #<-believe it is right here im messing up
games.screen.add(new_ball)
games.screen.mainloop()
main()
I am having a horrible time at getting my ball to correctly follow the y coordinate, I believe I am doing it wrong when i create instance new_ball, (main function) but i have no idea lol, anyone see what im doing wrong?
Make sure that this line
if self.top > 0 or self.bottom < games.screen.height:
self.dy = -self.dy
Isn't constantly evaluating to True. If so, your y-velocity will constantly toggle and the ball will never appear to change y coordinate.

Python Packman score

I did a packman game using Python but Im having 2 problems. First my score is not updating, so the score remains 1. Second after a while my packman game crashes it, it shows me 2 error message saying an attribute error: in packman.handle_collide() and self.check_collide().I dnt knw how to fix it.
here is my code:
# Create a SNAPMAN
from livewires import games, color
import random
games.init(screen_width = 200, screen_height = 150, fps = 50)
explosion_files = ["explosion1.bmp",
"explosion2.bmp",
"explosion3.bmp",
"explosion4.bmp",
"explosion5.bmp",
"explosion6.bmp",
"explosion7.bmp",
"explosion8.bmp",
"explosion9.bmp"]
def display_score(score):
display_score = games.Text(value = score + 1, size = 25, color = color.black,
top = 5, right = games.screen.width - 10)
games.screen.add(display_score)
#increase the score
score = score + 1
class Packman(games.Sprite):
"""Create a Gumpy that is controlled by the mouse"""
image = games.load_image("packman.png")
def __init__(self, x = games.mouse.x, y = games.mouse.y):
"""Initialise packman"""
super(Packman,self).__init__(image = Packman.image,
x = games.mouse.x,
y = games.mouse.y)
oldx = games.mouse.x
oldy = games.mouse.y
def display_score(score):
display_score = games.Text(value = score + 1, size = 25, color = color.black,
top = 5, right = games.screen.width - 10)
games.screen.add(display_score)
#increase the score
score = score + 1
def update(self):
"""Move Packman's x and y coordinates"""
oldx = self.x
oldy = self.y
self.x = games.mouse.x
self.y = games.mouse.y
if(self.x < oldx):
self.angle = 180
if(self.x > oldx):
self.angle = 0
self.check_collide()
def check_collide(self):
"""Check if collides with ball"""
for packman in self.overlapping_sprites:
packman.handle_collide()
class Ball(games.Sprite):
""" Create the moving balls"""
def update(self):
"""Change the direction when the ball reached the edge"""
if self.right > games.screen.width or self.left < 0:
self.dx = -self.dx
if self.bottom > games.screen.height or self.top < 0:
self.dy = -self.dy
def handle_collide(self):
"""Something must happen when the ball collides"""
#Explosive sound
sound = games.load_sound("explosion.wav")
sound.play()
explosion = games.Animation(images = explosion_files, x = self.x, y = self.y,
n_repeats = 5, repeat_interval = 5,
is_collideable = False)
games.screen.add(explosion)
self.x = random.randrange(games.screen.width)
self.y = random.randrange(games.screen.height)
display_score(1)
def randomX():
"""Generate random x values"""
rx = random.randrange(games.screen.width)
return rx
def ramdomY():
"""Generate random y values"""
ry = random.randrange(games.screen.width)
return ry
def main():
#Set background
wall_image = games.load_image("wall.jpg", transparent = False)
games.screen.background = wall_image
games.mouse.is_visible = False
games.screen.event_grab = True
#Display score
display_score(0)
#Load and display the balls( red, green and blue)
ball_image = games.load_image("ball_red.png")
ball_red = Ball(image = ball_image,
x = random.randrange(games.screen.width),
y = random.randrange(games.screen.height),
dx = 1, dy = 1)
games.screen.add(ball_red)
ball_image = games.load_image("ball_green.png")
ball_green = Ball(image = ball_image,
x = random.randrange(games.screen.width),
y = random.randrange(games.screen.height),
dx = 1, dy = 1)
games.screen.add(ball_green)
ball_image = games.load_image("ball_blue.png")
ball_blue = Ball(image = ball_image,
x = random.randrange(games.screen.width),
y = random.randrange(games.screen.height),
dx = 1, dy = 1)
games.screen.add(ball_blue)
packman_1 = Packman()
games.screen.add(packman_1)
games.screen.mainloop()
main()
The score is not updating because you are incrementing a local variable of the method display_score. You can make it a class member of Packman, for example:
def __init__(self, x = games.mouse.x, y = games.mouse.y):
"""Initialise packman"""
# do something ...
# initialize the score variable
self._score = 0
And then,
def display_score(self):
display_score = games.Text(value = self._score, size = 25,
color = color.black, top = 5, right = games.screen.width - 10)
games.screen.add(display_score)
#increase the score
self._score += 1 # add one to the current value
You missed the self argument in the definition of the method!

Debugging a simple pygame game

When I run this, it executes to an error in one of the imports. I posted the error at tend end of the code. The program essentially doesn't run, I can't tell what errors are occurring from the tracebacks. Any insight would be appreciated.
from livewires import games, color
import random
games.init (screen_width = 640, screen_height = 480, fps = 50)
class Pizza (games.Sprite):
def __init__(self, screen, x, y, image):
self.pizzaimage = games.load_image ("pizza.bmp", transparent = True)
self.init_sprite (screen = screen,
x = x,
y = 90,
image = self.pizzaimage,
dy = 1)
def moved (self):
if self.bottom > 480:
self.game_over()
def handle_caught (self):
self.destroy()
def game_over(self):
games.Message(value = "Game Over",
size = 90,
color = color.red,
x = 320,
y = 240,
lifetime = 250,
after_death = games.screen.quit())
class Pan (games.Sprite):
def __init__(self, screen, x, image ):
self.panimage = games.load_image ("pan.bmp", transparent = True)
self.init_sprite (screen = screen,
x = games.mouse.x,
image = self.panimage)
self.score_value = 0
self.score_text = games.Text (value = "Score"+str(self.score_value),
size = 20,
color = color.black,
x = 500,
y = 20)
def update (self):
self.x = games.mouse.x
#When it reaches end, return value to corner. Example:
if self.left < 0:
self.left = 0
if self.right > 640:
self.right = 640
def handling_pizzas (self):
for Pizza in self.overlapping_sprites:
self.score_value += 10
Pizza.handle_caught()
class Chef (games.Sprite):
def __init__(self, screen, x, y, image, dx, dy,):
self.chefimage = games.load_image ("chef.bmp", transparent = True)
self.timer = 80
self.init_sprite (screen = screen,
x = random.randrange(640),
y = y,
dx = 20,
dy = 0)
def update (self):
if self.left < 0:
self.dx = -self.dx
if self.right > 640:
self.dx = -self.dx
elif random.randrange (10) == 5:
self.dx = -self.dx
def add_teh_pizzas (self):
if self.timer > 80:
self.timer = self.timer - 1
else:
new_pizza = Pizza (x = self.x)
games.screen.add (new_pizza)
self.timer = 80
#main
def main ():
backgroundwall = games.load_image ("wall.jpg", transparent = False)
games.screen.background = backgroundwall
le_chef = Chef = ()
games.screen.add(le_chef)
le_pan = Pan = ()
games.screen.add (le_pan)
games.mouse.is_visible = False
games.screen.event_grab = False
games.screen.mainloop()
main()
ERRORS REPORT:
Traceback (most recent call last):
File "C:\Users\COMPAQ\My Documents\Aptana Studio Workspace\Pythonic Things\PizzaPanicAttempt\PizzaPanicAttempt1.py", line 98, in <module>
main()
File "C:\Users\COMPAQ\My Documents\Aptana Studio Workspace\Pythonic Things\PizzaPanicAttempt\PizzaPanicAttempt1.py", line 96, in main
games.screen.mainloop()
File "C:\Python27\lib\site-packages\livewires\games.py", line 303, in mainloop
object._erase()
AttributeError: 'tuple' object has no attribute '_erase'
Don't know anything about livewires, but the following looks suspicious:
le_chef = Chef = ()
games.screen.add(le_chef)
le_pan = Pan = ()
games.screen.add (le_pan)
You have classes called Chef, and Pan, but you're not doing anything with these - just assigning these things an empty tuple, and then adding them to games.screen. Were they supposed to be initialized instead?
eg.
le_chef = Chef(<some args>)
le_pan = Pan(<some args>)

Categories