[Tutor] pygame help please

2019-04-18 Thread fatima butt
Hi, I am trying to add lives to my pygame.I am using acer SWIFT computer.I
am using 3.7.3 python version and pygame 1.9.5

however the following error is coming:
Traceback (most recent call last):
  File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line 304,
in 
draw_lives(screen,WIDTH - 100,5, player.lives, player_mini_img)
  File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line 56,
in draw_lives
img.rect.x = x + 30 * i
AttributeError: 'pygame.Surface' object has no attribute 'rect'
>>>

My code is as following:
# Pygame template - skeleton for a new pygame project
#Frozen Jam by tgfcoder  licensed under
CC-BY-3
import pygame
import random
from os import path

img_dir = path.dirname(__file__)
snd_dir = path.dirname(__file__)


WIDTH = 480
HEIGHT = 600
FPS = 60

# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255,255,0)

pygame.init()

pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()

font_name = pygame.font.match_font('arial')
def draw_text(surf, text, size, x, y):
font = pygame.font.Font(font_name, size)
text_surface = font.render(text, True, WHITE)
text_rect = text_surface.get_rect()
text_rect.midtop =(x,y)
surf.blit(text_surface, text_rect)

def newmob():
m = Mob()
all_sprites.add(m)
mobs.add(m)

def draw_shield_bar(surf,x,y,pct):
if pct < 0:
pct = 0
BAR_LENGTH = 100
BAR_HEIGHT = 10
fill = (pct /100)* BAR_LENGTH
outline_rect = pygame.Rect(x,y,BAR_LENGTH,BAR_HEIGHT)
fill_rect = pygame.Rect(x,y,fill,BAR_HEIGHT)
pygame.draw.rect(surf, GREEN, fill_rect)
pygame.draw.rect(surf,WHITE,outline_rect, 2)
def draw_lives(surf,x,y,lives,img):
for i in range(lives):
img_rect = img.get_rect()
img.rect.x = x + 30 * i
img.rect.y = y
surf.blit(img, img_rect)
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.transform.scale(player_img,(50,38))
self.image.set_colorkey(BLACK)
self.rect =self.image.get_rect()
self.radius = 20
#pygame.draw.circle(self.image, RED,self.rect.center, self.radius)
self.rect.centerx = WIDTH / 2
self.rect.bottom = HEIGHT -10
self.speedx = 0
self.shield = 100
self.shoot_delay = 250
self.last_shot = pygame.time.get_ticks()
self.lives = 3
self.hidden = False
self.hide_time = pygame.time.get_ticks()

def update(self):
# unhide if hidden
if self.hidden and pygame.time.get_ticks() - self.hide_timer > 1000:
self.hidden = False
self.rect.centerx = WIDTH/2
self.rect.bottom = HEIGHT - 10

self.speedx = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_LEFT]:
self.speedx = -8
if keystate[pygame.K_RIGHT]:
self.speedx = 8
if keystate[pygame.K_SPACE]:
self.shoot()
self.rect.x += self.speedx
if self.rect.right > WIDTH:
self.rect.right = WIDTH
if self.rect.left < 0:
self.rect.left = 0

def shoot(self):
now = pygame.time.get_ticks()
if now - self.last_shot > self.shoot_delay:
self.last_shot = now
bullet = Bullet(self.rect.centerx, self.rect.top)
all_sprites.add(bullet)
bullets.add(bullet)
shoot_sound.play()
def hide(self):
#hide the player temporarily
self.hidden = True
self.hide_timer = pygame.time.get_ticks()
self.rect.center = (WIDTH / 2, HEIGHT + 200)

class Mob(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image_orig = meteor_img
self.image_orig = random.choice(meteor_images)
self.image_orig.set_colorkey(BLACK)
self.image = self.image_orig.copy()
self.rect = self.image.get_rect()
self.radius = int(self.rect.width *.9 / 2)
#pygame.draw.circle(self.image, RED,self.rect.center, self.radius)
self.rect.x =random.randrange(WIDTH-self.rect.width)
self.rect.y=random.randrange(-150,-100)
self.speedy=random.randrange(1,8)
self.speedx=random.randrange(-3,3)
self.rot = 0
self.rot_speed = random.randrange(-8,8)
self.last_update = pygame.time.get_ticks()

def rotate(self):
now = pygame.time.get_ticks()
if now - self.last_update > 50:
self.last_update = now
self.rot = (self.rot + self.rot_speed) % 360
new_image = pygame.transform.rotate(self.image_orig, self.rot)
old_center = self.rect.center
self.image = new_image
self.rect = self.image.get_rect()
se

Re: [Tutor] Help

2019-04-18 Thread fatima butt
Hi Peter,
Thanks soo much ...Its solved...have a nice day !

On Wed, 17 Apr 2019 at 22:48, Peter Otten <__pete...@web.de> wrote:

> fatima butt wrote:
>
> > hi Peter,
> > hope you are well.I am getting the following error when i am running the
> > pygame shell script.I am using Acer SWIFT computer.my python version is
> > 3.7.3 and pygame version is pygame 1.9.5
> >
> > Traceback (most recent call last):
> >   File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line
> >   166, in 
> > draw_text(screen, str(score),18, WIDTH/2,10)
> >   File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line
> 28,
> >   in draw_text
> > font = pygame.font.Font(font_name, size)
> > pygame.error: font not initialized
>
> OK, you now have a different script, with a different error. Does that
> mean
> you resolved your previous problem?
>
> Fine.
>
> Regarding the new error I found the following hint
>
> https://stackoverflow.com/questions/28517979/pygame-font-error
>
> i. e. use
>
> pygame.init()
>
> by entering the error message into a popular search engine ;)
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Mutable objects as dictionary keys: Accessing , Sorting and Using dictionary

