How can I Make appear Button above a Pygame Circle? - python

I have been trying for a long time to try to put a button above a multicoloured circle but I can't, when I do it the button is covered by the circle, I have tried several things but none of them work.
import pygame, math
from pygame import font
pygame.init()
def lerp_color(colors, value):
fract, index = math.modf(value)
color1 = pygame.Color(colors[int(index) % len(colors)])
color2 = pygame.Color(colors[int(index + 1) % len(colors)])
return color1.lerp(color2, fract)
def button(screen, position, text):
font = pygame.font.SysFont("Anton", 50)
text_render = font.render(text, 1, (0, 0, 0))
x, y, w , h = text_render.get_rect()
x, y = position
pygame.draw.line(screen, (150, 150, 150), (x, y), (x + w , y), 5)
pygame.draw.line(screen, (150, 150, 150), (x, y - 2), (x, y + h), 5)
pygame.draw.rect(screen, (100, 100, 100), (x, y, w , h))
return screen.blit(text_render, (x, y))
pygame.init()
screen = pygame.display.set_mode((800, 800))
screen2 = pygame.display.set_mode((800,800))
clock = pygame.time.Clock()
start_time = pygame.time.get_ticks()
b1 = button(screen,(310,200), "Faster")
variable = 1
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
start_time = pygame.time.get_ticks()
screen.blit(b1)
colors = [(000,000,000), (255,0,0), (000,255,000), (000,000,255), (255,0,255), (000,255,255), (255,255,000), (255,000,255)]
value = (pygame.time.get_ticks() - start_time) / 1000
current_color = lerp_color(colors, value)
pygame.draw.circle(screen2, current_color, screen.get_rect().center, 500)
print(value)
pygame.display.flip()
pygame.quit()
exit()

You must draw the button after the circle:
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
start_time = pygame.time.get_ticks()
colors = [(000,000,000), (255,0,0), (000,255,000), (000,000,255), (255,0,255), (000,255,255), (255,255,000), (255,000,255)]
value = (pygame.time.get_ticks() - start_time) / 1000
print(value)
current_color = lerp_color(colors, value)
screen.fill(0)
pygame.draw.circle(screen2, current_color, screen.get_rect().center, 500)
button(screen,(310,200), "Faster")
pygame.display.flip()
Note, The instruction screen.blit(b1) does not do what you expect. It only cuases an error. blit does not return an object, it only returns the rectangular area that was affected by the operation. Therefore the button returns a pygame.Rect object and bl is just a rectangle.

Related

How to add a background and simple square as a character for a single screen platformer in pygame?

I’ve tried adding a background for the game, but as it pops up a second later it goes away It is white and everything else with the background is fine, but this is the only issue. Also, I tried to make a square to use as a character but it just won’t pop up.
import pygame, sys
from pygame.locals import QUIT
background_colour = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
(width, height) = (900, 450)
screen = pygame.display.set_mode((width, height))
screen.fill(background_colour)
pygame.display.flip()
pygame.display.update()
pygame.init()
dt = 0
x = 30
y = 30
w = 30
h = 30
a = 30
e = 30
l = 30
k = 30
def draw():
square = pygame.draw.rect(screen, RED, pygame.Rect(30, 30, 60, 60), 2)
pygame.display.flip()
def screen_bound():
global x
global y
global w
global h
global a
global e
global l
global k
# hit right wall
if ((x+w) > width):
x = width - w
# hit floor
if ((y+h) > height):
y = height - h
# hit left wall
if (x < 0):
x = 0
# hit roof
if (y < 0):
y = 0
def movement():
global x
global y
GRAVITY = .8
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
y = y - (.5*dt)
if keys[pygame.K_s]:
y = y + (.5*dt)
y = y = GRAVITY
def handle_events():
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
pass
def start():
draw()
movement()
screen_bound()
handle_events()
You need to implement an application loop where you move the objects and redraw the scene in each frame. The typical PyGame application loop has to:
limit the frames per second to limit CPU usage with pygame.time.Clock.tick
handle the events by calling either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by calling either pygame.display.update() or pygame.display.flip()
import pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *background.get_size(), (128, 128, 128), (64, 64, 64)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
[pygame.draw.rect(background, color, rect) for rect, color in tiles]
rect = pygame.Rect(0, 0, 20, 20)
rect.center = window.get_rect().center
speed = 5
# main application loop
run = True
while run:
# limit frames per second
clock.tick(100)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# update the game states and positions of objects dependent on the input
keys = pygame.key.get_pressed()
rect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * speed
rect.y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * speed
border_rect = window.get_rect()
rect.clamp_ip(border_rect)
# clear the display and draw background
window.blit(background, (0, 0))
# draw the scene
pygame.draw.rect(window, (255, 0, 0), rect)
# update the display
pygame.display.flip()
pygame.quit()
exit()

