I am creating a pong game in python using pygame, and I wish to draw a dashed line down the middle. I think I have the correct code to draw the dashed line but it will not work.
for i in range(0, HEIGHT, HEIGHT//20):
if i % 2 == 1:
continue
pygame.draw.rect(win, WHITE, (WIDTH//2 - 5, i, 10, HEIGHT//20))
I tried this and expected it to draw a dashed line but it didn't. Here is my whole code:
import pygame
pygame.init()
WIDTH, HEIGHT = 1200, 800
NAME = "Pong"
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(NAME)
WHITE = (255,255,255)
BLACK = (0,0,0)
PADDLE_WIDTH, PADDLE_HEIGHT = 10, 200
FPS = 60
class Paddle:
COLOUR = WHITE
VEL = 5
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
def draw(self, win):
pygame.draw.rect(win, self.COLOUR, (self.x, self.y, self.width, self.height))
def move(self, up):
if up:
self.y -= self.VEL
else:
self.y += self.VEL
def draw(win, paddles):
win.fill(BLACK)
for paddle in paddles:
paddle.draw(win)
for i in range(0, HEIGHT, HEIGHT//20):
if i % 2 == 1:
continue
pygame.draw.rect(win, WHITE, (WIDTH//2 - 5, i, 10, HEIGHT//20))
pygame.display.update()
def handle_paddle_movement(keys, left_paddle, right_paddle):
if keys[pygame.K_w] and left_paddle.y - left_paddle.VEL > 0:
left_paddle.move(True)
if keys[pygame.K_s] and left_paddle.y + left_paddle.VEL + left_paddle.height < HEIGHT:
left_paddle.move(False)
if keys[pygame.K_UP] and right_paddle.y - right_paddle.VEL > 0:
right_paddle.move(True)
if keys[pygame.K_DOWN] and right_paddle.y + right_paddle.VEL + right_paddle.height < HEIGHT:
right_paddle.move(False)
def main():
run = True
clock = pygame.time.Clock()
left_paddle = Paddle(PADDLE_WIDTH, HEIGHT//2 - PADDLE_HEIGHT//2, PADDLE_WIDTH, PADDLE_HEIGHT)
right_paddle = Paddle(WIDTH - PADDLE_WIDTH*2, HEIGHT//2 - PADDLE_HEIGHT//2, PADDLE_WIDTH, PADDLE_HEIGHT)
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
keys = pygame.key.get_pressed()
handle_paddle_movement(keys, left_paddle, right_paddle)
draw(WIN, [left_paddle, right_paddle])
pygame.quit()
if __name__ == '__main__':
main()
This is the rectangle you are drawing each loop:
# x y width height
(WIDTH//2 - 5, i, 10, HEIGHT//20)
You should replace the HEIGHT//20
with a smaller value so each rect isn't so tall. Try something like this instead:
pygame.draw.rect(win, WHITE, (WIDTH//2 - 5, i, 10, 20))
In your loop iterates with a step of HEIGHT//20
. There for i
is always divisible by 2
with out a rest. There are many options to solve your problem. eg:
Option 1 use enumerate
:
for i, y in enumerate(range(0, HEIGHT, HEIGHT//20)):
if i % 2 == 1:
continue
pygame.draw.rect(win, WHITE, (WIDTH//2 - 5, y, 10, HEIGHT//20))
Option 2 iterate with a step of HEIGHT//10
:
for y in range(0, HEIGHT, HEIGHT//10):
pygame.draw.rect(win, WHITE, (WIDTH//2 - 5, y, 10, HEIGHT//20))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.