import pygame, sys
from settings1 import plant_game
ai_setting = plant_game()
class moving():
def __init__(self, ai_setting, screen):
self.screen = ai_setting.screen
self.ai_setting = ai_setting
self.image = pygame.Surface((60, 40))
self.rect = self.image.get_rect()
self.get_rect = screen.get_rect()
self.rect.centerx = self.get_rect.centerx
self.rect.bottom = (self.get_rect.bottom) / 2
print(self.rect.centerx)
def blite(self):
ai_setting.screen.blit(self.image, self.rect)
def update(self):
self.rect.centerx += ai_setting.ship_speed_factor
if self.rect.centerx > 800 or self.rect.centerx < 0:
ai_setting.ship_speed_factor = -ai_setting.ship_speed_factor
def run():
ship = moving(ai_setting, ai_setting.screen)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
ai_setting.screen.fill((255, 255, 255))
ship.blite()
ship.update()
pygame.display.update()
pygame.display.flip()
run()
Yesterday I finally made it. If you don’t use the class method, you can easily make the code for the rectangle to automatically collide left and right. I mainly call two functions.
- One is to draw the rectangle function blite function: draw a Surface on the screen (on the screen)
- The second is to update the rectangular position function: update, this formula is also a long time I have thought about these two days, forgive me for not being a science student. .
I first put the square RECT in the middle of the screen, I let it self.rect.centerx = self.screen.get_rect.centerx (the meaning of this equation is that I set the X coordinate of its rect in the middle of the screen, which is the screen's Middle), self.rect.bottom = (self.screen.get_rect.bottom)/2 (meaning, I set the Y-axis coordinate of its RECT in the middle of the width of the screen), so there is such a picture
interface The rectangle moves down.
def update(self):
self.rect.centerx += ai_setting.ship_speed_factor
if self.rect.centerx > 800 or self.rect.centerx < 0:
ai_setting.ship_speed_factor = -ai_setting.ship_speed_factor
This formula means that I first move the X-axis of the square. If the center of the square (centerx)>the length of the screen is 800 or less than the length of the screen, then the moving speed becomes negative.
I didn't understand this formula at first, but it suddenly dawned on me later. . The first movement is a positive movement, the second is a negative movement, and the third is a negative movement. This keeps looping, and you can always move in the area I gave.
The following are the parameters of the module:
import pygame
class plant_game():
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((800,600))
self.bg_color = (230,230,230)
self.ship_speed_factor = 1
self.bullet_speed_factor = 0.5
self.bullet_width = 10
self.bullet_height = 15
self.bullet_color = 60,60,60
self.bullet_allow = 3
self.screen_width = self.screen.get_width()
self.screen_height = self.screen.get_height()
self.alien_speed_factor = 1
self.fleet_drop = 10
self.fleet_direction = 1
For this parameter module, I use the setting parameters in the aircraft war, which does not affect my creation of moving rectangles.