The pygame code is mentioned and needs some modifications as specified: [duplicate] - python

This question already has answers here:
How can i shoot a bullet with space bar?
(1 answer)
How do I stop more than 1 bullet firing at once?
(1 answer)
Closed 2 years ago.
Here is a source code of my space-arcade pygame. It's a quite simple one and does shoots bullets as well but for now these bullets do not affect the obstacles coming in the screen.
Question: How can I shoot multiple bullets just after firing the first bullet and otherwise not waiting for the first bullet to pass y <= 0 to fire another bullet?
Please help me with this issue and if possible re-write this code with some adjustments made
import random
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Space Invader")
icon = pygame.image.load("space-invaders.png")
pygame.display.set_icon(icon)
background = pygame.image.load("background.png")
player_image = pygame.image.load("space-invaders.png")
playerX = 370
playerY = 480
playerX_change = 0
playerY_change = 0
def player(x, y):
screen.blit(player_image, (x, y))
obs_image = pygame.image.load("cave-painting (1).png")
obsX = random.randint(0, 800)
obsY = random.randint(0, 100)
obsX_change = 2
obsY_change = 0.3
def obstacle(x, y):
screen.blit(obs_image, (x, y))
bullet_image = pygame.image.load("bullet.png")
bulletX = 0
bulletY = 0
bulletX_change = 0
bulletY_change = 10
bullet_state = "ready"
def fire_bullet(x, y):
global bullet_state # Globaling it, inorder to access it inside this func.
bullet_state = "fire"
screen.blit(bullet_image, (x,y))
running = True
while running:
screen.fill( (0, 0,0) )
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -4
if event.key == pygame.K_RIGHT:
playerX_change = 4
if event.key == pygame.K_UP:
playerY_change = -4
if event.key == pygame.K_DOWN:
playerY_change = 4
if event.key == pygame.K_SPACE:
if bullet_state is "ready":
bulletX = playerX
bulletY = playerY
fire_bullet(bulletX, bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
playerY_change = 0
playerX += playerX_change
playerY += playerY_change
# Stopping our player beyond screen:
if playerX <= 0:
playerX = 0
elif playerX >= 765:
playerX = 765
elif playerY <= 200:
playerY = 200
elif playerY >= 480:
playerY = 480
obsX += obsX_change
obsY += obsY_change
# Movement mechanics for obstacles:
if obsX <= 0:
obsX_change = 3
obsY += obsY_change
elif obsX >= 736:
obsX_change = -3
obsY += obsY_change
if bulletY <= 0:
bulletY = 480
bullet_state = "ready"
if bullet_state is "fire":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
player(playerX, playerY)
obstacle(obsX, obsY)
pygame.display.update()
Also Here are the source pictures used in the aforementioned code:
background
cave-painting (1)
space-invaders
bullet

You have to add a list of bullets:
bullet_list = []
When a new bullet spawns then add a new position to the head of the bullet list:
if event.key == pygame.K_SPACE:
bullet_list.insert(0, [playerX, playerY])
Move and draw the bullets in a loop. Delete the bullets which are out of the window from the list:
while running:
# [...]
for i in range(len(bullet_list)):
bullet_list[i][1] -= bulletY_change
if bullet_list[i][1] < 0:
del bullet_list[i:]
break
# [...]
for bullet in bullet_list:
screen.blit(bullet_image, bullet)
Complete example:
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
pygame.display.set_caption("Space Invader")
icon = pygame.image.load("space-invaders.png")
pygame.display.set_icon(icon)
background = pygame.image.load("background.png")
player_image = pygame.image.load("space-invaders.png")
playerX = 370
playerY = 480
playerX_change = 0
playerY_change = 0
def player(x, y):
screen.blit(player_image, (x, y))
obs_image = pygame.image.load("cave-painting (1).png")
obsX = random.randint(0, 800)
obsY = random.randint(0, 100)
obsX_change = 2
obsY_change = 0.3
def obstacle(x, y):
screen.blit(obs_image, (x, y))
bullet_image = pygame.image.load("bullet.png")
bulletX_change = 0
bulletY_change = 10
bullet_list = []
def draw_bullets(bullets):
for bullet in bullets:
screen.blit(bullet_image, bullet)
running = True
while running:
clock.tick(60)
screen.fill( (0, 0,0) )
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -4
if event.key == pygame.K_RIGHT:
playerX_change = 4
if event.key == pygame.K_UP:
playerY_change = -4
if event.key == pygame.K_DOWN:
playerY_change = 4
if event.key == pygame.K_SPACE:
bullet_list.insert(0, [playerX, playerY])
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
playerY_change = 0
playerX += playerX_change
playerY += playerY_change
# Stopping our player beyond screen:
if playerX <= 0:
playerX = 0
elif playerX >= 765:
playerX = 765
elif playerY <= 200:
playerY = 200
elif playerY >= 480:
playerY = 480
obsX += obsX_change
obsY += obsY_change
# Movement mechanics for obstacles:
if obsX <= 0:
obsX_change = 3
obsY += obsY_change
elif obsX >= 736:
obsX_change = -3
obsY += obsY_change
for i in range(len(bullet_list)):
bullet_list[i][1] -= bulletY_change
if bullet_list[i][1] < 0:
del bullet_list[i:]
break
player(playerX, playerY)
obstacle(obsX, obsY)
draw_bullets(bullet_list)
pygame.display.update()

Related

How to add game restart in this pgame code? [duplicate]

This question already has answers here:
how restarting game on pygame works
(1 answer)
Pygame restart function
(2 answers)
In game loop, how do I restart game properly using nested class or loop?
(1 answer)
Reset and restart pygame program doesn't work
(1 answer)
Closed 3 months ago.
I followed a tutorial guide on how to make this game but they never said how to put in a restart. I'm not quite sure how to do it, would anyone here know off the top of their head? Here's the code.
import pygame
import math
import random
from pygame import mixer
# Start the Program
pygame.init()
# Create the Screen
screen = pygame.display.set_mode((800, 600))
running = True
# Background
background = pygame.image.load('background.png')
# Background Sound
mixer.music.load('theme.mp3')
mixer.music.play(-1)
# Title and Icon
pygame.display.set_caption("Boot Craft")
icon = pygame.image.load('Starcraft.png')
pygame.display.set_icon(icon)
# Player
player_image = pygame.image.load('BC.png')
playerX = 370
playerY = 480
playerX_change = 0
playerY_change = 0
# Enemy
enemy_image = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 6
for i in range(num_of_enemies):
enemy_image.append(pygame.image.load('Zergling.png'))
enemyX.append(random.randint(0, 768))
enemyY.append(random.randint(50, 150))
enemyX_change.append(0.2)
enemyY_change.append(40)
# Bullet
# Ready - You can't see the bullet on the screen
# Fire - The bullet is currently moving
bullet_image = pygame.image.load('Bullet.png')
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = .5
bullet_state = "ready"
# Score
score_value = 0
font = pygame.font.Font('freesansbold.ttf', 32)
textX = 10
textY = 10
# Game Over
over_font = pygame.font.Font('freesansbold.ttf', 64)
def show_score(x,y):
score = font.render('Score: ' + str(score_value), True, (255,255,255))
screen.blit(score, (x, y))
def game_over_text():
over_text = over_font.render('GAME OVER', True, (255, 255, 255))
continue_text = over_font.render('CONTINUE? Y/N', True, (255, 255, 255))
screen.blit(over_text, (200, 250))
screen.blit(continue_text, (160, 350))
def player(x, y):
screen.blit(player_image, (x, y))
def enemy(x, y, i):
screen.blit(enemy_image[i], (x, y))
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bullet_image, (x + 8, y + 10))
def isCollision(enemyX, enemyY, bulletX, bulletY):
distance = math.sqrt((math.pow(enemyX - bulletX, 2)) + (math.pow(enemyY - bulletY, 2)))
if distance < 27:
return True
else:
return False
# Game Loop - Runs Game
while running:
# RGB Color Values 0-255
screen.fill((0, 0, 0))
# Background Image
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_y:
running = True
# Check Key pressed.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
playerX_change = -0.2
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
playerX_change = 0.2
if event.key == pygame.K_SPACE:
if bullet_state == "ready":
bullet_sound = mixer.Sound('laser.wav')
bullet_sound.play()
# Get the current X coordinate of the spaceship
bulletX = playerX
fire_bullet(bulletX, bulletY)
# Check Key released.
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_a or event.key == pygame.K_d:
playerX_change = 0
playerX += playerX_change
# Adds Boundary
if playerX <= 0:
playerX = 0
elif playerX >= 721:
playerX = 721
# Enemy Movement
for i in range(num_of_enemies):
# Game Over
if enemyY[i] > 440:
for j in range(num_of_enemies):
enemyY[j] = 2000
game_over_text()
enemyX[i] += enemyX_change[i]
if enemyX[i] <= 0:
enemyX_change[i] = 0.2
enemyY[i] += enemyY_change[i]
elif enemyX[i] >= 768:
enemyX_change[i] = -0.2
enemyY[i] += enemyY_change[i]
# Collision
collision = isCollision(enemyX[i],enemyY[i],bulletX,bulletY)
if collision:
explosion_sound = mixer.Sound('explode.wav')
explosion_sound.play()
bulletY = 480
bullet_state = "ready"
score_value += 1
enemyX[i] = random.randint(0, 768)
enemyY[i] = random.randint(50, 150)
enemy(enemyX[i], enemyY[i], i)
# Bullet Movement
if bulletY <= 0:
bulletY = 480
bullet_state = "ready"
if bullet_state == "fire":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
player(playerX, playerY)
show_score(textX, textY)
pygame.display.update()
I'm not sure what to try, as what I googled used different ways of running the game. New to this, so genuinely confused.

