ball()クラスは "for event in pygame.event.get()"のKEYDOWN/KEYUPから情報を取得しないため、ボールは移動しません画面全体でそれは右の座標に表示されますが、私を混乱させる移動に失敗します。コードは次のとおりです。Pygameスプライトは "event.type KEYDOWN"で移動しません
import pygame, sys
from pygame.locals import *
pygame.init()
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
darkBlue = (0,0,128)
white = (255,255,255)
black = (0,0,0)
pink = (255,200,200)
background = pygame.image.load('background.jpg')
screen = pygame.display.set_mode((640,521),0,32)
pygame.display.set_caption('V1.1')
s = 0 #s and s2 for displaying score later on
s2 = 0
class Ball():
def __init__(self):
ball = pygame.sprite.Sprite()
ball.image = pygame.image.load('red_ball.png').convert()
ball.rect = ball.image.get_rect()
ball.image.set_colorkey((white))
screen.blit(ball.image,(x,y))
pygame.display.update()
def update(self):
if x > 640:
x = 0
if x < 0:
x = 640
def main():
while 1:
screen.blit(background, (0,0))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
Ball()
pygame.display.update()
x,y = 285, 430
m_x, m_y = 0,0
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_a:
m_x = -4
s+=1
elif event.key == K_d:
m_x = +4
s2+=1
elif event.type == KEYUP:
if event.key == K_a:
m_x = 0
elif event.key == K_d:
m_x = 0
x+= m_x
y+= m_y
main()