I want 'rock' to be able to automatically move to the left when running the program but nothing happens, from my understanding I have made it so that the rocks x position moves by 3 every iteration
import pygame
p = pygame.display.set_mode((900, 600))
pygame.display.set_caption("First game")
FPS = 60
WHITE = 255, 255, 255
RED = 255, 0, 0
GREEN = 0, 255, 0
BLUE = 0, 0, 255
DBLUE = 57, 64, 90
board1 = pygame.image.load("board.png")
board2 = pygame.image.load("board.png")
rock = pygame.image.load("rock.png")
def draw_window(board1_move, board2_move, rock_scaled):
p.fill(DBLUE)
board1_scaled = pygame.transform.rotate(pygame.transform.scale(board1, (55, 40)), 40)
board2_scaled = pygame.transform.rotate(pygame.transform.scale(board2, (55, 40)), 40)
rock_scaled = pygame.transform.rotate(pygame.transform.scale(rock, (55, 40)), 0)
p.blit(board1_scaled, (board1_move.x, board1_move.y))
p.blit(board2_scaled, (board2_move.x, board2_move.y))
p.blit(rock_scaled, (400, 250))
pygame.display.update()
keypress = pygame.key.get_pressed()
def board1_move_func(keypress, board1_move):
if keypress[pygame.K_w] and board1_move.y > 0:
board1_move.y -= 3
if keypress[pygame.K_s] and board1_move.y < 530:
board1_move.y += 3
def board2_move_func(keypress, board2_move):
if keypress[pygame.K_UP] and board2_move.y > 0:
board2_move.y -= 3
if keypress[pygame.K_DOWN] and board2_move.y < 530:
board2_move.y += 3
def main():
rock_x = 450
rock_y = 250
board1_move = pygame.Rect(20, 250, 55, 40)
board2_move = pygame.Rect(805, 250, 55, 40)
rock_move = pygame.Rect(rock_x, rock_y, 55, 40)
clock = pygame.time.Clock()
run = True
while run == True:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keypress = pygame.key.get_pressed()
rock_x += 3
board1_move_func(keypress, board1_move)
board2_move_func(keypress, board2_move)
draw_window(board1_move, board2_move, rock_move)
pygame.quit()
if __name__ == "__main__":
main()
You change rock_x, but draw the object at the position stored in rock_move. rock_move is not magically tied with rock_x. You can update rock_move after changing rock_x:
rock_x += 3
rock_move.x = rock_x
However, I recommend changing rock_move instead of rock_x:
rock_move.x += 3
Related
i am kinda new to python and pygame and i'm currently working on fnf-like game in pygame, but when i was trying to make falling arrows, AttributeError: 'pygame.Surface' object has no attribute 'display' appeared. I looked in other people projects, but i couldn't find what's wrong
import pygame, sys, random
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("Python Night Funkin'")
upImg = pygame.image.load("up.png")
downImg = pygame.image.load("down.png")
rightImg = pygame.image.load("right.png")
leftImg = pygame.image.load("left.png")
test = 0
x = 0
y = 0
arrX = 0
arrY = 0
height = 500
width = 500
arrHeight = 20
arrWidth = 20
arrSpeed = 5
def drawWindow():
win.fill((0,0,0))
pygame.draw.rect(win, (20,20,20), (70,370, 60, 60))
pygame.draw.rect(win, (20,20,20), (170,370, 60, 60))
pygame.draw.rect(win, (20,20,20), (270,370, 60, 60))
pygame.draw.rect(win, (20,20,20), (370,370, 60, 60))
pygame.draw.rect(win, (255, 0, 0), (x-20, y-20, 40, 40))
if arrX == 100:
win.display.blit(rightImg, (arrX-20,arrY-20))
if arrX == 200:
win.display.blit(downImg, (arrX-20,arrY-20))
if arrX == 300:
win.display.blit(upImg, (arrX-20,arrY-20))
if arrX == 400:
win.display.blit(leftImg, (arrX-20,arrY-20))
pygame.display.update()
clock = pygame.time.Clock();
run = True
arrowX = [100, 200, 300, 400]
Arrow = []
while run:
clock.tick(30);
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
win is a Surface and has no display attribute, but a blit function.
So instead of
win.display.blit(rightImg, (arrX-20,arrY-20))
simply use
win.blit(rightImg, (arrX-20,arrY-20))
etc.
I want to change different variables in my code while it's running, for example changing the variable speed = 1 to speed = 2 using the CMD. The variable I want to change is decided on the person running the code through an input(). They type in speed, and the variable speed changes to another number, also chosen by the person running the code through another input().
To change the values, you should need to-
.
Press Enter, to pause the game
Select the CMD window
Type 'setvar', then Enter
Type 'speed', then Enter
Type '5', then Enter
.
I want this to change speed = 1 to speed = 5, but I also want to be able to do this with every variable, being able to change the score variable, or anything else. So far, I have made it so you input the variable you want to change, and what you want to change it to. The code is slightly messy, but readable.
So far I have tried
exec("%s = %s" % (var,setTo))
and
exec(f"{var} = '{setTo}'")
But it hasn't worked, although speed = 5 does.
The code I want help with is near the bottom. The whole code is...
def RUN():
import pygame
import time
import random
import msvcrt as m
pygame.init()
dis_width = 800
dis_height = 600
score = 0
x = 375
y = 275
foodx = -100
foody = -100
speed = 1
stanima = 0
gotox = 200
gotoy = 200
gotoSet = []
goto = []
gotoSet.append(gotox)
gotoSet.append(gotoy)
goto.append(gotoSet)
people = []
numOfPeople = 1 # Number of people
foodEaten = True
foodRun = True
speedRunPlus = True
speedRunMinus = True
runningRun = True
clock = pygame.time.Clock()
font = pygame.font.SysFont("Arial", 18)
dis = pygame.display.set_mode((dis_width, dis_height)) #screen stuff
pygame.display.set_caption('Game')
game_over = False
running = True
lightBlue = (50, 153, 213) #colors
red = (255, 100, 0)
yellow = (255, 255, 102)
blue = (70, 70, 255)
green1 = (0, 255, 0)
green2 = (0, 255, 25)
green3 = (0, 255, 50)
green4 = (0, 255, 75)
green5 = (0, 255, 100)
green6 = (0, 255, 125)
green7 = (0, 255, 150)
green8 = (0, 255, 175)
green9 = (0, 255, 200)
green10 = (0, 255, 225)
dis.fill(blue) #Backround
pygame.draw.rect(dis, lightBlue, [50, 50, 700, 500]) #Inner rectangle
def update_fps():
fps = str(int(clock.get_fps()))
fps = str("FPS: "+fps)
fps_text = font.render(fps, 1, pygame.Color("coral"))
return fps_text
def speedRender():
speed_str = str(speed)
speed_sentance = str("Average Speed: "+speed_str)
speed_text = font.render(speed_sentance, 1, pygame.Color("coral"))
return speed_text
def player(x, y, color, stamina): #Rectangle player
player = pygame.draw.rect(dis, color, [x, y, 25, 25])
if stanima == 1:
pygame.draw.rect(dis, red, [x+11, y+11, 3, 3])
elif stanima == 2:
pygame.draw.rect(dis, red, [x+10, y+10, 5, 5])
elif stanima == 3:
pygame.draw.rect(dis, red, [x+9, y+9, 7, 7])
elif stanima == 4:
pygame.draw.rect(dis, red, [x+8, y+8, 9, 9])
elif stanima == 5:
pygame.draw.rect(dis, red, [x+7, y+7, 11, 11])
elif stanima == 6:
pygame.draw.rect(dis, red, [x+6, y+6, 13, 13])
elif stanima == 7:
pygame.draw.rect(dis, red, [x+5, y+5, 15, 15])
elif stanima == 8:
pygame.draw.rect(dis, red, [x+4, y+4, 17, 17])
elif stanima == 9:
pygame.draw.rect(dis, red, [x+3, y+3, 19, 19])
def food(foodx, foody): #Rectangle food
food = pygame.draw.rect(dis, yellow, [foodx, foody, 10, 10])
for num1 in range(numOfPeople):
x = random.randint(300,400)
y = random.randint(300,400)
person = []
person.append(x)
person.append(y)
person.append(1)
person.append(goto)
person.append(score)
people.append(person)
while not game_over:
while running:
dis.fill(blue)
pygame.draw.rect(dis, lightBlue, [50, 50, 700, 500])
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
game_over = True
for num2 in range(numOfPeople):
x = people[0][0]
y = people[0][1]
#speed = people[0][2]
goto = people[0][3]
score = people[0][4]
del people[0]
if x+12 > foodx+5: # "AI"
x -= speed
elif x+12 < foodx+5:
x += speed
if y+12 > foody+5: # "AI"
y -= speed
elif y+12 < foody+5:
y += speed
if x > 725: #Creates Borders
x = 725
game_over = True
elif x < 50:
x = 50
game_over = True
if y > 525:
y = 525
game_over = True
elif y < 50:
y = 50
game_over = True
if x >= foodx-24 and x <= foodx+10:
if y >= foody-24 and y <= foody+10:
score += 1
foodEaten = True
#print("Score:", score)
person = []
person.append(x)
person.append(y)
person.append(1)
person.append(goto)
person.append(score)
people.append(person)
playerColor = green1
player(x, y, playerColor, stanima) #Creating Player
if foodEaten == True:
foodx = random.randint(90, 700)
foody = random.randint(90, 500)
stanima += 1
foodEaten = False
food(foodx, foody) #Creating Food
pressed = pygame.key.get_pressed() #Key Movement
if pressed[pygame.K_RCTRL]:
dis.blit(update_fps(), (10,0))
dis.blit(speedRender(), (100,0))
if pressed[pygame.K_SPACE]:
RUN()
if pressed[pygame.K_ESCAPE]:
running = False
game_over = True
if pressed[pygame.K_RSHIFT]:
if foodRun == True:
foodx = random.randint(90, 700)
foody = random.randint(90, 500)
stanima += 1
foodEaten = False
foodRun = False
else:
foodRun = True
if pressed[pygame.K_RETURN]:
if runningRun == True:
running = False
runningRun = False
print(" ")
else:
runningRun = True
if pressed[pygame.K_MINUS]:
if speedRunMinus == True:
speed -= 0.1
speedRunMinus = False
else:
speedRunMinus = True
if pressed[pygame.K_EQUALS]:
if speedRunPlus == True:
speed += 0.1
speedRunPlus = False
else:
speedRunPlus = True
pygame.display.flip() #updating screen
clock.tick()
if not game_over:
command = input(" :")
commandSplit = command.split(".")
#try:
if commandSplit[0] == "return":
var = input(" :")
if var in dir():
print(" :", eval(var), sep='')
else:
print(" :Variable Not Found")
elif commandSplit[0] == "run":
if commandSplit[1] == "exit":
running = False
game_over = True
elif commandSplit[0] == "setvar":
var = input(" :")
print("Var value now:", eval(var), sep='')
if var in locals():
setTo = input("Change variable to:") #Code I need help with -----------------------
print("Var value after:", eval(var), sep='')
else:
print(" :Invalid Syntax Error")
'''
except Exception:
print(" :Invalid Syntax Error")
pass
'''#to implement later
RUN()
This question already has an answer here:
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)
Closed 1 year ago.
So I'm trying to check im my bird images are touching my could images and if they are to print print('Collided1!').
My Issue is that print('Collided1!') goes off no matter what and is not checking whether the images are touching. colliderect was the solution I found online but I don't seem to know how it works because this is not working.
Do You Know how to fix this? and check whether my images are touching or not?
from random import randint
import pygame, sys
import random
import time
pygame.init()
pygame.display.set_caption('Lokaverkefni')
DISPLAYSURF = pygame.display.set_mode((1224, 724))
fpsClock = pygame.time.Clock()
FPS = 60
a = 1
b = 1
c = 15
x = 100
y = 480
start = 0
score = 0
landX = 1205
totalScore = 0
level = 'low'
directionForBird = 'none'
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BASICFONT = pygame.font.Font('freesansbold.ttf', 30)
background_resized = pygame.image.load('sky.jpg')
background = pygame.transform.scale(background_resized, (1224, 724))
bird1 = pygame.image.load('bird1.png')
bird1_resized = pygame.transform.scale(bird1, (170, 150))
bird1Surface = bird1_resized.get_rect()
bird2 = pygame.image.load('bird2.png')
bird2_resized = pygame.transform.scale(bird2, (170, 150))
bird2Surface = bird2_resized.get_rect()
cloudsList = ['cloud1.png', 'cloud2.png', 'cloud3.png', 'cloud4.png']
clouds = random.choice(cloudsList)
cloud = pygame.image.load(clouds)
cloud_resized = pygame.transform.scale(cloud, (352, 352))
cloudSurface = cloud_resized.get_rect()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if level == 'low':
if (event.key == K_SPACE ):
directionForBird = 'up'
level = 'high'
FPS += 2
c += 1
if directionForBird == 'up':
y -= 10
if y == 10:
directionForBird = 'down'
if directionForBird == 'down':
y += 10
if y == 480:
directionForBird = 'none'
if a == 1:
DISPLAYSURF.blit(background, (0, 0))
DISPLAYSURF.blit(bird1_resized, (x, y))
DISPLAYSURF.blit(cloud_resized, (landX, 300))
b += 1
if b == c:
a += 1
if a == 2:
DISPLAYSURF.blit(background, (0, 0))
DISPLAYSURF.blit(bird2_resized, (x, y))
DISPLAYSURF.blit(cloud_resized, (landX, 300))
b -= 1
if b == 1:
a -= 1
start += 1
if start == 100:
start -= 1
directionForLand = 'left'
if directionForLand == 'left':
landX -= 15
if landX == -550:
landX = 1205
level = 'low'
clouds = random.choice(cloudsList)
cloud = pygame.image.load(clouds)
cloud_resized = pygame.transform.scale(cloud, (352, 352))
score += 1
if score == 30:
score = 0
totalScore += 1
scoreText = BASICFONT.render('Stig : %s' % (totalScore), True, (BLACK))
scoreRect = scoreText.get_rect()
scoreRect.topleft = (1070, 10)
DISPLAYSURF.blit(scoreText, scoreRect)
# This is Supossed to Be what checks if the bird images
# colide with the cloud images
if bird1Surface.colliderect(cloudSurface):
print('Collided1!')
if bird2Surface.colliderect(cloudSurface):
print('Collided1!')
pygame.display.update()
fpsClock.tick(FPS)
bird1Surface, bird2Surface and cloudSurface always have an upper left of (0,0), so they are always on top of each other.. You don't change the rectangles when you move the birds. You need to track the bird x,y and the cloud x,y, and construct new rectangles with the current x,y and the known width and height before you do the collision check.
I am a beginner programmer trying to build tetris in Python using Pygame. The problem is that I am unable to make my test shape fall. Only go up. When I attempt to make the shape fall, the program gives me an
IndexError: list assignment index out of range
I was wondering if I could please get help? The code is listed as follows.
import pygame, sys
from pygame.locals import *
import base64
from TetrisFont import Tetrisfont
from RGBTitle import RGBTitle
from TetrisLogo import logo
from BSOD import Cancer
from BSODFont import Death
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
DOWN = pygame.USEREVENT
class Game:
def __init__(self):
self.running = True
def run(self):
# The main gameplay loop
self.load_assets()
self.initialize_game()
clock = pygame.time.Clock()
while self.running:
self.handle_events()
self.update(clock.tick())
self.draw()
def handle_events(self):
for event in pygame.event.get():
if event.type == DOWN:
for ix, x in enumerate(self.grid):
for iy, y in enumerate(self.grid[0]):
print (y, end='')
print('') # create a new line
for x in range(len(self.grid)):
for y in range(len(self.grid[0])):
if self.grid[x][y] == True:
self.grid[x][y]= False
self.grid[x][y+1] = True
# print(x, y+1)
if event.type == QUIT:
self.running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
def draw(self):
# Handle game rendering
self.windowSurface.fill(BLACK)
pygame.draw.rect(self.windowSurface,WHITE,(550,0, 250, 600), 2)
for x in range(0,600,50):
pygame.draw.line(self.windowSurface,WHITE,(1,x),(550,x), 2)
pygame.draw.line(self.windowSurface,WHITE,(x,1),(x,550), 2)
self.title.render(self.windowSurface)
for x in range(len(self.grid)):
for y in range(len(self.grid[0])):
if self.grid[x][y] == True:
# draw a block in the right place
# render_rect(X * 50, Y * 50, )
pygame.draw.rect(self.windowSurface, RED, (x*50+2, y*50+2, 48, 48))
pygame.display.update()
def update(self, deltaTime):
pass
def load_assets(self):
# Load assets needed for the game. This should only be called once.
pygame.init()
pygame.time.set_timer(DOWN, 500)
with open('Tetrisfont.ttf', 'wb') as imagef:
imagef.write(base64.b64decode(Tetrisfont))
self.Font = pygame.font.Font('Tetrisfont.ttf', 48)
with open('Logo.png', 'wb') as imagef:
imagef.write(base64.b64decode(logo))
with open('BSODFont.ttf', 'wb') as imagef:
imagef.write(base64.b64decode(Death))
self.BSODFont = pygame.font.Font('BSODFont.ttf', 48)
with open('BSOD.png', 'wb') as imagef:
imagef.write(base64.b64decode(Cancer))
self.windowSurface = pygame.display.set_mode((800, 600), 0, 32)
pygame.display.set_caption('Tetris')
self.a = pygame.image.load('Logo.png')
pygame.display.set_icon(self.a)
def initialize_game(self):
# Set up the board for play. This can happen multiple times to restart the game.
self.score = 0
self.title = RGBTitle(self.Font, "Tetris", (200, 550), [WHITE, RED, GREEN, BLUE, GREEN, RED])
grid_width = 11
grid_height = 11
self.grid = [[0 for x in range(grid_width)] for y in range(grid_height)]
# self.grid = []
# for x in range(11):
# self.grid.append([False] * 11)
self.grid[5][5] = True
# print(self.grid)
# print(len(self.grid))
# print(len(self.grid[0]))
if __name__ == '__main__':
g = Game()
g.run()
Thank you.
The error is caused, by self.grid[x][y+1] in
for x in range(len(self.grid)):
for y in range(len(self.grid[0])):
if self.grid[x][y] == True:
self.grid[x][y]= False
self.grid[x][y+1] = True
If y is equal self.grid[0]-1, then y+1 is out of range.
Add an additional condition if y+1 < len(self.grid[0]). Furthermore, I recommend to iterate through the grid in reversed() order from the bottom to the top:
for x in range(len(self.grid)):
for y in reversed(range(len(self.grid[0]))):
if self.grid[x][y] == True:
self.grid[x][y] = False
if y+1 < len(self.grid[0]):
self.grid[x][y+1] = True
Trying the following code with python 2.7. Basically its a circle that hangs from a bar and an apple that you can hit with an impulse by pressing the spacebar. There is also a square.
import time
import pygame
import pymunk
import pymunk.pygame_util
import sys
pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
space = pymunk.Space()
space.gravity = 0, -1000
ball_body = pymunk.Body(100, 100)
ball_body.position = 400.0, 300.0
ball_body.angular_velocity = 10.0
ball_shape = pymunk.Circle(ball_body, 15)
ball_shape.friction = 0.5
ball_shape.elasticity = 0.9
ball_shape.color = (0,0,0,0)
space.add(ball_body, ball_shape)
static_lines = [pymunk.Segment(space.static_body, (20,20), (780,20), 2),
pymunk.Segment(space.static_body, (20,580), (780,580), 2),
pymunk.Segment(space.static_body, (20,20), (20,580), 2),
pymunk.Segment(space.static_body, (780,20), (780,580), 2)]
for static_line in static_lines:
static_line.color = (255,255,255)
static_line.elasticity = 0.9
static_line.friction = 1
space.add(static_lines)
i = 0
prev_body = pymunk.Body(10, 10000)
prev_body.position = (300, 580)
chain_fix_point = pymunk.Body()
chain_fix_point.position = (300, prev_body.position[1])
# Another
i = 0
prev_body = pymunk.Body(10, 10000)
prev_body.position = (600, 580)
chain_fix_point = pymunk.Body()
chain_fix_point.position = (600, prev_body.position[1])
while i < 20:
# rotation_center_body = pymunk.Body()
# rotation_center_body.position = (400, prev_body.position[1] - 20)
body = pymunk.Body(1, 1)
body.position = (600, prev_body.position[1] - 10)
line = pymunk.Circle(body, 5)
line.elasticity = 0
line.friction = 1
if i == 0:
rotation_center_joint = pymunk.PinJoint(body, chain_fix_point)
else:
rotation_center_joint = pymunk.PinJoint(body, prev_body)
space.add(line, body, rotation_center_joint)
prev_body = body
i += 1
blob_body = pymunk.Body(5, 1)
blob_body.position = prev_body.position[0], prev_body.position[1] - 40
blob_shape = pymunk.Circle(blob_body, 20)
rotation_center_joint = pymunk.SlideJoint(blob_body, prev_body,(0,0),(0,0),0,40)
space.add(blob_body, blob_shape, rotation_center_joint)
appleimg = pygame.image.load('apple.png')
box_body = pymunk.Body(10,10000)
box_body.position = 600, 300
box_vertices = [(570, 270),(570, 330),(630,330),(630,270)]
box_shape = pymunk.Poly(box_body, box_vertices, offset=(0, 0), radius=1).create_box(box_body, size = (60,60))
box_shape.friction = 0.5
box_shape.elasticity = 0.9
box_shape.color = (255,0,0)
space.add(box_body, box_shape)
def main():
running = True
angle = 0;
while running == True:
print "game loop"
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
ball_body.apply_impulse(j = (100000, 100000))
screen.fill((0,0,0))
pymunk.pygame_util.draw(screen, space)
if ball_body.angle != 0:
angle += ball_body.angular_velocity
img = pygame.transform.rotate(appleimg, angle)
screen.blit(img, (ball_body.position[0] - 20, 580 - ball_body.position[1]))
pygame.display.flip()
clock.tick(60)
space.step(1/60)
pygame.quit()
quit()
main()
The gameloop runs but the position does not update.
This code worked pretty well for python 3.5. But when I switched to 2.7, its failing.
The problem is that in python 2.x you get 0 when you divide 1 by 60 when you call the step function, since / is doing integer division in 2.x.
You can fix this problem either by importing the python 3 division with
from __future__ import division
Or you can divide by a float instead 1/60.0
Check this question for mor info: In Python 2, what is the difference between '/' and '//' when used for division?