Character isnt able to move when pressing another button or using mouse, bullet also wont fire

So I've gotten movement to work for enemy and the player, but when I press a button outside of left or right, like space which will be to shoot, the player cant move and the bullet itself wont actually come out or appear at all. Its in pygame and ima be honest I have no clue whats wrong so im sorry it looks so long and weird
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((800, 600))
background = pygame.image.load("BackgroundImg.jpg")
pygame.display.set_caption("Real Hero")
icon = pygame.image.load('Icon.png')
pygame.display.set_icon(icon)
playerImg = pygame.image.load('Player.png')
playerX = 370
playerY = 480
playerX_change = 0
enemyImg = pygame.image.load('Enemy.png')
enemyX = random.randint(0, 800)
enemyY = random.randint(50, 150)
enemyX_change = 0.1
enemyY_change = 40
bulletImg = pygame.image.load('Bullet.png')
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 10
bullet_state = "ready"
def player(x, y):
screen.blit(playerImg, (x, y))
def enemy(x, y):
screen.blit(enemyImg, (x, y))
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bulletImg, (x + 16, y + 10))
running = True
while running:
screen.fill((0, 0, 0))
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.3
if event.key == pygame.K_RIGHT:
playerX_change = 0.3
if event.key == pygame.K_SPACE:
fire_bullet(playerX, bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
enemyX += enemyX_change
if enemyX <= 0:
enemyX_change = 0.1
enemyY += enemyY_change
elif enemyX >= 736:
enemyX_change = -0.1
enemyY += enemyY_change
if bullet_state is "fire":
fire_bullet(playerX, bulletY)
bulletY -= bulletY_change
enemy(enemyX, enemyY)
player(playerX, playerY)
pygame.display.update()
It is a matter of Indentation:
running = True
while running:
screen.fill((0, 0, 0))
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# INDENTATION
#-->|
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.3
if event.key == pygame.K_RIGHT:
playerX_change = 0.3
if event.key == pygame.K_SPACE:
fire_bullet(playerX, bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
# INDENTATION
#<--|
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
enemyX += enemyX_change
if enemyX <= 0:
enemyX_change = 0.1
enemyY += enemyY_change
elif enemyX >= 736:
enemyX_change = -0.1
enemyY += enemyY_change
if bullet_state is "fire":
fire_bullet(playerX, bulletY)
bulletY -= bulletY_change
enemy(enemyX, enemyY)
player(playerX, playerY)
pygame.display.update()

bullet not appearing in pygame

i have tried making bullet in pygame using this tutorial https://youtu.be/8wEYDLVrAB8 but i have failed to do so here is commented code ,
. if problem arise please leave a comment on it . thank you for helping .i have already asked it once in this website but it didnt work [![ this is current state when firing bullet][1]][1] . i am running it in pycharm
import pygame
import random
#module importing
pygame.init()
pygame.display.init()
#screen creation
screen = pygame.display.set_mode((800, 600))
bacground = pygame.image.load("background.png")
#player information
playerimg = pygame.image.load("spaceship.png")
playerX_change = 0
playerX = 360
playerY = 480
#enemy information
enemyimg = pygame.image.load("enemy.png")
enemyX = random.randint(0,736)
enemyY = random.randint(50,150)
enemyX_change = 4
enemyY_change = 40
#bullet information
bulletimg = pygame.image.load("enemy.png")
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 40
bullet_state = "ready"
#all three functions
def player(x, y):
screen.blit(playerimg, (x, y))
#enemy draw function
def enemy(x, y):
screen.blit(enemyimg, (x, y))
#bullet fire function
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bulletimg, (x + 16, y+10))
# drawing player on screen
# game loop
running = True
while running == True:
#stuff happening in game loop
screen.fill((200, 200, 0))
screen.blit(bacground,(0,0))
player(playerX, playerY)
enemy(enemyX, enemyY)
if bullet_state == "fire":
fire_bullet(playerX, bulletY)
bulletY -= bulletY_change
#detection of the keys pressed and consequences
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
player(playerX, playerY)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -5
if event.key == pygame.K_RIGHT:
playerX_change = 5
if event.key == pygame.K_SPACE:
fire_bullet(bulletX,bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
#player and enemy movement system
playerX += playerX_change
enemyX += enemyX_change
#enemy and player collision detection
if enemyX <= 0:
enemyX_change = 4
enemyY += enemyY_change
if enemyX>= 736:
enemyX_change = -4
enemyY += enemyY_change
if playerX <= 0:
playerX = 0
if playerX >= 736:
playerX = 736
#display update
pygame.display.update()
It is a matter of Indentation. You have to handle the events in the event loop instead of the application loop:
while running == True:
#stuff happening in game loop
screen.fill((200, 200, 0))
screen.blit(bacground,(0,0))
player(playerX, playerY)
enemy(enemyX, enemyY)
if bullet_state == "fire":
fire_bullet(playerX, bulletY)
bulletY -= bulletY_change
#detection of the keys pressed and consequences
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
# INDENTATAION
#-->|
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -5
if event.key == pygame.K_RIGHT:
playerX_change = 5
if event.key == pygame.K_SPACE:
fire_bullet(bulletX,bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0

How do i detect if my character collided with an enemy and closes the game if it hits 3 times [duplicate]

This question already has answers here:
How to detect collisions between two rectangular objects or images in pygame
(1 answer)
Why is my collision test always returning 'true' and why is the position of the rectangle of the image always wrong (0, 0)?
(1 answer)
How do I detect collision in pygame?
(5 answers)
Closed 2 years ago.
I want to make it kind of like a life system so that if it collides with the enemy 3 times it will quit
pygame.init()
screen_width = 800
screen_height = 600
window = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Test')
time = pygame.time.Clock()
bg_color1 = (135, 142, 142) # MAIN BG COLOR
bg_color2 = (255, 0, 0) # red
bg_color3 = (255, 255, 0) # yellow
UFO = pygame.image.load('ufo.png')
bg_pic = pygame.image.load('Letsgo.jpg')
clock = pygame.time.Clock()
playerImg = pygame.image.load('enemy.png')
playerX = random.randrange(0, screen_width)
playerY = -50
playerX_change = 0
player_speed = 5
def player(x, y):
window.blit(playerImg, (playerX, playerY))
crashed = False
rect = UFO.get_rect()
obstacle = pygame.Rect(400, 200, 80, 80)
menu = True
playerY = playerY + player_speed
if playerY > screen_height:
playerX = random.randrange(0, screen_width)
playerY = -25
def ufo(x, y):
window.blit(UFO, (x, y))
while menu:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
menu = False
window.fill((0, 0, 0))
time.tick(30)
window.blit(bg_pic, (0, 0))
pygame.display.update()
x = (screen_width * 0.45)
y = (screen_height * 0.8)
x_change = 0
car_speed = 0
y_change = 0
while not crashed:
x += x_change
if x < 0:
x = 0
elif x > screen_width - UFO.get_width():
x = screen_width - UFO.get_width()
y += y_change
if y < 0:
y = 0
elif y > screen_height - UFO.get_height():
y = screen_height - UFO.get_height()
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
############SIDE TO SIDE################
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
###########UP AND DOWN#################
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_change = -5
elif event.key == pygame.K_DOWN:
y_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
if playerY > screen_height:
playerX = random.randrange(0, screen_width)
playerY = 0
playerY += 10
##
window.fill(bg_color1)
ufo(x, y)
player(playerX, playerY)
pygame.display.update()
clock.tick(100)
pygame.quit()
quit()
im thinking of using a collide.rect but cant figure it out any help is appreciated
I want the "playerImg" to be the enemy ...................................................................................
Create a variable that stores the number of lives. Create pygme.Rect objects of the player and the UFO with the method get_rect. Set the location of the rectangles by keyword arguments. Use colliderect to test the collision and decrease the number of lives when a collision is detected and create a new random starting position:
lives = 3
while not crashed:
# [...]
player_rect = playerImg.get_rect(topleft = (playerX, playerY))
ufo_rect = UFO.get_rect(topleft = (x, y))
if player_rect.colliderect(ufo_rect):
lives -= 1
print(lives)
playerX = random.randrange(0, screen_width)
playerY = 0
if lives == 0:
crashed = True
# [...]

Why can't I use float numbers instead of integers?

import pygame
import random
pygame.init()
# create screen
screen = pygame.display.set_mode((1000, 600))
# Title + Logo
pygame.display.set_caption("Space Invader")
icon = pygame.image.load("chicken.png")
pygame.display.set_icon(icon)
# Player icon
player_icon = pygame.image.load("spaceship.png")
playerX = 400
playerY = 500
player_changeX = 0
player_changeY = 0
# enemy Player
enemy_icon = pygame.image.load("space-invaders.png")
enemyX = random.randint(0, 936)
enemyY = random.randint(-100, -50)
enemy_changeX = random.randint (0.1, 0.5)
enemy_changeY = random.randint (0.1, 0.5)
if enemyY >= 500:
print("Game over")
exit()
def player(x, y):
screen.blit(player_icon, (x, y))
def enemy(x, y):
screen.blit(enemy_icon, (x, y))
# game loop
running = True
while running:
# backround colour RGB
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# If key pressed check whether its right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_changeX = -1
if event.key == pygame.K_RIGHT:
player_changeX = 1
if event.key == pygame.K_UP:
player_changeY = -1
if event.key == pygame.K_DOWN:
player_changeY = 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
player_changeX = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
player_changeY = 0
# If player reaches boarder
if playerX >= 936:
playerX = 20
if playerX <= 0:
playerX = 936
if playerY <= 0:
playerY = 0
if playerY >= 550:
playerY = 550
# enemy control
if enemyX >= 936:
enemyX = 20
if enemyX <= 0:
enemyX = 936
if enemyY <= 0:
enemyY = 0
if enemyY >= 550:
enemyY = random.randint(-100, -50)
# Player change in coordinates
playerX += player_changeX
playerY += player_changeY
#enemy change in coordinates
enemyX += enemy_changeX
enemyY += enemy_changeY
#Results
player(playerX, playerY)
enemy(enemyX, enemyY)
pygame.display.update()
I just got into programing, I am creating a little game. It's not finished yet but I am running into a problem I want to use float numbers instead of integers but it doesn't let me. Can you change this? and if yes how. The flaot number i want to use is in enemy_changeX and Y.
The error it's giving me is ,
line 212, in randrange
raise ValueError("non-integer arg 1 for randrange()")
ValueError: non-integer arg 1 for randrange()
even though I don't have a line 212
I hope the question was accurate enough.
First of all, you are requesting an integer between 0.1 and 0.5, which is not possible for obvious reasons.
To get a float you can use random.uniform(0.1, 0.5).
Although I have to note here, that pixels can't be float values, because there are no half pixels. Thus you have to think whether you really need a float value for a pixel change value.

Categories

Resources