Я создаю небольшую игру пинг понг)
И я столкнулся с проблемами:
после проигрыша шарик не останавливается
после нажатия на кнопку 'R' -рестарт шарик скачет только вверх и вниз
и пожалуйста напишите в коде чтобы место чёрного фона бала какая нибудь картика
ЗАРАНЕЕ СПАСИБО БОЛЬШОЕ
КОД:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
import pygame as pg import random pg.init() pg.font.init() TITLE = 'absolute.pong' WIDTH = 800 HEIGHT = 480 FPS = 60 WHITE = (255, 255, 255) BLACK = (0, 0, 0) PLATFORM_WIDH = 90 PLATFORM_HEIGHT = 22 PLATFORM_SPEED = 13 platform_rect= pg.rect.Rect(WIDTH / 2 - PLATFORM_WIDH / 2, HEIGHT - PLATFORM_HEIGHT * 2, PLATFORM_WIDH, PLATFORM_HEIGHT) CIRCLE_RADIUS = 15 CIRCLE_SPEED = 7 circle_frist_collide = False circle_x_speed = 0 circle_y_speed = CIRCLE_SPEED circle_rect = pg.rect.Rect(WIDTH / 2 - CIRCLE_RADIUS, HEIGHT /2 - CIRCLE_RADIUS, CIRCLE_RADIUS * 2, CIRCLE_RADIUS * 2) score = 0 ARIAL_FONT_PATH = pg.font.match_font('arial') ARIAL_FONT_48 = pg.font.Font(ARIAL_FONT_PATH, 48) ARIAL_FONT_36 = pg.font.Font(ARIAL_FONT_PATH, 36) screen = pg.display.set_mode([WIDTH, HEIGHT]) pg.display.set_caption(TITLE) clock = pg.time.Clock() game_over = False running = True while running: for event in pg.event.get(): if event.type == pg.QUIT: running = False continue elif event.type == pg.KEYDOWN: if event.key == pg.K_ESCAPE: running = False continue elif event.key == pg.K_r: game_over = False platform_rect.centerx = WIDTH / 2 platform_rect.bottom = HEIGHT - PLATFORM_HEIGHT circle_rect.center = [WIDTH / 2, HEIGHT / 2] circle_x_speed = 0 circle_y_speed = CIRCLE_SPEED score = 0 screen.fill(BLACK) if not game_over: keys = pg.key.get_pressed() if keys[pg.K_a]: platform_rect.x -= PLATFORM_SPEED elif keys[pg.K_d]: platform_rect.x += PLATFORM_SPEED if platform_rect.colliderect(circle_rect): if not circle_frist_collide: if random.randint(0, 1) == 0: circle_x_speed = CIRCLE_SPEED else: circle_x_speed = -CIRCLE_SPEED circle_frist_collide = True circle_y_speed = -CIRCLE_SPEED score += 1 pg.draw.rect(screen, WHITE, platform_rect) circle_rect.x += circle_x_speed circle_rect.y += circle_y_speed if circle_rect.bottom >= HEIGHT: game_over = True circle_y_speed = -CIRCLE_SPEED elif circle_rect.top <= 0: circle_y_speed = CIRCLE_SPEED elif circle_rect.left <= 0: circle_x_speed = CIRCLE_SPEED elif circle_rect.right >= WIDTH: circle_x_speed = -CIRCLE_SPEED pg.draw.circle(screen, WHITE, circle_rect.center, CIRCLE_RADIUS) score_surface = ARIAL_FONT_48.render(str(score), True, WHITE) if not game_over: screen.blit(score_surface, [WIDTH / 2 - score_surface.get_width() / 2, 15]) else: retry_surface = ARIAL_FONT_36.render('press R to restart', True, WHITE) screen.blit(score_surface, [WIDTH / 2 - score_surface.get_width() / 2, HEIGHT / 3]) screen.blit(retry_surface, [WIDTH / 2 - retry_surface.get_width() / 2, HEIGHT / 3 + score_surface.get_height()]) clock.tick(FPS) pg.display.flip() pg.quit() |
@kiruha555 Для решения проблем и добавления изображения вместо черного фона, вам нужно внести несколько изменений в код. Попробуйте этот код:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
import pygame as pg import random pg.init() pg.font.init() # Определение цветов и размеров TITLE = 'absolute.pong' WIDTH = 800 HEIGHT = 480 FPS = 60 # Добавьте путь к вашей картинке BACKGROUND_IMAGE_PATH = 'background.jpg' # Замените 'background.jpg' на путь к вашей картинке WHITE = (255, 255, 255) PLATFORM_WIDH = 90 PLATFORM_HEIGHT = 22 PLATFORM_SPEED = 13 platform_rect = pg.rect.Rect(WIDTH / 2 - PLATFORM_WIDH / 2, HEIGHT - PLATFORM_HEIGHT * 2, PLATFORM_WIDH, PLATFORM_HEIGHT) CIRCLE_RADIUS = 15 CIRCLE_SPEED = 7 circle_frist_collide = False circle_x_speed = 0 circle_y_speed = CIRCLE_SPEED circle_rect = pg.rect.Rect(WIDTH / 2 - CIRCLE_RADIUS, HEIGHT / 2 - CIRCLE_RADIUS, CIRCLE_RADIUS * 2, CIRCLE_RADIUS * 2) score = 0 ARIAL_FONT_PATH = pg.font.match_font('arial') ARIAL_FONT_48 = pg.font.Font(ARIAL_FONT_PATH, 48) ARIAL_FONT_36 = pg.font.Font(ARIAL_FONT_PATH, 36) # Создание экрана с заданным размером screen = pg.display.set_mode([WIDTH, HEIGHT]) pg.display.set_caption(TITLE) clock = pg.time.Clock() game_over = False running = True # Загрузка изображения фона background_image = pg.image.load(BACKGROUND_IMAGE_PATH) # Загрузите изображение фона while running: for event in pg.event.get(): if event.type == pg.QUIT: running = False continue elif event.type == pg.KEYDOWN: if event.key == pg.K_ESCAPE: running = False continue elif event.key == pg.K_r: game_over = False platform_rect.centerx = WIDTH / 2 platform_rect.bottom = HEIGHT - PLATFORM_HEIGHT circle_rect.center = [WIDTH / 2, HEIGHT / 2] circle_x_speed = 0 circle_y_speed = CIRCLE_SPEED score = 0 # Отрисовка изображения фона screen.blit(background_image, (0, 0)) if not game_over: keys = pg.key.get_pressed() if keys[pg.K_a]: platform_rect.x -= PLATFORM_SPEED elif keys[pg.K_d]: platform_rect.x += PLATFORM_SPEED if platform_rect.colliderect(circle_rect): if not circle_frist_collide: if random.randint(0, 1) == 0: circle_x_speed = CIRCLE_SPEED else: circle_x_speed = -CIRCLE_SPEED circle_frist_collide = True circle_y_speed = -CIRCLE_SPEED score += 1 pg.draw.rect(screen, WHITE, platform_rect) circle_rect.x += circle_x_speed circle_rect.y += circle_y_speed if circle_rect.bottom >= HEIGHT: game_over = True circle_y_speed = -CIRCLE_SPEED elif circle_rect.top <= 0: circle_y_speed = CIRCLE_SPEED elif circle_rect.left <= 0: circle_x_speed = CIRCLE_SPEED elif circle_rect.right >= WIDTH: circle_x_speed = -CIRCLE_SPEED pg.draw.circle(screen, WHITE, circle_rect.center, CIRCLE_RADIUS) score_surface = ARIAL_FONT_48.render(str(score), True, WHITE) if not game_over: screen.blit(score_surface, [WIDTH / 2 - score_surface.get_width() / 2, 15]) else: retry_surface = ARIAL_FONT_36.render('press R to restart', True, WHITE) screen.blit(score_surface, [WIDTH / 2 - score_surface.get_width() / 2, HEIGHT / 3]) screen.blit(retry_surface, [WIDTH / 2 - retry_surface.get_width() / 2, HEIGHT / 3 + score_surface.get_height()]) clock.tick(FPS) pg.display.flip() pg.quit() |