I am trying to make my code display a text in the middle of the screen once a square goes past right by 500 pixels, but it does seem to be displaying with my If condition. I dont know what I am doing wrong.
import pygame
pygame.init()
displaywidth=500
displayheight=500
gameDisplay = pygame.display.set_mode((displaywidth, displayheight))
pygame.display.set_caption("First Game")
red = (0,255,0)
font=pygame.font.Font(None, 20)
#Class
def Message(msg, color):
screen_text=font.render(msg,True,color)
gameDisplay.blit(screen_text,[displaywidth/2,displayheight/2])
win = pygame.display.set_mode((displaywidth, displayheight))
x = 50
y = 50
width = 40
height = 40
vel = 5
x1 = 0
y1 = 0
width1 = 40
height1 = 40
vel2 = 100
vel3=100
x2 = 100
y2 = 100
pygame.draw.rect(win, (0, 255, 0), (x, y, width, height))
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= vel2
elif keys[pygame.K_RIGHT]:
x += vel2
elif keys[pygame.K_UP]:
y1-=vel3
elif keys[pygame.K_DOWN]:
y1+=vel3
if x > 500:
Message("meow", red)
pygame.display.update()
print("pew")
elif x < 0:
x = 50
elif y1 > 500:
y1 = 450
elif y1 < 0:
y1 = 50
print(x)
print(y)
win.fill((0, 0, 0))
meow = pygame.draw.rect(win, (0, 255, 0), (x, y1, width, height))
pygame.draw.rect(win, (160, 0, 0), (x1, y1, width1, height1))
pygame.display.update()
pygame.quit()
My print command appears to be working but I dont know why its not displaying.
The issue is that you've 2 calls to pygame.display.update() in your code, but the display is cleared immediately after the first one:
if x > 500:
Message("meow", red)
pygame.display.update() # <----
print("pew")
# [...]
win.fill((0, 0, 0)) # <----
Clear the display before anything is drawn and do a single pygame.display.update() at the end of the main application loop:
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= vel2
elif keys[pygame.K_RIGHT]:
x += vel2
elif keys[pygame.K_UP]:
y1-=vel3
elif keys[pygame.K_DOWN]:
y1+=vel3
if x < 0:
x = 50
elif y1 > 500:
y1 = 450
elif y1 < 0:
y1 = 50
# clear dispaly
win.fill((0, 0, 0))
# draw scene
if x > 500:
Message("meow", red)
print("pew")
meow = pygame.draw.rect(win, (0, 255, 0), (x, y1, width, height))
pygame.draw.rect(win, (160, 0, 0), (x1, y1, width1, height1))
# update display
pygame.display.update()
Related
This question already has answers here:
How can i shoot a bullet with space bar?
(1 answer)
How do I detect collision in pygame?
(5 answers)
How do I stop more than 1 bullet firing at once?
(1 answer)
Closed 1 year ago.
This program currently makes two squares appear on the screen when you press start, the one on the left is controlled using WASD whilst the one on the right is controlled using the arrow keys. I want to make it so that when you click spacebar, the square on the left shoots a red rectangle (the bullet) to the right of the screen and if it hits the other square, then the health goes down by one. If it doesn't hit the other square, the bullet hits the edge of the screen and disappears (I would also like the other square to do this too but the other way around and instead of pressing space you press right control). The health variable for both squares is on lines 9 and 10. Can somebody please tell me how to do this?
Full Code (The two key presses that I would like to make the chosen character shoot are lines 60 and 72):
import pygame
import sys
import os
pygame.init()
FPS = 60
HEALTH_L = 3
HEALTH_R = 3
start_smallfont = pygame.font.SysFont('Corbel', 45)
start_text = start_smallfont.render('Start', True, (255, 255, 255))
rect_smallfont = pygame.font.SysFont('Corbel', 33)
rect_text = rect_smallfont.render('You', True, (255, 255, 255))
x = 630
y = 325
x2 = 170
y2 = 325
vel = 0.1
startWIDTH, startHEIGHT = 170, 80
screenWIDTH, screenHEIGHT = 800, 720
WIN = pygame.display.set_mode((screenWIDTH, screenHEIGHT))
pygame.display.set_caption(":D")
def main():
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if 800/2-85 <= mouse[0] <= 800/2-85+startWIDTH and 720/2-40 <= mouse[1] <= 720/2-40+startHEIGHT:
clock.tick(FPS)
run = False
while True:
global x, y, x2, y2, movingx
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
WIN.fill((0, 0, 0))
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
if keys[pygame.K_RIGHT] and x < screenWIDTH - 50 - vel:
x += vel
if keys[pygame.K_UP] and y > vel:
y -= vel
if keys[pygame.K_DOWN] and y < screenHEIGHT - 50 - vel:
y += vel
if keys[pygame.K_SPACE]:
#PUT BULLET FUNCTION HERE
if keys[pygame.K_a] and x2 > vel:
x2 -= vel
if keys[pygame.K_d] and x2 < screenWIDTH - 50 - vel:
x2 += vel
if keys[pygame.K_w] and y2 > vel:
y2 -= vel
if keys[pygame.K_s] and y2 < screenHEIGHT - 50 - vel:
y2 += vel
if keys[pygame.K_RCTRL]:
#PUT BULLET FUNCTION HERE
pygame.draw.rect(WIN, (255, 0, 0), (x, y, 50, 50))
pygame.draw.rect(WIN, (255, 0, 0), (x2, y2, 50, 50))
pygame.display.update()
mouse = pygame.mouse.get_pos()
if 800/2-85 <= mouse[0] <= 800/2-85+startWIDTH and 720/2-40 <= mouse[1] <= 720/2-40+startHEIGHT:
pygame.draw.rect(WIN, (255, 91, 91), (800/2-85, 720/2-40, startWIDTH, startHEIGHT))
else:
pygame.draw.rect(WIN, (255, 0, 0), (800/2-85, 720/2-40, startWIDTH, startHEIGHT))
WIN.blit(start_text, (800/2-85+42,720/2-40+20))
pygame.display.update()
if __name__ == "__main__":
main()
How can I add a sprite when no keys are pressed? I have each four directions covered when the arrow keys are pressed but when they are released the sprite goes obviously but cant think of how to add it. I tried adding else statements and other things best thing i got was the standing forward sprite being underneath the others but cant seem to get it to go when one of the arrow keys is pressed and return when they are relased.
Any help appreciated. Thanks in advance.
my code:
import pygame
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (200, 0, 0)
GREEN = (0, 200, 0)
BLUE = (0, 0, 200)
# background
background = pygame.image.load('Playing Game state 2.png')
# character
standingforward = pygame.image.load('standingforward.png')
standingdownward = pygame.image.load('standingdownwards.png')
standingleft = pygame.image.load('standingleft.png')
standingright = pygame.image.load('standingright.png')
# player variables
x = 375
y = 525
w = 50
h = 50
vel = 0.5
screenWidth = 800
screenHeight = 575
screen = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption("FROGGER")
sprite = pygame.draw.rect
running = True
while running:
# sprites
screen.blit(background, (0, 0))
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
screen.blit(standingleft, (x, y))
if keys[pygame.K_RIGHT]:
screen.blit(standingright, (x, y))
if keys[pygame.K_DOWN]:
screen.blit(standingdownward, (x, y))
if keys[pygame.K_UP]:
screen.blit(standingforward, (x, y))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
# controls
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= vel
if x < 0:
x = 0
if keys[pygame.K_RIGHT]:
x += vel
if x > screenWidth-w:
x = screenWidth-w
if keys[pygame.K_UP]:
y -= vel
if y < 0:
y = 0
if keys[pygame.K_DOWN]:
y += vel
if y > screenHeight-h:
y = screenHeight-h
pygame.quit()
Add a variable that refers to the current image. Change the variable when a key is pressed. Draw the current image in the application loop:
current_image = standingright
running = True
while running:
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
current_image = standingleft
if keys[pygame.K_RIGHT]:
current_image = standingright
if keys[pygame.K_DOWN]:
current_image = standingdownward
if keys[pygame.K_UP]:
current_image = standingforward
screen.blit(background, (0, 0))
screen.blit(current_image, (x, y))
# [...]
I am a python beginner. I want to recreate chrome dino game. the random rectangle won't stop and the loop runs forever...please help me to stop the loop and make rectangles move.
Code:
import pygame
import random
pygame.init()
win = pygame.display.set_mode((500, 500))
#red rectangle(dino)
x = 20
y = 400
width = 30
height = 42
gravity = 5
vel = 18
black = (0, 0, 0)
#ground
start_pos = [0, 470]
end_pos = [500, 470]
#cactus
x1 = 20
y1 = 30
white = (2, 200, 200)
run = True
clock = pygame.time.Clock()
while run:
clock.tick(30)
pygame.time.delay(10)
win.fill(black)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#random rectangle generation
for i in range(1):
width2 = random.randint(25, 25)
height2 = random.randint(60, 60)
top = random.randint(412, 412)
left = random.randint(300, 800)
rect = pygame.draw.rect(win, white, (left, top, width2,height2))
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
y = y - vel
else:
y = min(428, y + gravity)
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.draw.line(win, white, start_pos, end_pos, 2)
pygame.display.update()
pygame.display.flip()
pygame.quit()
pygame.draw.rect() des not "generate" a rectangle, it draws a rectangle on a surface.
pygame.Rect is a rectangle object. Create an instance of pygame.Rect before the main application loop:
obstracle = pygame.Rect(500, random.randint(0, 412), 25, 60)
Change the position of the rectangle:
obstracle.x -= 3
if obstracle.right <= 0:
obstracle.y = random.randint(0, 412)
And draw the rectangle to the window surface:
pygame.draw.rect(win, white, obstracle)
Example:
import pygame
import random
pygame.init()
win = pygame.display.set_mode((500, 500))
start_pos = [0, 470]
end_pos = [500, 470]
gravity = 5
vel = 18
black = (0, 0, 0)
white = (2, 200, 200)
hit = 0
dino = pygame.Rect(20, 400, 30, 40)
obstracles = []
number = 5
for i in range(number):
ox = 500 + i * 500 // number
oy = random.randint(0, 412)
obstracles.append(pygame.Rect(ox, oy, 25, 60))
run = True
clock = pygame.time.Clock()
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
dino.y -= vel
else:
dino.y = min(428, dino.y + gravity)
for obstracle in obstracles:
obstracle.x -= 3
if obstracle.right <= 0:
obstracle.x = 500
obstracle.y = random.randint(0, 412)
if dino.colliderect(obstracle):
hit += 1
win.fill(black)
color = (min(hit, 255), max(255-hit, 0), 0)
pygame.draw.rect(win, color, dino)
for obstracle in obstracles:
pygame.draw.rect(win, white, obstracle)
pygame.draw.line(win, white, start_pos, end_pos, 2)
pygame.display.update()
pygame.quit()
I have a pause menu for a game im working on for school. If the user clicks 'p' it launches the pause menu. And the pause menu has a function that if a user clicks a button the user will be launched back into the game. Problem is after they are launched into the game the 'p' function to pause doesn't work anymore. I'm not sure if i looped it correctly.
the pong game
import pygame
black = (0,0,0)
white = (255,255,255)
pygame.init()
size = 800,600
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Basketball Shootout")
done = False
clock = pygame.time.Clock()
def player1(x1, y1, xsize, ysize):
pygame.draw.rect(screen, black, [x1, y1, xsize, ysize])
def player2(x2, y2, xsize, ysize):
pygame.draw.rect(screen, black, [x2,y2,xsize,ysize])
def ball(ballx, bally):
pygame.draw.circle(screen, black, [ballx,bally],20)
def Score1(score1):
font = pygame.font.Font("Minecraft.ttf" ,50)
text = font.render(str(score1), True, white)
screen.blit(text, [160, 550])
def Score2(score2):
font = pygame.font.Font("Minecraft.ttf" ,50)
text = font.render(str(score2), True, white)
screen.blit(text, [610, 550])
x1 = 20
y1 = 175
xsize = 35
ysize = 150
speed1 = 0
x2 = 740
y2 = 175
speed2 = 0
ballx = 550
bally = 250
speedx = 8
speedy = 5
score1 = 0
score2 = 0
bg = pygame.image.load("pongbg2.png")
rect1 = pygame.Rect(50,510,100,50)
def pausescreen():
import pausescreen
display_game = True
game_page = 1
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pygame.draw.rect(screen, (255, 255, 255), rect1)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
speed1 = -10
if event.key == pygame.K_s:
speed1 = 10
if event.key == pygame.K_UP:
speed2 = -10
if event.key == pygame.K_DOWN:
speed2 = 10
if event.type == pygame.KEYUP:
if event.key == pygame.K_w:
speed1 = 0
if event.key == pygame.K_s:
speed1 = 0
if event.key == pygame.K_UP:
speed2 = 0
if event.key == pygame.K_DOWN:
speed2 = 0
if event.key == pygame.K_p:
pausescreen()
screen.blit(bg, (0, 0))
player1(x1, y1, xsize, ysize)
player2(x2, y2, xsize, ysize)
ball(ballx,bally)
Score1(score1)
Score2(score2)
y1 += speed1
y2 += speed2
ballx += speedx
bally += speedy
if y1 < 0:
y1 = 0
if y1 > 350:
y1 = 350
if y2 < 0:
y2 = 0
if y2 > 350:
y2 = 350
if ballx+20 > x2 and bally-20 > y2 and bally+20 < y2+ysize and ballx < x2+3:
speedx = -speedx
if ballx-20 < x1+35 and bally-20 > y1 and bally+20 < y1+ysize and ballx > x1+38:
speedx = -speedx
if bally > 477 or bally < 23:
speedy = -speedy
if ballx < 13:
score2 += 1
ballx = 350
bally = 250
if ballx > 750:
score1 += 1
ballx = 350
bally = 250
pygame.display.flip()
clock.tick(60)
pygame.quit()
now here is my pause menu code.
import pygame
import sys
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
pygame.init()
# Set the height and width of the screen
size = [800, 600 ]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Basketball Shootout")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
#Rectangles
rect1 = pygame.Rect(300,300,205,80)
rect2 = pygame.Rect(300,400,205,80)
#Font
font3 = pygame.font.Font("Minecraft.ttf", 40)
def playerpong():
import playerpong
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(BLACK)
pygame.draw.rect(screen, GREEN, rect1)
pygame.draw.rect(screen, RED, rect2)
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if rect1.collidepoint(event.pos):
playerpong()
if rect2.collidepoint(event.pos):
pygame.quit()
clock.tick(60)
pygame.display.update()
pygame.quit()
The problem is that you are importing your pause screen from another program which creates a problem as you can import a library only once. You can try to put your pause screen code in the function instead of calling it but if you insist, you can put your pause_screen code in a function and do a from pausescreen import name_of_function so whenever you need to call that code just write name_of_funtion()
I decided to move that Squarey game to pygame, and now I have 2 rectangles that can move around and bump into the walls. However, the rectangles can move right through each other. How would I make them bump into each other and stop?
My code:
import pygame
pygame.init()
screen = pygame.display.set_mode((1000, 800))
pygame.display.set_caption("Squarey")
done = False
is_red = True
x = 30
y = 30
x2 = 100
y2 = 30
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
is_red = not is_red
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP]: y -= 3
if pressed[pygame.K_DOWN]: y += 3
if pressed[pygame.K_LEFT]: x -= 3
if pressed[pygame.K_RIGHT]: x += 3
if pressed[pygame.K_w]: y2 -= 3
if pressed[pygame.K_s]: y2 += 3
if pressed[pygame.K_a]: x2 -= 3
if pressed[pygame.K_d]: x2 += 3
if y < 0:
y += 3
if x > 943:
x -= 3
if y > 743:
y -= 3
if x < 0:
x += 3
if y2 < 0:
y2 += 3
if x2 > 943:
x2 -= 3
if y2 > 743:
y2 -= 3
if x2 < 0:
x2 += 3
screen.fill((0, 0, 0))
if is_red: color = (252, 117, 80)
else: color = (168, 3, 253)
if is_red: color2 = (0, 175, 0)
else: color2 = (255, 255, 0)
rect1 = pygame.draw.rect(screen, color, pygame.Rect(x, y, 60, 60))
rect2 = pygame.draw.rect(screen, color2, pygame.Rect(x2, y2, 60, 60))
pygame.display.flip()
clock.tick(60)
pygame.quit()
Use pygame.Rect.colliderect
if rect1.colliderect(rect2):
print("Collision !!")
BTW: you can create rect1 (and rect2) only once - before main loop - and then you can use rect1.x and rect1.y instead of x, y. And you can use pygame.draw.rect(screen, color, rect1) without creating new Rect all the time.
Rect is usefull
# create
rect1 = pygame.Rect(30, 30, 60, 60)
# move
rect1.x += 3
# check colision with bottom of the screen
if rect1.bottom > screen.get_rect().bottom:
# center on the screen
rect1.center = screen.get_rect().center
To check for collisions, try something like this:
def doRectsOverlap(rect1, rect2):
for a, b in [(rect1, rect2), (rect2, rect1)]:
# Check if a's corners are inside b
if ((isPointInsideRect(a.left, a.top, b)) or
(isPointInsideRect(a.left, a.bottom, b)) or
(isPointInsideRect(a.right, a.top, b)) or
(isPointInsideRect(a.right, a.bottom, b))):
return True
return False
def isPointInsideRect(x, y, rect):
if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom):
return True
else:
return False
Then, while moving them, you can call
if doRectsOverlap(rect1, rect2):
x -= 3
y -= 3
rect1 = pygame.draw.rect(screen, color, pygame.Rect(x, y, 60, 60))
Or something like that.