Why is the program not stopping after the main loop is stopped? - python

I am trying to make my own version of Snake. I'm trying to set it up so that when a wall is hit, the main while loop stops running by setting run = False. This works when I close the program with the quit button, but for some reason is not working when a collision is detected. Any help?
Note: I did try moving the gameOver() function out of the updateScreen() function and putting it separately at the end of the loop, but that didn't work either.
import pygame
import time
import os
pygame.init()
width, height = (500, 500)
win = pygame.display.set_mode((width, height))
pygame.display.set_caption("Snake")
win.fill((0, 0, 0))
# Snake Class
class Snake:
def __init__(self, x, y, width, height, color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.len = []
self.xvel = 0
self.yvel = 0
def draw(self, window):
win.fill((0, 0, 0))
pygame.draw.rect(window, self.color, (self.x, self.y, self.width, self.height))
def move(self):
self.x += self.xvel
self.y += self.yvel
def checkCol(self):
# Check if edge is hit
if self.x <= 0 or self.x + self.width >= width:
return True
elif self.y <= 0 or self.y + self.height >= height:
return True
else:
return False
sn = Snake(100, 100, 15, 15, (0, 255, 0))
# MAIN LOOP
def main():
run = True
FPS = 15
clock = pygame.time.Clock()
fontStyle = pygame.font.SysFont(None, 50)
def updateScreen():
sn.move()
sn.draw(win)
gameOver()
pygame.display.update()
def gameOver():
if sn.checkCol():
run = False
print("check")
def message(msg, color):
m = fontStyle.render(msg, True, color)
win.blit(m, [width / 2, height / 2])
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
sn.yvel = -10
sn.xvel = 0
if keys[pygame.K_s]:
sn.yvel = 10
sn.xvel = 0
if keys[pygame.K_a]:
sn.xvel = -10
sn.yvel = 0
if keys[pygame.K_d]:
sn.xvel = 10
sn.yvel = 0
updateScreen()
message("Game Over!", (255, 0, 0))
main()

use breakwhen your condition is met
if keys[pygame.K_d]:
sn.xvel = 10
sn.yvel = 0
break

Related

Pygame program stopping/crashing without any error logged

As a beginner im trying to create flappy bird to learn the basics of python and pygame. But when i run this code and click the button that is created the code just stops and python becomes not responsive. Does any one know why?
import pygame
import math
import random
import pyautogui as pg
pygame.init()
screenWidth, screenHeight = pg.size()
gameDisplay = pygame.display.set_mode((screenWidth, screenHeight), pygame.FULLSCREEN)
pygame.display.set_caption("Flappy by Tallik")
time = pygame.time.Clock()
crashed = False
bg = (0, 0, 0)
textSize = 32
font = pygame.font.Font("freesansbold.ttf", textSize)
buttonClicked = [0, 0]
mainGame = False
titleScreen = True
settings = False
changeBird = False
dead = False
phase = 1
def buttonFunc(x, y, width, height, color1, color2, text, textColor, textColor2, buttonNum, dissaperance):
global buttonClicked
mouseClick = pygame.mouse.get_pressed()
mouseX, mouseY = pygame.mouse.get_pos()
buttonText = font.render(str(text), True, textColor)
buttonText2 = font.render(str(text), True, textColor2)
textX = x + 10
textY = y + (height//3)
buttonClicked = [0, buttonNum]
if mouseX >= x and mouseX <= x + width and mouseY >= y and mouseY <= y + height:
pygame.draw.rect(gameDisplay, (color2), (x, y, width, height))
gameDisplay.blit(buttonText2, (textX, textY))
if mouseClick[0] == True:
buttonClicked[0] = 1
if dissaperance == False:
pygame.draw.rect(gameDisplay, (color2), (x, y, width, height))
gameDisplay.blit(buttonText2, (textX, textY))
buttonClicked[0] = 1
else:
gameDisplay.fill(bg)
else:
pygame.draw.rect(gameDisplay, (color1), (x, y, width, height))
gameDisplay.blit(buttonText, (textX, textY))
class bird(object):
def __init__(self, birdVelMultiplier, skin, width, height):
self.birdVelMultiplier = birdVelMultiplier
self.skin = skin
self.grav = -1
self.height = height
self.width = width
self.y = screenHeight//2
self.x = screenWidth//30
self.vel = screenHeight//100
self.jumps = 0
self.minVel = -20
self.maxVel = 20
def jump(self):
if keys[pygame.K_SPACE] and self.jumps == 0:
self.vel = -15
self.jumps += 1
if keys[pygame.K_UP] and self.jumps == 0:
self.vel = -15
self.jumps += 1
if not keys[pygame.K_UP] and not keys[pygame.K_SPACE]:
self.jumps = 0
def drawToGame(self, gameDisplay):
if self.y + self.height <= screenHeight:
if self.y >= 0:
if self.vel < self.maxVel and self.vel > self.minVel:
self.vel -= self.grav
self.y += self.vel
else:
if abs(self.vel)/self.vel == 1:
self.vel = self.maxVel
self.vel -= self.grav
self.y += self.vel
else:
self.vel = self.minVel
self.vel -= self.grav
self.y += self.vel
else:
self.y = 0
self.vel = 1
else:
self.y = screenHeight//2
self.vel = 0
pygame.draw.rect(gameDisplay, (0, 0, 255), (self.x, self.y, self.width, self.height))
class obstacle(object):
def __init__(self, speedMultiplier, width):
self.speedMultiplier = speedMultiplier
self.width = width
self.ranHeight = random.randrange(50, screenHeight - (screenHeight//16)*4)
self.ranHeight2 = (screenHeight - self.ranHeight) - 150
def spawn(self):
print(1)
def drawToSurface(self, gameDisplay):
print(3)
bird1 = bird(1, 1, 60, 60)
pipe = obstacle(1, 130)
sB = bird1
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
gameDisplay.fill(bg)
keys = pygame.key.get_pressed()
if buttonClicked == [1, 1]:
phase = 4
if phase == 1:
buttonFunc(screenWidth//2, screenHeight//4, screenWidth//18, screenHeight//20, (115, 115, 115), (85, 85, 85), "Start!", (0, 0, 0), (0, 0, 0), 1, True)
elif phase == 2:
print("?")
elif phase == 3:
print("??")
elif phase == 4:
while dead == False:
sB.jump()
sB.drawToGame(gameDisplay)
pygame.display.update()
time.tick(30)
pygame.quit()
quit()
To specify: after i have pressed the button created by buttFunc and the phase variable gets to 4 the program just stops and no eroor message is recieved. Also when closing the program the IDLE shell says the program is still running. I posted all the code because i dont know what made it break.
The program does not "crash", you just have an infinite loop. You do not need a loop that controls the game in the application loop. The application loop is continuously executed. Remove while dead == False::
while not crashed and not dead:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
gameDisplay.fill(bg)
keys = pygame.key.get_pressed()
if buttonClicked == [1, 1]:
phase = 4
if phase == 1:
buttonFunc(screenWidth//2, screenHeight//4, screenWidth//18, screenHeight//20, (115, 115, 115), (85, 85, 85), "Start!", (0, 0, 0), (0, 0, 0), 1, True)
elif phase == 2:
print("?")
elif phase == 3:
print("??")
elif phase == 4:
sB.jump()
sB.drawToGame(gameDisplay)
pygame.display.update()
time.tick(30)

How do I make the collisions work in this side scroller?

import pygame #importing the pygame library
pygame.init()
all_sprites = pygame.sprite.Group()
obstacle_group = pygame.sprite.Group()
player_group = pygame.sprite.Group()
defines the sprite groups we'll be calling on later
####Variables
width, height = 626, 375
fontObj = pygame.font.Font('Alsina Ultrajada.TTF', 16)
pygame.display.set_caption("side scroller")
screen = pygame.display.set_mode((width, height))
bg = pygame.image.load("achtergrondPixel.png")
gameOver = pygame.image.load("gameOver.png")
R_char = pygame.transform.scale(pygame.image.load("character.png"), (100, 100))
L_char = pygame.transform.flip((R_char), True, False)
char = R_char
jade = (55, 255, 20)
red = (255, 0, 0)
hitbox = (150, 200, 100, 100)
here we are making our player class, first we are initializing our player
class Player(pygame.sprite.Sprite): #making a class for our character so we can easily call the variables
def __init__(self, x, y, width, height, pos):
global player_group
super().__init__(player_group, all_sprites)
self.x = x
self.y = y
self.width = width
self.height = height
self.charFlip = False
self.isJump = False
self.jumpCount = 10
self.isFalling = False
self.fallCount = int(1)
self.pos = pos
self.rect = pygame.Rect((self.x - self.pos + 21),(self.y + 20), (self.width - 33), (self.height- 33))
self.add(player_group)
here we are making the function that draws our character, we also have a jump system and an falling system
def draw(self, screen):
screen.blit(char, (self.x, self.y))
if self.isFalling:
if self.y <= 200 and self.x < 488 and self.x > 573: #hier moet var worden aangemaakt voor als hij op platform staat
self.y -= (self.fallCount**2) * 0.1 * -1
self.fallCount += 1
else:
self.isFalling = False
self.fallCount = 1
if self.isJump:
if self.jumpCount >= 0:
neg = 1
if self.jumpCount == 0:
self.isFalling = True
self.y -= (self.jumpCount**2) * .25 * neg
self.jumpCount -= 1
else:
self.isJump = False
self.jumpCount = 10
self.hitbox = pygame.Rect((self.x - self.pos + 21),(self.y + 20), (self.width - 33), (self.height- 33))
if pygame.sprite.spritecollideany(self, obstacle_group):
print("collide")
Here we are making the green rectangle that is our obstacle in the game, first initializing it and that drawing it to the screen in the color jade
class Obstacle(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, pos):
global obstacle_group
super().__init__(obstacle_group, all_sprites)
self.pos = pos
self.x = x
self.y = y
self.width = width
self.height = height
self.rect = pygame.Rect((self.x - self.pos),self.y, self.width, self.height)
#self.add(obstacle_group)
def draw(self, screen, pos):
pygame.draw.rect(screen, jade, pygame.Rect((self.x - self.pos), self.y, self.width, self.height))
self.hitbox = pygame.Rect((self.x - self.pos),self.y, self.width, self.height)
pygame.draw.rect(screen, red, self.hitbox, 1)
pos is the variable we use to scroll the background
pos = 0
here we are making the player character and the only obstacle in the side scroller
obstacle2 = Obstacle(300, 200, 100, 20, pos)
nkdMonkey = Player(150, 200, 100, 100, pos)
FPS = 60
run = True
clock = pygame.time.Clock()
this is our main drawing function that we call on in our main while loop
def draw_window():
screen.blit(bg, ((-1 * pos), 0))
textSurfaceObj = fontObj.render((str(pos)), False, (240,240,240, 255))
screen.blit(textSurfaceObj, (40,40))
obstacle2.draw(screen, pos)
nkdMonkey.draw(screen)
#pygame.draw.rect(screen, red, nkdMonkey.hitbox, 1)
#for if you want to see the hitbox of the player which is used for collisions
pygame.display.update()
this is our while loop
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#all_sprites = pygame.sprite.Group()
#obstacle_group = pygame.sprite.Group()
#player_group = pygame.sprite.Group()
keys_pressed = pygame.key.get_pressed()
#Checks if the front and and back button is pressed and if so, changes pos.
if keys_pressed[pygame.K_d] and pos < 1874:
pos += 2
char = R_char
if keys_pressed[pygame.K_d] and keys_pressed[
pygame.K_LSHIFT] and pos < 2000:
pos += 5
char = R_char
if keys_pressed[pygame.K_a] and pos > 0:
pos -= 2
char = L_char
if keys_pressed[pygame.K_a] and keys_pressed[pygame.K_LSHIFT] and pos > 0:
pos -= 5
char = L_char
if keys_pressed[pygame.K_w] and nkdMonkey.isJump == False:
nkdMonkey.isJump = True
if nkdMonkey.y > 200 and nkdMonkey.x > 488 and nkdMonkey.x < 573:
nkdMonkey.y -= 1
#if nkdMonkey.x > 488 and nkdMonkey.x < 573:
#nkdMonkey.isFalling = True
if pos > 1980:
run = False
this is the point we can't figure out, we want it to print collide when the two sprites are colliding
if pygame.sprite.spritecollideany(nkdMonkey, obstacle_group):
print("collide")
all_sprites.update()
draw_window()
Here we made a simple end screen
screen.fill(jade)
screen.blit(pygame.image.load("character.png"), (0, 70))
text = fontObj.render('You win!!', True, (0,0,0, 255))
textRect = text.get_rect()
score = fontObj.render(str(pos), True, red)
screen.blit(score, (40,40))
textRect.center = (width // 2, (height // 2 + 20))
screen.blit(text, textRect)
pygame.display.flip()
pygame.sprite.spritecollideany attributes of the pygame.sprite.Sprite objects to detect a collision. Therefore you have to update the location of the rectangle by the position of the player:
nkdMonkey.rect.topleft = (nkdMonkey.x - nkdMonkey.pos), nkdMonkey.y)
if pygame.sprite.spritecollideany(nkdMonkey, obstacle_group):
print("collide")

How to detect a key press once and use '!=' symbol?

I am trying to figure out how to increase the intensity of my shapes moving when holding the 'up' arrow key. When I hold space bar, I wanted my shapes to rumble based on the intensity, which I've done. What I also would like to do is be able to increase the intensity by pressing the 'up' arrow key once (NOTE: Once. Currently when I press the arrow key once, it loops way too fast and multiplies the intensity by like 5000x. I would like it just to add one number each time.) This is the code that does not work how I'd like.
import pygame
import random
import time
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# Edit the intensity of the shake (Must be one number apart)
# Ex: a = -100, b = 101. A is negative, B is positive
a = -4
b = 5
up = 10
intensity = (a, b)
pygame.init()
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
done = False
clock = pygame.time.Clock()
class Rectangle():
def __init__(self):
self.x = random.randrange(0, 700)
self.y = random.randrange(0, 500)
self.height = random.randrange(20, 70)
self.width = random.randrange(20, 70)
self.x_change = random.randrange(-3, 3)
self.y_change = random.randrange(-3, 3)
self.color = random.sample(range(250), 4)
def draw(self):
pygame.draw.rect(screen, self.color, [self.x, self.y, self.width, self.height])
def move(self):
self.x += self.x_change
self.y += self.y_change
class Ellipse(Rectangle):
pass
def draw(self):
pygame.draw.ellipse(screen, self.color, [self.x, self.y, self.width, self.height])
def move(self):
self.x += self.x_change
self.y += self.y_change
my_list = []
for number in range(600):
my_object = Rectangle()
my_list.append(my_object)
for number in range(600):
my_object = Ellipse()
my_list.append(my_object)
# -------- Main Program Loop -----------
while not done:
keys = pygame.key.get_pressed()
# --- Main event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(BLACK)
for rect in my_list:
rect.draw()
rect.move()
for rectElli in my_list:
rectElli.draw()
if keys[pygame.K_SPACE]:
rectElli.y_change = random.randrange(a, b)
rectElli.x_change = random.randrange(a, b)
rectElli.move()
print(a)
print(b)
if keys[pygame.K_UP] and up / 10 == 1:
up += 1
a -= 1
b -= -1
print(a)
print(b)
print(up)
if up / 10 != 1:
up += 1
pygame.display.flip()
clock.tick(60)
pygame.quit()
This is the code that worked before I wanted to add the increasing of intensity.
import pygame
import random
import time
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# Edit the intensity of the shake (Must be one number apart)
# Ex: a = -100, b = 101. A is negative, B is positive
a = -4
b = 5
up = 10
intensity = (a, b)
pygame.init()
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
done = False
clock = pygame.time.Clock()
class Rectangle():
def __init__(self):
self.x = random.randrange(0, 700)
self.y = random.randrange(0, 500)
self.height = random.randrange(20, 70)
self.width = random.randrange(20, 70)
self.x_change = random.randrange(-3, 3)
self.y_change = random.randrange(-3, 3)
self.color = random.sample(range(250), 4)
def draw(self):
pygame.draw.rect(screen, self.color, [self.x, self.y, self.width, self.height])
def move(self):
self.x += self.x_change
self.y += self.y_change
class Ellipse(Rectangle):
pass
def draw(self):
pygame.draw.ellipse(screen, self.color, [self.x, self.y, self.width, self.height])
def move(self):
self.x += self.x_change
self.y += self.y_change
my_list = []
for number in range(600):
my_object = Rectangle()
my_list.append(my_object)
for number in range(600):
my_object = Ellipse()
my_list.append(my_object)
# -------- Main Program Loop -----------
while not done:
keys = pygame.key.get_pressed()
# --- Main event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(BLACK)
for rect in my_list:
rect.draw()
rect.move()
for rectElli in my_list:
rectElli.draw()
if keys[pygame.K_SPACE]:
rectElli.y_change = random.randrange(a, b)
rectElli.x_change = random.randrange(a, b)
rectElli.move()
pygame.display.flip()
clock.tick(60)
pygame.quit()
The first issue is, that you do the increasing of the intensity in the for-loop which traverse the ellipses. So the intensity is not increased once, it is increased once per ellipse.
If you don't want to increase the intensity in every 10th frame, then you've to verify if the increment the frame counter up in every frame, but you've to verify if it is divisible by 10. Verify if the remainder of an integral division by 10 is 0. Use the modulo operator (%) to calculate the remainder:
if up % 10 == 0:
See the example, when UP is pressed, the the intensity increases and when it is released then the intensity returns to normal:
while not done:
keys = pygame.key.get_pressed()
# --- Main event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(BLACK)
for rect in my_list:
rect.draw()
rect.move()
for rectElli in my_list:
rectElli.draw()
if keys[pygame.K_SPACE]:
rectElli.y_change = random.randrange(a, b)
rectElli.x_change = random.randrange(a, b)
rectElli.move()
if keys[pygame.K_UP]:
up += 1
if up % 10 == 0:
a -= 1
b -= -1
else:
a, b = -4, 5
pygame.display.flip()
clock.tick(60)

How do you make the game end when the main player collides with an image (obstacle)?

import math
import random
import pygame
from pygame.locals import *
import sys
def events():
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
pygame.init()
W = 700
H = 400
updater = pygame.time.Clock()
display = pygame.display.set_mode((700, 400))
pygame.display.set_caption("Skating_Game")
x = y = 0
surface = pygame.image.load("man2.png")
pygame.display.set_icon(surface)
class player:
def __init__(self, velocity, maxJumpRange):
self.velocity = velocity
self.maxJumpRange = maxJumpRange
def setLocation(self, x, y):
self.x = x
self.y = y
self.xVelocity = 0
self.jumping = False
self.jumpCounter = 0
self.falling = True
def keys(self):
k = pygame.key.get_pressed()
if k[K_LEFT]:
self.xVelocity = -self.velocity
elif k[K_RIGHT]:
self.xVelocity = self.velocity
else:
self.xVelocity = 0
if k[K_SPACE] and not self.jumping and not self.falling:
self.jumping = True
self.jumpCounter = 0
def move(self):
self.x += self.xVelocity
if self.jumping:
self.y -= self.velocity
self.jumpCounter += 1
if self.jumpCounter == self.maxJumpRange:
self.jumping = False
self.falling = True
elif self.falling:
if self.y <= H - 60 and self.y + self.velocity >= H - 60:
self.y = H - 60
self.falling = False
else:
self.y += self.velocity
def draw(self):
display = pygame.display.get_surface()
character = pygame.image.load("man3.png").convert_alpha()
display.blit(character, (self.x, self.y - 100))
#pygame.draw.circle(display, (255, 255, 255), (self.x, self.y - 25), 25, 0)
def do(self):
self.keys()
self.move()
self.draw()
P = player(3, 50)
P.setLocation(350, 0)
BLACK = ( 0, 0, 0)
g=0
font = pygame.font.SysFont("Plump", 30)
obstacle = pygame.image.load("obstacle.png").convert_alpha()
background = pygame.image.load("Road.png").convert()
x = 0
while True:
events()
rel_x = x % background.get_rect().width
display.blit(background, (rel_x - background.get_rect().width,0))
if rel_x < 700:
display.blit(background, (rel_x, 0))
x -= 1
g += 0.01
pygame.draw.rect(display, (255,255,255,128), [rel_x, 275, 150, 50])
display.blit(obstacle, (rel_x, 250))
text = font.render("Score: "+str(int(g)), True, (255, 255, 255))
display.blit(text, (0,0))
P.do()
if P.rect.collidepoint(self.x,self.y):
pygame.quit()
pygame.display.update()
updater.tick(200)
So if the player collides with the obstacle image the game should stop. How do i do this? I have made a class for the player and the obstacle is just an image which is constantly moving.
I was thinking maybe I could track the x and y coordinate of the player and obstacle and when their radius overlaps the game could stop.
Here's a working (simplified) version of your program with some comments. You have to create rects for the obstacle and the player and then check if the rects collide with the help of the colliderect method.
import sys
import pygame
from pygame.locals import *
pygame.init()
W = 700
H = 400
updater = pygame.time.Clock()
display = pygame.display.set_mode((700, 400))
PLAYER_IMAGE = pygame.Surface((30, 50))
PLAYER_IMAGE.fill(pygame.Color('dodgerblue1'))
class Player:
def __init__(self, x, y, velocity, maxJumpRange):
self.velocity = velocity
self.maxJumpRange = maxJumpRange
self.image = PLAYER_IMAGE # Give the player an image.
# Create a rect with the size of the PLAYER_IMAGE and
# pass the x, y coords as the topleft argument.
self.rect = self.image.get_rect(topleft=(x, y))
self.x = x
self.y = y
self.xVelocity = 0
self.jumping = False
self.jumpCounter = 0
self.falling = True
def keys(self):
k = pygame.key.get_pressed()
if k[K_LEFT]:
self.xVelocity = -self.velocity
elif k[K_RIGHT]:
self.xVelocity = self.velocity
else:
self.xVelocity = 0
if k[K_SPACE] and not self.jumping and not self.falling:
self.jumping = True
self.jumpCounter = 0
def move(self):
self.x += self.xVelocity
if self.jumping:
self.y -= self.velocity
self.jumpCounter += 1
if self.jumpCounter == self.maxJumpRange:
self.jumping = False
self.falling = True
elif self.falling:
if self.y >= H - 160: # Simplified a little.
self.y = H - 160
self.falling = False
else:
self.y += self.velocity
# Update the position of the rect, because it's
# used for the collision detection.
self.rect.topleft = self.x, self.y
def draw(self, display):
# Just draw the image here.
display.blit(self.image, (self.x, self.y))
def do(self):
self.keys()
self.move()
player = Player(350, 0, 3, 50)
obstacle = pygame.Surface((150, 50))
obstacle.fill(pygame.Color('sienna1'))
# Create a rect with the size of the obstacle image.
obstacle_rect = obstacle.get_rect()
g = 0
x = 0
FPS = 60 # Cap the frame rate at 60 or 30 fps. 300 is crazy.
while True:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
# --- Update the game ---
player.do()
rel_x = x % display.get_width()
x -= 7
g += 0.01
obstacle_rect.topleft = rel_x, 250 # Update the position of the rect.
# --- Draw everything ---
display.fill((30, 30, 30))
display.blit(obstacle, (rel_x, 250))
if g > 30:
display.blit(obstacle, (rel_x+350, 250))
# Check if the obstacle rect and the player's rect collide.
if obstacle_rect.colliderect(player.rect):
print("Game over!") # And call pygame.quit and sys.exit if you want.
# Draw the image/surface of the player onto the screen.
player.draw(display)
# Draw the actual rects of the objects (for debugging).
pygame.draw.rect(display, (200, 200, 0), player.rect, 2)
pygame.draw.rect(display, (200, 200, 0), obstacle_rect, 2)
pygame.display.update()
updater.tick(FPS)
Pygame rectangles include a collidepoint and colliderect method that allows you to check to see if something intersects with a rectangle. So you could have rectangles drawn beneath the obstacle and check to see if the player's coordinates intersect with the rectangle. Like this:
if self.rect.collidepoint(self.x,self.y):
pygame.quit()

Python variables change in every object

I'm playing around with pygame for the first time, trying to make multiple rectangles move across the screen. Here's my code:
import pygame
pygame.init()
scrWidth = 1200
scrHeigth = 900
done = False
rectangles = []
screen = pygame.display.set_mode((scrWidth, scrHeigth))
clock = pygame.time.Clock()
class Rectangle:
speed = [1,1]
colourSpeed = 300
colourID = 0
colour = (0, 0, 255)
size = 60
def __init__(self, name = "", x=0, y=0, speed=False, colour=False, size=False):
self.name = name
self.x = x
self.y = y
self.doesSpeed = speed
self.doesColour = colour
self.doesSize = size
def draw(self):
pygame.draw.rect(screen, self.colour, pygame.Rect(self.x, self.y, self.size, self.size))
def checkCollision(self):
if self.x < 0 or self.x > scrWidth-self.size:
self.speed[0] = -self.speed[0]
if self.y < 0 or self.y > scrHeigth-self.size:
self.speed[1] = -self.speed[1]
def move(self):
self.x += self.speed[0]
self.y += self.speed[1]
def changeColour(self):
self.colourID = (self.colourID+1)%self.colourSpeed
if 0 <= self.colourID < (self.colourSpeed/3):
self.colour = (0, 0, 255)
elif (self.colourSpeed/3) <= self.colourID < (2*self.colourSpeed/3):
self.colour = (0, 255, 0)
elif (2*self.colourSpeed/3) <= self.colourID < self.colourSpeed:
self.colour = (255, 0, 0)
rect1 = Rectangle("rect1", 30, 30, False, True, False)
rectangles.append(rect1)
rect2 = Rectangle("rect2", 300, 300)
rectangles.append(rect2)
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
screen.fill((0, 0, 0))
for obj in rectangles:
obj.checkCollision()
if obj.doesColour: obj.changeColour()
obj.move()
obj.draw()
pygame.display.flip()
clock.tick(60)
The problem is that when a rectangle collides with the edge of the screen all rectangles change direction(speed). However if I make rectangles with different speeds this doesn't happen.
ex: rect1 has speed [1,1] and rect2 has speed [2,2].
What's causing this and how can I fix it?
Move the speed, colourSpeed, colourID, colour, size to the init method.
At that moment that fields are defined as class fields, so they are changed globally for all rectangles.
Solution:
def __init__(self, name = "", x=0, y=0, speed=False, colour=False, size=False):
self.name = name
self.x = x
self.y = y
self.doesSpeed = speed
self.doesColour = colour
self.doesSize = size
# here it comes
self.speed = [1,1]
self.colourSpeed = 300
self.colourID = 0
self.colour = (0, 0, 255)
self.size = 60

Categories

Resources