Форум сайта python.su
import pygame
import sys
class Game():
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((1200, 800))
self.screen_rect = self.screen.get_rect()
self.character_image = pygame.image.load('game_character.bmp')
self.character_rect = self.character_image.get_rect()
self.character_rect.center = self.screen_rect.center
self.moving_right = False
self.bullet_image = pygame.image.load('bullet.bmp')
self.bullet_rect = self.bullet_image.get_rect()
def run_game(self):
while True:
self.screen.fill((255, 250, 250))
self._check_events()
self._movement()
self._draw_character()
pygame.display.flip()
def _draw_character(self):
self.screen.blit(self.character_image, self.character_rect)
def _check_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
self.moving_right = True
if event.key == pygame.K_RIGHT:
self._fire_bullet_right()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_d:
self.moving_right = False
def _fire_bullet_right(self):
self.bullet_rect.midleft = self.character_rect.midright
self.screen.blit(self.bullet_image, self.bullet_rect)
if self.bullet_rect.x < 1200:
self.bullet_rect.x += 2
def _movement(self):
if (self.moving_right) and (self.character_rect.x < (1200 - self.character_rect.width)):
self.character_rect.x += 1
game = Game()
game.run_game()
Прикреплённый файлы:
game.py (1,5 KБ)
Офлайн
В этих двух строках неправильный отступ.
if event.key == pygame.K_RIGHT: self._fire_bullet_right()
Офлайн