0
1

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 3 years have passed since last update.

Pythonのpygame関連のコード

Posted at

pygame関連のコードです
ボールゲーム

python:ballgame.py
import pygame
from pygame.locals import *
import sys


# def event(kvx):
#     # イベント処理
#     for event in pygame.event.get():  # イベントキューからキーボードやマウスの動きを取得
#         if event.type == QUIT:        # 閉じるボタンが押されたら終了
#             pygame.quit()             # Pygameの終了(ないと終われない)
#             sys.exit()                # 終了(ないとエラーで終了することになる)
#         elif event. type == KEYDOWN: 
#             if event.key==K_LEFT:
#                 kvx = -1#横方向の速度
#             elif event.key==K_RIGHT:
#                 kvx = +1#横方向の速度
#     return kvx            

            
# for event in pygame.event.get(): 
#     if event. type == QUIT: 
#         pygame. quit() 
#         sys. exit()
#     elif event. type == KEYDOWN: 
#         if event.key==K_LEFT:
#           pvx =-5#横方向の速度
#         elif event.key==K_RIGHT:
#           pvx =+5#横方向の速度            
            
pygame.init()                                 # Pygameの初期化
screen = pygame.display.set_mode((800, 600))  # 800*600の画面
px=120
py=100

vx = 1
vy = 1

kx =350
ky =500
kvx = 0
kh = 50
kw = 100

while True:
    screen.fill((255,255,255))                                      # 画面を白に
    pygame.draw.circle(screen,(200,10,10),(px,py),10)               # ●
         
    px = px + vx
    py = py + vy
    if py >= 600:
        vy = -1
    if px >=800:
        vx = -1
    if py <=0:
        vy = 1
    if px <=0:
        vx = 1
        
    if kx<px<kx+kw and ky<py<ky+kh:
        print("hit")
        vy *= -1
        py = ky
        
    #kvx =event(kvx)
    #event()
    for event in pygame.event.get():  # イベントキューからキーボードやマウスの動きを取得
        if event.type == QUIT:        # 閉じるボタンが押されたら終了
            pygame.quit()             # Pygameの終了(ないと終われない)
            sys.exit()                # 終了(ないとエラーで終了することになる)
        elif event. type == KEYDOWN: 
            if event.key==K_LEFT:
                kvx = -1#横方向の速度
            elif event.key==K_RIGHT:
                kvx = +1#横方向の速度
            elif event.key==K_UP:
                kvx = 0#横方向の速度
    
    
    kx = kx + kvx    
    pygame.draw.rect(screen, (255,0,0), Rect(kx, ky, kw,kh), 1) 
    pygame.display.update()                                         # 画面更新

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?