0

I write this code because I want to move a sprite using the key arrow. Nothing special I suppose.

I spent a lot of time searching for tutorials that show and move a simple picture.

Below is my code:

The first part is quite standard, import libraries and define the characteristic value of the program:

import sys
import pygame

pygame.init()

#refer value
border_X=800
border_Y=600
FPS=30
POS_X=300
POS_Y=300
INCREASE_X=0
INCREASE_Y=0

screen=pygame.display.set_mode((border_X,border_Y))
clock=pygame.time.Clock()

Here I define the sprite class, maybe there's a misunderstood about the definition of the class, maybe I used the rect.center in an improper way.

class Ship(pygame.sprite.Sprite):
       image = pygame.image.load("fighter_0.png")
       image = image.convert_alpha()
       def __init__(self, X_INIT, Y_INIT):         
           super(Ship, self).__init__()
           self.image = Ship.image
           self.rect = self.image.get_rect()
           self.rect.center = (X_INIT, Y_INIT)
       def update(self,x,y):
           self.rect.center = (x,y)

Here I create the sprite group, probably it's not necessary for a single sprite, but the main purpose of this program is the learning.

In any case I've tried to display the sprite without create the group 'character'

character = pygame.sprite.Group()
Ship.groups=character
ship=Ship(POS_X,POS_Y)
ship.add(character)

And finally the loop cycle, maybe there's an error in the updating modalities

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:             
        if event.type == pygame.KEYUP:

        '''i've removed the if cycle of event.type 
           because it's long and unnecessary to 
           explain why the sprite doesn't appear'''  

    POS_X+=INCREASE_X
    POS_Y+=INCREASE_Y    
    ship.update(POS_X,POS_Y)
    clock.tick(FPS)
    #i've tried both, flip and update
    pygame.display.flip()
    pygame.display.update()

pygame.quit()

In case I have completely miscalculated the modalities of the class sprite,could someone explain how to set for this situation the class Ship?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Ziru93
  • 71
  • 1
  • 2
  • 6
  • 2
    Somewhere in there you should be calling the sprite group's draw method. So if your sprite group is "character" you'd call character.draw(screen) to draw all the sprites within that group. Otherwise you have to blit each image individually. Try calling it just before display.flip() – oxrock Mar 22 '17 at 01:17
  • @oxrock, unfortunately, this is the interpreter answer: [AttributeError: 'Ship' object has no attribute 'draw'] – Ziru93 Mar 22 '17 at 11:27
  • in your code, "character" is your spritegroup. To be honest your declarations up there are all funky. Try just adding your ship to your character group, because it looks like you're trying to do it backwards. So declare the sprite group. create the ship, then just add the ship to the group. so: character = pygame.sprite.Group(), and then ship=Ship(POS_X,POS_Y), then simply character.add(ship). Now at the bottom before display.flip() you should be able to call character.draw(screen) to draw your ship (and all sprites contained within character) on the screen. – oxrock Mar 22 '17 at 13:02
  • @oxrock, it seems to work, and i have also fix the problem to remove the character for update the screen, thank you very much! – Ziru93 Mar 22 '17 at 14:02
  • no problem, happy coding. – oxrock Mar 22 '17 at 14:10

0 Answers0