3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pygameを使ってPongを作った

Last updated at Posted at 2019-09-10

#初めに
ゲームAIとかでよく見るボールをラケットで打つPongをPythonで簡単に作れるらしかったので、作ってみました。もとからあるゲームを作る意味があまりない上に、普通はゲームをPython以外の言語で作ることが多いので、需要あるのかなあとは思います。

基本仕様

あまり複雑ではないゲームではないです。しいて言うならボールが壁に衝突したとき、もしくはラケットに衝突したときに衝突方向の速度成分を反転させることくらいです。

プログラム本体

import pygame
from pygame.locals import *
import sys
import math
import random

def main():
    (w, h) = (300, 200) # window size
    (x, y) = (10, h//2) # racket center
    (bx, by) = (w//2, h//2)
    vx, vy = 8, 5
    
    pygame.init() # initialize pygame
    pygame.display.set_mode((w, h)) # set screen
    screen = pygame.display.get_surface()
    
    pygame.mixer.init(frequency = 44100) # initial setting
    pygame.mixer.music.load("./hit1.mp3") # hit sound
    
    while (1):                            
        pygame.display.update() # update display
        pygame.time.wait(30) # interval of updating
        screen.fill((0, 0, 0, 0)) # fill screen with black R:0 G:0 B:0
        
        # racket
        pygame.draw.line(screen, (255, 255, 255), (x, y-20), (x, y+20), 10)
        # ball
        pygame.draw.line(screen, (255, 0, 0), (bx-3, by), (bx+3, by), 8)
        
        pressed_key = pygame.key.get_pressed()
        # racket movement
        if pressed_key[K_UP]:
            y -= 10
        if pressed_key[K_DOWN]:
            y += 10
        
        # racket inside window
        if y < 20:
            y = 20
        if y > h-20:
            y = h-20
            
        # ball movement
        bx += vx
        by += vy
        
        # ball inside window
        if (bx > w and vx > 0):
            vx = -vx   
        if (0 > by and vy < 0) or (by > h and vy > 0):
            vy = -vy
        
        # racket hit
        if (y-30 < by < y+30 and vx < 0 and 7 < bx < 15):
            vx = -vx
            pygame.mixer.music.play(1)
        
        # ball went outside
        if bx < -50:
            pygame.time.wait(1000)
            bx, by = (w//2, h//2)
            theta = 2*math.pi*random.random()
            vx = int(math.cos(theta) * 7) + 2*math.cos(theta)/abs(math.cos(theta))
            vy = int(math.sin(theta) * 7) + 2*math.sin(theta)/abs(math.sin(theta))
        
        for event in pygame.event.get():
            # close button pushed
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            
            # escape key pressed
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit()
    
if __name__ == '__main__':
    main()

おわりに

本格的にゲームを作りたいならPython以外の言語のほうが良さそうですね。簡単な2Dゲームなら作れそうなので、今後機会があればやってみようと思います。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?