2019-04-18 Thread Pushkar vaity
Hi,

I am using Python 3.7 with anaconda install in PyCharm
I am trying to traverse some directories using the os module and grab some
tcl procedures and put the proc names in python dictionary as keys. Values
are some other info related to the proc.

Problem: procs found in the directories can have similar names. But python
dict keys cannot be same.
So I am using the mutable objects as the key.

For Ex:   class MyClass:

def __init__(self, name):
self.name = name

def __repr__(self):
return repr(self.name)


proc_dict[MyClass(proc_name)] = (full_file, proc_args)

This has allowed me to have same separate keys in my dictionary.
But now,

   1. I am not able to access individual dictionary values by
specifying only the key name.
   2. I am not able to use the 'in' or 'not in' operations on my
dictionary successfully.
   3. Also, I am not able to use the sorted() function on my dict
items. It gives me an error as follows:


   - for proc_name, (file_name, proc_args) in sorted(proc_dict.items()):
  - TypeError: '<' not supported between instances of 'MyClass'
and 'MyClass'

Any ideas on how I can achieve the above points?
Thanks in advance!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pygame help please

2019-04-18 Thread Alan Gauld via Tutor
On 18/04/2019 15:16, fatima butt wrote:

> however the following error is coming:
> Traceback (most recent call last):
>   File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line 304,
> in 
> draw_lives(screen,WIDTH - 100,5, player.lives, player_mini_img)
>   File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line 56,
> in draw_lives
> img.rect.x = x + 30 * i
> AttributeError: 'pygame.Surface' object has no attribute 'rect'

> 
> def draw_lives(surf,x,y,lives,img):
> for i in range(lives):
> img_rect = img.get_rect()
> img.rect.x = x + 30 * i

Note that you store the rect in img_rect but then attempt to access it
via img.rect. The first form is a variable name. The second is an
attribute access. And as the error message says img does not have
a rect attribute. You need to use your variable.



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Mutable objects as dictionary keys: Accessing , Sorting and Using dictionary

2019-04-18 Thread Steven D'Aprano
On Thu, Apr 18, 2019 at 11:18:02AM -0400, Pushkar vaity wrote:
> Hi,
> 
> I am using Python 3.7 with anaconda install in PyCharm
> I am trying to traverse some directories using the os module and grab some
> tcl procedures and put the proc names in python dictionary as keys. Values
> are some other info related to the proc.
> 
> Problem: procs found in the directories can have similar names. But python
> dict keys cannot be same.

Sorry, can you explain why this is a problem? *Similar* is not 
enough to cause keys to clash. If you have a proc name "abcd" and a 
similar name "abcd.1", they count as different keys.

Could you use the full pathname?

"/some/directory/proc.123"

"/another/directory/proc.123"

would count as different keys.


> So I am using the mutable objects as the key.
> 
> For Ex:   class MyClass:
> 
> def __init__(self, name):
> self.name = name
> 
> def __repr__(self):
> return repr(self.name)

I'm not convinced that you actually need this class, but if you do, you 
ought to define a hash method:

def __hash__(self):
return hash(self.name)

and equality:

def __eq__(self, other):
if isinstance(other, MyClass):
return self.name == other.name
return NotImplemented


otherwise dict lookups will be by object identity, not value.


> proc_dict[MyClass(proc_name)] = (full_file, proc_args)
> 
> This has allowed me to have same separate keys in my dictionary.
> But now,
> 
>1. I am not able to access individual dictionary values by
> specifying only the key name.

Add the __hash__ and __eq__ methods as above, and then try:


values = proc_dict[ MyClass(proc_name) ]


>2. I am not able to use the 'in' or 'not in' operations on my
> dictionary successfully.

Again, you need __hash__ and __eq__ for them to work correctly.


>3. Also, I am not able to use the sorted() function on my dict
> items. It gives me an error as follows:
> 
> 
>- for proc_name, (file_name, proc_args) in sorted(proc_dict.items()):
>   - TypeError: '<' not supported between instances of 'MyClass'
> and 'MyClass'

You need to define a __lt__ method:

def __lt__(self, other):
if isinstance(other, MyClass):
return self.name < other.name
return NotImplemented


But again, I'm not convinced that you need this class at all. Can you 
please show us the proc names you get, how you get them, and what you do 
with them?

Using a wrapper class like this is probably the last resort.



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor