0

This is my first real code project. I am trying to create a Crash gambling project for a math project at School.

My current problem is that when I flip my arc it draws 2 separate arcs. I don't know why it does this but if anyone can help it would be much appreciated.

Here is my code:

import pygame
from math import sin, cos, radians

grid = pygame.image.load('grid3.jpg')
wn = pygame.display.set_mode((600, 600))
wn2 = pygame.display.set_mode((600, 600))
clock = pygame.time.Clock()
r = 600
a = 0
b = 0

def x_y(r, i, a, b):
  return (int(r * cos(radians(i)) + a), int(r * sin(radians(i)) + b))
for i in range(0, 90, 1): 
  clock.tick(30)
  pygame.draw.line(wn, (255, 255, 255), x_y(r, i, a, b), x_y(r, i+1, a, b), 10)
  wn2.blit(wn, (0,0))
  wn.blit(grid,(0,0))
  wn2.blit(pygame.transform.rotate(wn2, -90), (0, 0))
  wn = pygame.transform.flip(wn2, True, False)
  pygame.display.update()
D_00
  • 1,440
  • 2
  • 13
  • 32

1 Answers1

2

When you draw onto a display Surface, it won't remove the other existing elements. Take this code as an example:

import pygame
from pygame.locals import *

screen = pygame.display.set_mode((320, 240))
pygame.draw.rect(screen, (0, 255, 0), Rect(50, 20, 100, 100))
pygame.draw.rect(screen, (255, 0, 0), Rect(120, 100, 100, 100))
pygame.display.flip() # same as pygame.display.update() (with no arguments)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()

When executing this, you will see 2 squares appear, the second one being drawn not preventing the first one from still appearing, even though you updated the screen.

As a display surface is also a transparent surface, blitting a display Surface onto another will not erase its content.
To do that, you can call the Surface.fill function.


A few optional (but some I recommend) improvements to your code:

  • Implement a game loop to your game/simulation to namely get the user events and preventing your game from constantly crashing after a while
  • Use pygame.display.flip() instead of pygame.display.update() (the latter is useful when you want to only update part of the screen)
  • You are creating 2 screen surfaces, but that's not possible. wn and wn2 are referring to the same surface. Use pygame.Surface for creating other surfaces.
  • Try to leave the main display surface as it is, and work with other surfaces rather than changing the entire screen by transforming it, as this can be confusing when debugging.

I hope that helped!

D_00
  • 1,440
  • 2
  • 13
  • 32