我正在尝试绘制一个静态矩形和一个圆圈(试图制作一个简单版本的乒乓)。圆圈显示正常,但我无法显示矩形!我是python的新手,我不知道该怎么做。我用paddle(矩形)的类/方法导入了文件,但这似乎不是问题所在。任何帮助表示赞赏!
这是基本代码(L1_base.py):
import math
import random
import sys, pygame
from pygame.locals import *
import ball
import colors
import paddle
# draw the scene
def draw(screen, ball1) :
screen.fill((128, 128, 128))
ball1.draw_ball(screen)
#function to start up the main drawing
def main():
pygame.init()
width = 600
height = 600
screen = pygame.display.set_mode((width, height))
ball1 = ball.Ball(300, 1, 40, colors.RED)
while 1:
for event in pygame.event.get():
if event.type == QUIT: sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
print("right key")
if event.key == pygame.K_LEFT:
print("left key")
draw(screen, ball1)
pygame.display.flip()
if __name__ == '__main__':
main()
这是带有paddle类/方法的文件(paddle.py):
import pygame
class paddle:
def __init__(self, x, y, color, width, height):
self.x = x
self.y = y
self.color = c
self.width = w
self.height = h
def draw_paddle(self, screen):
pygame.draw.rect(screen, self.color,
pygame.Rect(self.x, self.y, self.width, self.height), 0)
如果你需要它,这里是圆圈/球(ball.py)的代码:
import pygame
class Ball:
def __init__(self, x, y, radius, color):
self.x = x
self.y = y
self.radius = radius
self.color = color
def draw_ball(self, screen):
pygame.draw.ellipse(screen, self.color,
pygame.Rect(self.x, self.y, self.radius, self.radius))
提前感谢您的帮助!