0
PythonのPython 2.7.3を使って簡単なゲームを作成しています。ボールは障害物がボールの方に落ちるのを避けなければなりません。しかし、私はどのように障害物を繰り返し落とさせるかを理解することができず、それぞれの障害物は無作為に落ちる。これは私のコードです:Pygame、Python 2.7.3。繰り返しの障害物
import pygame
from math import pi
import random
pygame.init()
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
BLUE = ( 0, 0, 255)
GREEN = ( 0, 255, 0)
RED = (255, 0, 0)
TAN = (252,231,182)
background = (38,37,37)
size = [400, 400]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Game")
siz = 20
x = 200
xminus = x - 140
xadd = x + 140
y = 350
fallingy = 0
rectx = 0
rectl = 60
rectw = 30
done = False
clock = pygame.time.Clock()
while not done:
clock.tick(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
screen.fill(background)
pressed = pygame.key.get_pressed()
circle = pygame.draw.circle(screen, BLUE, [x,y],siz)
if pressed[pygame.K_LEFT]:
screen.fill(background)
x = 60
circle2 = pygame.draw.circle(screen, BLUE,[x,y],siz)
if pressed[pygame.K_RIGHT]:
screen.fill(background)
x = 340
circle = pygame.draw.circle(screen, BLUE,[x,y],siz)
if pressed[pygame.K_LEFT] and pressed[pygame.K_RIGHT]:
screen.fill(background)
circle = pygame.draw.circle(screen, BLUE,[xminus,y],siz)
circle = pygame.draw.circle(screen, BLUE,[xadd,y],siz)
else:
circle = pygame.draw.circle(screen, BLUE, [x,y],siz)
x = 200
"""THIS IS THE CODE I WANT TO BE REPEATED RANDOMLY"""
def middle1():
pygame.draw.rect(screen, RED, [rectx, fallingy, 170, rectw])
pygame.draw.rect(screen, RED, [rectx + 400, fallingy, -170, rectw])
def lnr():
pygame.draw.rect(screen, RED, [rectx, fallingy, 20, rectw])
pygame.draw.rect(screen, RED, [rectx +400, fallingy, -20, rectw])
pygame.draw.rect(screen, RED, [rectx + 100, fallingy, 200, rectw])
def l():
pygame.draw.rect(screen, RED, [rectx, fallingy, 20, rectw])
pygame.draw.rect(screen, RED, [rectx + 400, fallingy, -300, rectw])
def r():
pygame.draw.rect(screen, RED, [rectx + 400, fallingy, -20, rectw])
pygame.draw.rect(screen, RED, [rectx, fallingy, 300, rectw])
"""THIS IS THE CODE I WANT TO BE REPEATED RANDOMLY"""
fallingy += 5
pygame.display.flip()
pygame.quit()
助けていただければ幸いです。
pygame.sprite.Spriteの使い方の簡単な例を教えてください。あなたのお返事ありがとうございます! – Petras99
リンク先のドキュメントページには、「Sprite」を使用しているオープンソースコードの[この検索ページ](http://nullege.com/codes/search?cq=pygame.sprite.Sprite)へのリンクがあります。実際のコードで動作します。これらの検索リンクは、Pygameのドキュメントの新機能で、ユーザーが投稿したひどいサンプルコードを持っていました。彼らはかなり素晴らしいです! – Blckknght
ありがとう!私はすぐにそれをチェックします。 – Petras99