当我运行该程序时,它会运行大约1秒然后停止但是当我运行它时没有任何弹出。昨天我问了一个关于这个的问题,人们告诉我只是为每个对象上课,所以我尝试了但是现在什么都没有出现。任何人都可能知道这里的问题是什么?
import sys
import pygame
import math
import random
class Paddle():
def __init__(self, background, x_axis, height):
self.screen = screen
(sw, sh) = screen.get_size()
self.x = x - 25
self.y = sh / 2 - self.h / 2
self.h = h
self.w = 20
self.max_y = sh - height
self.min_y = 0
self.speed = 0
def moving(self, amount):
self.speed = amount
self.y = self.y + amount
if self.y > self.max_y:
self.y = self.max_y
elif self.y < self.min_y:
self.y = self.min_y
def draw(self):
pygame.draw.rect(self.screen, (255, 255, 255), (self.x, self.y, self.w, self.h))
class Ball():
def __init__(self, screen):
self.screen = screen
self.w = 50
self.h = 50
self.playClick = False
self.reset()
def reset(self):
(sw, sh) = self.screen.get_size()
self.x = sw / 2 - 25
self.y = sh / 2 - 25
angle = random() * math.radians(20)
speed = 10
self.dir_x = speed * math.cos(ball_angle)
self.dir_y = speed * math.sin(ball_angle)
if random() > 0.5:
self.dir_x = -self.dir_x
elif random() > 0.5:
self.dir_y = -self.dir_y
self.timer = 2
def updating(self, time, p1, p2):
if self.timer > 0:
self.timer -= time
else:
self.x += self.dir_x
self.y += self.dir_y
(sw, sh) = self.screen.get_size()
if self.x_axis + 50 < 0:
self.reset()
elif self.x_axis > side_width:
self.reset()
else:
if (self.y < 0 and self.dir_y < 0) or (self.y + self.h > sh and self.dir_y > 0):
self.dir_y = -self.dir_y
self.playClick = True
selfrect = pygame.Rect(self.x_axis, self.y_axis, self.width, self.height)
p1rect = pygame.Rect(p1.x, p1.y, p1.w, p1.h)
p2rect = pygame.Rect(p2.x, p2.y, p2.w, p2.h)
if selfrect.colliderect(p1rect) and self.dir_x < 0:
self.dir_x = -self.dir_x
self.dir_y += p1.speed
self.playClick = True
if selfrect.colliderect(p2rect) and self.dir_x > 0:
self.dir_x = -self.dir_x
self.dir_y += p2.speed
self.playClick = True
if self.dir_y > 15:
self.dir_y = 15
elif self.dir_y < -15:
self.dir_y = -15
def draw(self, click):
pygame.draw.rect(self.screen, (255, 255, 255), (self.x, self.y, self.w, self.h))
if self.playclick:
click.play()
self.playClick = False
def main():
pygame.init()
screen = pygame.display.set_mode((1000, 800))
clock = pygame.time.Clock()
p1 = Paddle(screen, 100, 150)
p2 = Paddle(screen, 900, 150)
ball = Ball(screen)
background_image = pygame.image.load("RPGMAP.png").convert()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
p1.moving(-10)
if event.key == pygame.K_s:
p1.moving(10)
if event.key == pygame.K_UP:
p2.moving(-10)
if event.key == pygame.K_DOWN:
p2.moving(10)
if event.type == pygame.KEYUP:
if event.key == pygame.K_w:
p1.moving(0)
if event.key == pygame.K_s:
p1.moving(0)
if event.key == pygame.K_UP:
p2.moving(0)
if event.key == pygame.K_DOWN:
p2.moving(0)
ball.update(1/60, p1, p2)
screen.blit(background_image, [0, 0])
p1.draw()
p2.draw()
pygame.display.flip()
clock.tick(60)
main()
答案 0 :(得分:0)
您不会在任何地方声明screen
:
self.screen = screen <- NameError
您也不传递x或h:
self.x = x - 25 <- NameError
self.h = h <- NameError
您还有许多其他未在任何地方声明的变量和属性。