Home > other >  Python game programming an alien invasion in the spaceship cannot be loaded into the top left corner
Python game programming an alien invasion in the spaceship cannot be loaded into the top left corner

Time:03-23

The following for creating Alien class code

# alien. Py
The import pygame
The from pygame. Sprite import Sprite

The class lived (Sprite) :
# represents a single alien class
Def __init__ (self, ai_settings, screen) :
# initialization aliens and set its starting position
The self, super (Alien) __init__ ()
The self. The screen=screen
Self. Ai_settings=ai_settings

# load alien images, and set its the rect attribute
Self. Image=pygame. Image. The load (r 'H: \ Study \ python \ \ alien practice \ alien_invasion \ images. BMP')
The self. The rect=self. Image. Get_rect ()

# every alien initially near the left upper corner of the screen
The self. The rect. X=self. The rect. Width
The self. The rect. Y=self. The rect. Height

# storage alien the accurate position of the
Self. X=float (self. The rect. X)

Def blitme (self) :
# draw aliens in the location specified
The self. The screen. The blit (self image, self. The rect)

(the above is the program code, after many compilation and examination, no error, the final confirmation code itself is correct, but the spacecraft images leng load is less than the upper left screen)


After replacing load the picture for many times, find programs load images is very big, ' 'alien. BMP "file has more than 14 m, then change its size to less than 10 m, with the need of screen appears, I change to around 30 k, problem solved,

(the above experience for himself, not whether because of image size, but the problem is solved, if you encounter the same situation or can try)
  • Related