I'm a Python beginner.While learning from the basics through reference books, we are learning using pygame as an application.
I don't understand why you specify self as the argument for Ship() when you create an instance of the Ship class in the main module.There is no description in the reference book that I have been working on so far, and I did not understand it when I looked it up on the Internet (I think there is also a problem with my method of investigation).If anyone knows, please let me know.
Main Module
import sys
import pygame
from settings import Settings
from ship import ship
class AlienInvasion:
"""overall class to manage game associations and behavior."""
def__init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.settings=Settings()
"""To create a display window"""
self.screen=pygame.display.set_mode(
(self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Alien Invasion")
## **This is where I am confused about!!!
self.ship=Ship(self)
def run_game(self):
"""Start the main loop of the game"""
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
self.screen.fill (self.settings.bg_color)
self.ship.blitme()
pygame.display.flip()
if__name__=='__main__':
ai= AlienInvasion()
ai.run_game()
settings.py Module
class Settings:
"""A class to stroke all settings for Alien Invasion"""
def__init__(self):
self.screen_width=900
self.screen_height=600
self.bg_color=(230,230,230)
ship.py Module
import pygame
class Ship:
"""A class to manage the ship."""
def_init__(self,ai_game):
"""Initialize the ship and set its starting position."""
self.screen=ai_game.screen
self.screen_rect=ai_game.screen.get_rect()
self.image=pygame.image.load('images/ship.bmp')
self.rect=self.image.get_rect()
self.rect.midbottom=self.screen_rect.midbottom
def blitme(self):
"""Draw the ship at its current location."""
self.screen.blit (self.image, self.rect)
Updated with comments
I'm sorry.I didn't read the question well.
I don't understand why you specify self as the Ship() argument when generating instances of the Ship class in the main module.
For this, self of The following is a description of what self argument is a little off the mark It's a bit of a lack of explanation, but the Python document says: The first argument of a method is often referred to as In this description, naming The first argument when defining a method that belongs to Python's class represents your own instance. In other object-oriented languages, it is called Pythonself.ship=Ship(self)
is the second argument to def_init__(self,ai_game):
of class Ship:
.self.settings=Settings()
does not specify any arguments, but class Ship:
has no arguments other than self, so settings
instance self.settings SSettings()
has no arguments, but class Ship:
has no arguments.It's called code>.Pygame
is not dependent, but Python
is your own specification and usage habit.
9.Class
9.4. Various Notesself
.This naming is just a custom; the name self
has no special meaning in Python.However, if this practice is not followed, the code will be somewhat difficult for other Python programmers to read.In addition, a class browser program may be written to rely on this practice.
The method can invoke other methods using the method attribute of the self
argument:self
is a habit and actually says anything is fine, but it is not clear what the first argument is.this
.
This article explains many languages and is easy to understand.
this (programming)-Wikipediathis
is one of the concepts in the programming language and is a reserved word for the object in which it is moving.Used primarily within the instance method. In addition to this
, some languages use words such as self
, Me
, but...(omitted below)
In Python, this
is not a grammatical reserved word, but it is the first argument of the member function that automatically passes the target object.Traditionally, self
is used as the name for this argument.
Self is a variable that refers to itself.
For example, in the AlienInvasion class,
Created AlienInvasion class using self
US>Storing values.
Probably about object-oriented programming with Python
I think I didn't learn.
© 2024 OneMinuteCode. All rights reserved.