How to create a rhomboid in pygame

I want to create a rhomboid like this. I can not do this I found the solution just for the diamond.
my code:
import pygame as pg
pg.init()
screen = pg.display.set_mode((500, 700))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
BLUE = pg.Color('dodgerblue')
points = [(200, 200), (250, 250), (200, 300), (150, 250)]
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
screen.fill(BG_COLOR)
pg.draw.polygon(screen, BLUE, points)
pg.display.flip()
clock.tick()
Write a function that draws a rhomboid with a with, height and offset:
import pygame as pg
def drawRhomboid(surf, color, x, y, width, height, offset, thickness=0):
points = [
(x + offset, y),
(x + width + offset, y),
(x + width, y + height),
(x, y + height)]
pg.draw.polygon(surf, color, points, thickness)
pg.init()
screen = pg.display.set_mode((500, 700))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
BLUE = pg.Color('dodgerblue')
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
screen.fill(BG_COLOR)
drawRhomboid(screen, BLUE, 50, 50, 300, 200, 100, 3)
pg.display.flip()
clock.tick()
If you want to draw an isometric cube, you just need to calculate the correct points and stick the cube from Rhomboids:
import pygame, math
def drawCube(surf, center, size, angle):
v = pygame.math.Vector2(1, 0)
v.rotate_ip(angle + 45)
lx = round(v.x * size * math.sqrt(2) / 2)
ly = round(v.y * size * math.sqrt(2) / 2)
x, y = center
s = size/2
f1 = [(x+lx, y-s+ly//2), (x-ly, y-s+lx//2), (x-lx, y-s-ly//2), (x+ly, y-s-lx//2)]
pts = [(lx, ly//2), (-ly, lx//2), (-lx, -ly//2), (ly, -lx//2)]
faces = []
for i in range(4):
p0, p1 = pts[i], pts[(i+1) % 4]
f = [(p0[0]+x, p0[1]+y-s), (p1[0]+x, p1[1]+y-s), (p1[0]+x, p1[1]+y+s), (p0[0]+x, p0[1]+y+s)]
faces.append(f)
colors = ["red", "yellow", "green", "blue"]
for face, color in zip(faces, colors):
if (face[0][1] + face[1][1]) / 2 > y-s:
pygame.draw.polygon(surf, color, face, 3)
pygame.draw.polygon(surf, "white", f1, 3)
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
angle = 0
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window_center = window.get_rect().center
window.fill(0)
drawCube(window, window_center, 150, angle)
pygame.display.flip()
angle += 1
pygame.quit()
exit()

Pygame Rendering Text 1 by 1 Causes Lag In Game How Do I Fix This?

when ever I try to display to render this text 1 by 1 it seems to slow my game down and make everything run slow and for some reason it starts the text as well I want it to end when its finished typing the text VIDEO< as you can see in the video the text keeps playing the game keeps lagging for some reason when ever each of the text is rendered on the screen
def show_text(string):
WHITE = (255, 255, 255)
text = ''
font = pygame.font.Font("Alice.ttf", 30)
for i in range(len(string)):
text += string[i]
text_surface = font.render(text, True, WHITE)
text_rect = text_surface.get_rect()
text_rect.center = (700/2, 800/2)
window.blit(text_surface, text_rect)
pygame.display.update()
pygame.time.wait(100)
display_text_animation('Hello World!')
heres the full code there all rects execept the Alice.tff < text style
import pygame, sys
from pygame.locals import *
WINDOW_WIDTH = 500
WINDOW_HEIGHT = 500
pygame.init()
DISPLAYSURF = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
class player:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(DISPLAYSURF,self.color,self.rect)
white = (120,120,120)
player1 = player(150,150,50,50,white)
def display_text_animation(string):
text = ''
for i in range(len(string)):
text += string[i]
font = pygame.font.Font("Alice.ttf", 30)
text_surface = font.render(text, True, white)
text_rect = text_surface.get_rect()
DISPLAYSURF.blit(text_surface, text_rect)
pygame.display.update()
pygame.time.wait(100)
def main():
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_d]:
player1.x += 5
if keys[pygame.K_a]:
player1.x -= 5
DISPLAYSURF.fill((0,0,0))
player1.draw()
display_text_animation('Hello World!')
main()
Do not create the pygame.font.Font object in every frame. Creating a font object is very time consuming. Every time the Font object is created, the font file ("Alice.ttf") must be read and interpreted. Create the Fontobject before the application loop during initialization.
Furthermore do not animate the text in a loop use the application loop. Use pygame.time.get_ticks() to measure the time. Calculate the number of letters to be displayed as a function of time:
font = pygame.font.Font("Alice.ttf", 30)
def display_text_animation(string, start_time):
current_time = pygame.time.get_ticks()
letters = (current_time - start_time) // 100
text = string[:letters]
WHITE = (255, 255, 255)
text_surface = font.render(text, True, WHITE)
text_rect = text_surface.get_rect()
text_rect.center = (700/2, 800/2)
DISPLAYSURF.blit(text_surface, text_rect)
def main():
text = 'Hello World!'
start_time = pygame.time.get_ticks()
clock = pygame.time.Clock()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_d]:
player1.x += 5
if keys[pygame.K_a]:
player1.x -= 5
DISPLAYSURF.fill((0,0,0))
player1.draw()
display_text_animation(text, start_time)
pygame.display.update()
When ever you want to animate a new text you just have to set text and start_time. e.g:
text = "New text"
start_time = pygame.time.get_ticks()
Minimal typewriter effect example:
import pygame
pygame.init()
window = pygame.display.set_mode((500, 150))
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 100)
background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *window.get_size(), (32, 32, 32), (64, 64, 64)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
for rect, color in tiles:
pygame.draw.rect(background, color, rect)
text = 'Hello World'
text_len = 0
typewriter_event = pygame.USEREVENT+1
pygame.time.set_timer(typewriter_event, 100)
text_surf = None
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == typewriter_event:
text_len += 1
if text_len > len(text):
text_len = 0
text_surf = None if text_len == 0 else font.render(text[:text_len], True, (255, 255, 128))
window.blit(background, (0, 0))
if text_surf:
window.blit(text_surf, text_surf.get_rect(midleft = window.get_rect().midleft).move(40, 0))
pygame.display.flip()
pygame.quit()
exit()

how do i set the mouse position around a circle in pygame

I'm trying to create this program in pygame to study code/math, but i don't know how to lock the mouse position around a circle in pygame, any help?
import pygame
pygame.init()
x = 250
y = 250
window = pygame.display.set_mode((500, 500))
pygame.display.set_caption("around circle")
#line to be created
def Lin(xref, yref):
lin = pygame.draw.line(window, (250, 250, 0), (x, y), (xref, yref), 1)
window_open = True
while window_open:
pygame.display.update()
window.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_open = False
mousepos = pygame.mouse.get_pos()
xref = mousepos[0]
yref = mousepos[1]
# 2 circles to get only the border
cir0 = pygame.draw.circle(window, (250, 250, 250), (x, y), 100, 1)
cir1 = pygame.draw.circle(window, (250, 250, 250), (x, y), 99, 1)
Lin(xref, yref)
pygame.quit()
I would do something similar to Mike67 answer, but would take advantage of the features of pygame's Vector's (see docs here).
import pygame
pygame.init()
x = 250
y = 250
radius = 100
center = pygame.Vector2(x, y)
window = pygame.display.set_mode((500, 500))
pygame.display.set_caption("around circle")
#line to be created
def Lin(start_point, vector):
pygame.draw.line(window, (250, 250, 0), start_point, start_point+vector, 1)
window_open = True
while window_open:
pygame.display.update()
window.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_open = False
mousepos = pygame.mouse.get_pos()
line_vector = pygame.Vector2(mousepos) - center
if line_vector.length() > radius:
line_vector.scale_to_length(radius)
# 2 circles to get only the border
cir0 = pygame.draw.circle(window, (250, 250, 250), (x, y), radius, 1)
cir1 = pygame.draw.circle(window, (250, 250, 250), (x, y), radius-1, 1)
Lin(center, line_vector)
pygame.quit()
I modified the Lin() function as well.
EDIT:
From your comment you indicated that you want it to always scale to the side of the circle. In that case just skip the test if line_vector.length() > radius and always scale it. You might be tempted to only scale it if length != radius, but when comparing floating point calculated numbers they are very unlikely to actually be the same (because of the decimal places involved) and you could see if the difference is less than a threshold and call them equal, but it really isn't worth the complication.
import pygame
pygame.init()
x = 250
y = 250
radius = 100
center = pygame.Vector2(x, y)
window = pygame.display.set_mode((500, 500))
pygame.display.set_caption("around circle")
#line to be created
def Lin(start_point, vector):
pygame.draw.line(window, (250, 250, 0), start_point, start_point+vector, 1)
window_open = True
while window_open:
pygame.display.update()
window.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_open = False
mousepos = pygame.mouse.get_pos()
line_vector = pygame.Vector2(mousepos) - center
line_vector.scale_to_length(radius)
# 2 circles to get only the border
cir0 = pygame.draw.circle(window, (250, 250, 250), (x, y), radius, 1)
cir1 = pygame.draw.circle(window, (250, 250, 250), (x, y), radius-1, 1)
Lin(center, line_vector)
pygame.quit()
To keep the line in the circle, just adjust the mouse x/y coordinates relative to the mouse distance from the center.
Here is the updated code:
import pygame
import math
pygame.init()
x = 250
y = 250
window = pygame.display.set_mode((500, 500))
pygame.display.set_caption("around circle")
#line to be created
def Lin(xref, yref):
lin = pygame.draw.line(window, (250, 250, 0), (x, y), (xref, yref), 1)
window_open = True
while window_open:
pygame.display.update()
window.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_open = False
mousepos = pygame.mouse.get_pos()
xref = mousepos[0]
yref = mousepos[1]
# get mouse distance from center
dist = math.sqrt((xref-x)**2 + (yref-y)**2)
if (dist > 100): # if mouse outside circle, adjust x/y proportionally
xref = x + (xref - x) * (100/dist)
yref = y + (yref - y) * (100/dist)
# 2 circles to get only the border
cir0 = pygame.draw.circle(window, (250, 250, 250), (x, y), 100, 1)
cir1 = pygame.draw.circle(window, (250, 250, 250), (x, y), 99, 1)
Lin(xref, yref)
pygame.quit()

Why does the game lag when I load an image and when I display pygame shapes?

Just out of curiosity, why does a game lag when I load an image as well as display pygame shapes for example rectangles, circles and ellipses. I have this code where you shoot the ghosts that fall down (I'm still working on the shooting part). I made the cannon out of pygame shapes. But when I run it the images of the ghosts are prefect but the images of the cannons lag and disappears and reappear and so on. Is there any way to stop this lag or disappear and reappear thing? I'm running python 2.6 with windows vista.
import pygame, sys, random, math
from pygame.locals import *
WINDOWHEIGHT = 600
WINDOWWIDTH = 600
FPS = 30
BACKGROUNDCOLOR = (255, 255, 255)
TEXTCOLOR = (0, 0, 0)
WHITE = (255, 255, 255)
BLACK = ( 0, 0, 0)
BROWN = (139, 69, 19)
DARKGRAY = (128, 128, 128)
BGCOLOR = WHITE
GHOSTSPEED = 10
GHOSTSIZE = 20
ADDNEWGHOSTRATE = 8
def keyToPlayAgain():
while True:
for event in event.type.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
return
def getAngle(x1, y1, x2, y2):
# Return value is 0 for right, 90 for up, 180 for left, and 270 for down (and all values between 0 and 360)
rise = y1 - y2
run = x1 - x2
angle = math.atan2(run, rise) # get the angle in radians
angle = angle * (180 / math.pi) # convert to degrees
angle = (angle + 90) % 360 # adjust for a right-facing sprite
return angle
def Text(text, font, surface, x, y):
textobj = font.render(text, 2, TEXTCOLOR)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
pygame.init()
mainClock = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWHEIGHT, WINDOWWIDTH))
pygame.display.set_icon(pygame.image.load('s.icon'))
pygame.display.set_caption('Ghost Invasion Pacfighters')
pygame.mouse.set_visible(False)
font = pygame.font.SysFont(None, 48)
gameOverSound = pygame.mixer.Sound('gameover.wav')
pygame.mixer.music.load('background.mid')
ghostImage = pygame.image.load('ghosts.png')
dotImage = pygame.image.load('dot.png')
dotRect = dotImage.get_rect()
creditsPage = pygame.image.load('credits.png')
titlePage = pygame.image.load('title.png')
pygame.time.wait(10000)
DISPLAYSURF.blit(creditsPage, (0, 0))
pygame.time.wait(10000)
DISPLAYSURF.blit(titlePage, (0, 0))
pygame.display.update()
cannonSurf = pygame.Surface((100, 100))
cannonSurf.fill(BGCOLOR)
pygame.draw.circle(cannonSurf, DARKGRAY, (20, 50), 20)
pygame.draw.circle(cannonSurf, DARKGRAY, (80, 50), 20)
pygame.draw.rect(cannonSurf, DARKGRAY, (20, 30, 60, 40))
pygame.draw.circle(cannonSurf, BLACK, (80, 50), 15)
pygame.draw.circle(cannonSurf, BLACK, (80, 50), 20, 1)
pygame.draw.circle(cannonSurf, BROWN, (30, 70), 20)
pygame.draw.circle(cannonSurf, BLACK, (30, 70), 20, 1)
health = 100
score = 0
topScore = 0
while True:
ghosts = []
moveLeft = moveRight = moveUp = moveDown = False
reverseCheat = slowCheat = False
ghostAddCounter = 0
pygame.mixer.music.play(-1, 0.0)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_x:
bombs()
elif event.type == ESCAPE:
pygame.quit()
sys.exit()
mousex, mousey = pygame.mouse.get_pos()
for cannonx, cannony in ((100, 500), (500, 500)):
degrees = getAngle(cannonx, cannony, mousex, mousey)
rotatedSurf = pygame.transform.rotate(cannonSurf, degrees)
rotatedRect = rotatedSurf.get_rect()
rotatedRect.center = (cannonx, cannony)
DISPLAYSURF.blit(rotatedSurf, rotatedRect)
pygame.draw.line(DISPLAYSURF, BLACK, (mousex - 10, mousey), (mousex + 10, mousey))
pygame.draw.line(DISPLAYSURF, BLACK, (mousex, mousey - 10), (mousex, mousey + 10))
pygame.draw.rect(DISPLAYSURF, BLACK, (0, 0, WINDOWWIDTH, WINDOWHEIGHT), 1)
pygame.display.update()
if not reverseCheat and not slowCheat:
ghostAddCounter += 1
if ghostAddCounter == ADDNEWGHOSTRATE:
ghostAddCounter = 0
newGhost = {'rect': pygame.Rect(random.randint(0, WINDOWWIDTH-GHOSTSIZE), 0 - GHOSTSIZE, GHOSTSIZE, GHOSTSIZE),
'speed': (GHOSTSIZE),
'surface':pygame.transform.scale(ghostImage, (GHOSTSIZE, GHOSTSIZE)),
}
ghosts.append(newGhost)
for s in ghosts:
if not reverseCheat and not slowCheat:
s['rect'].move_ip(0, s['speed'])
elif reverseCheat:
s['rect'].move_ip(0, -5)
elif slowCheat:
s['rect'].move_ip(0, -1)
for s in ghosts[:]:
if s['rect'].top > WINDOWHEIGHT:
health -= 10
DISPLAYSURF.fill(BACKGROUNDCOLOR)
Text('Score: %s' % (score), font, DISPLAYSURF, 10, 0)
Text('Top score: %s' % (topScore), font, DISPLAYSURF, 10, 40)
Text('Health: %s' % (health), font, DISPLAYSURF, 10, 560)
for s in ghosts:
DISPLAYSURF.blit(s['surface'], s['rect'])
pygame.display.update()
mainClock.tick(FPS)
pygame.mixer.music.stop()
gameOverSound.play()
Text('GAME OVER', font, windowSurface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3))
pygame.display.update()
keyToPlayAgain()
pygame.display.update()
gameOverSound.stop()
The problem is that you draw your cannons, update the display, then clear the display, draw the other stuff, and update the display again. You basically never see the falling ghosts and the cannons at the same time. This results in the flickering you see.
So remove pygame.display.update() from this for loop
for cannonx, cannony in ((100, 500), (500, 500)):
...
pygame.display.update()
and put DISPLAYSURF.fill(BACKGROUNDCOLOR) at the top of your while loop (or at least before you draw anything):
while True:
for event in pygame.event.get():
...
mousex, mousey = pygame.mouse.get_pos()
DISPLAYSURF.fill(BACKGROUNDCOLOR)
for cannonx, cannony in ((100, 500), (500, 500)):
...
It's best to clear the background once at the start of your code that draws everything, and call pygame.display.update() once at the end of that drawing code.

Categories

Resources