im making a basic pygame and I was wondering if anyone can help me out
so after a certain amount of time a power-up scrolls down the screen but I cant get it to work
here is the method im using:
def random_event(self):
self.force_img_r = self.force_img.get_bounding_rect()
self.rnd_x = random.randint(5,315)
self.force_img_r.x = self.rnd_x
self.force_img_r.y += 3
screen.blit(self.force_img,(self.rnd_x, self.force_img_r.y))
all its doing is the image is blinking for a split second then nothing
can anyone tell me why its not working!?
In pygame you usually have a main loop that cleans the screen on every iteration, that's probably why you only see a blink, you draw and in the next loop you clean again.
To work arround this, in pygame events should only update game state, and draw the screen on every loop according to the current state.
Some pseudocode:
# Main Loop
while True:
# Process events
# -> Update game state
# Clean screen
# Draw current state to the screen
# Update or flip display
# Keep framerate (clock.Tick())
You can call external classes/methods/functions, but you should always keep this structure at the main loop.
Related
I am trying to write a script in python that would take control of Halo 5 forge in order to automatically create and script an in-game script brain object and use the game's built-in scripting system to script the object to randomize the movement of 2 agents (will be added later) in order to procedurally generate a map, saving me time in doing tedious manual work on creating a method from scratch I have repeated many times in Halo 5 Forge.
Basically I am trying to create a class representing this script brain object in python and under the init method it is supposed to follow all of the steps in Forge to create a script brain object.
At first I tried pyautogui as it initially worked previously on Halo 5. But now it doesn't seem to work. It is a known issue that pyautogui doesn't input certain things properly on directX games and was suggested pydirectinput as an alternative.
So I did and while the mouse responds properly in the game and opens the object menu using the press() method, it doesn't actually seem to respond to leftClick() for some reason, only repositioning the mouse to the correct position but not actually clicking the object menu like I had hoped.
Here is the code:
import os
import pyautogui as pygui
import pydirectinput as pydi
import time
class forgeObjectRandomizer:
def __init__(self):
#CREATE THE SCRIPT BRAIN
time.sleep(5)
screenWidth, screenHeight = pydi.size()
pydi.moveTo(round(screenWidth / 2), round(screenHeight / 2))
time.sleep(1)
# pydi.leftClick(1749, 44)
pydi.press('o')
time.sleep(0.5)
pydi.leftClick(1554, 337) # --- Extras
time.sleep(0.5)
pydi.leftClick(1605, 266) # --- Scripting
time.sleep(0.5)
pydi.leftClick(1630, 236) # --- Script Brain
time.sleep(0.5)
pydi.press('p') # --- Properties
time.sleep(0.5)
pydi.mouseDown(1886, 231) # --- Scroll down
pydi.moveTo(1886, 453) # --- Scroll down
pydi.mouseUp()
brainRandomizer = forgeObjectRandomizer()
You can use to move mouse
import win32api,win32con
def click():
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, int(x), int(y), 15, 15)
for mouse click equivalent, but I only had problems with moving the mouse
This is kinda a blind shot since u said pyautogui was working fine before on halo 5...are you running the game as administrator? If so make sure that ur code does too, because i had same problem in a game named asda global
I've been testing the turtle library for about 3 days now. One recurring 'issue' that I've been getting is the traceback error whenever I exit my application window. The terminal displays rows of details regarding the turtle update function and it ends with:
_tkinter.TclError: can't invoke "update" command: application has been destroyed
Here's my code:
import turtle
wn = turtle.Screen()
wn.title("Game Window")
wn.bgcolor("black")
wn.setup(width=1000, height=650)
wn.tracer(0)
run = True
while run:
wn.update()
I've been trying to wrap my head around the traceback report. I'm assuming it happens because the application continuously updates the window (as you can see in the while run block). So, there is a possibility that, once I exit the window, the application is already processing the wn.update() function, and it returns an error because it did not finish its operation. If that is the case, then what should I do about the update function? If not then, please, explain to me the issue and solution. Thank you!
The problem is your loop:
while run:
wn.update()
This is the wrong way to approach Python turtle programming. I see this loop often in SO questions so there must be a book ("Programming Python Turtle by Bad Example") or tutorial somewhere teaching people the wrong way to approach turtle.
Generally, I'd suggest you avoid tracer() and update() until your program is basically working and you now need to optimize its performance. If you do use tracer(), then you should only call update() when you are finished making changes and you want the user to see the current display. Something like:
from turtle import Screen, Turtle
screen = Screen()
screen.setup(width=1000, height=650)
screen.title("Game Window")
screen.tracer(0)
turtle = Turtle()
radius = 1
while radius < 300:
turtle.circle(radius, extent=1)
radius += 0.25
screen.update() # force above to be seen
screen.mainloop()
A key point to note is that our program ends with a mainloop() call which passes control onto Tk(inter)'s event loop. That's the same event loop that receives the window close event and closes down turtle cleanly.
I trying to make a rhythm game for my final project. I'm using pygame, and I want my program to draw a shape, wait a second, and then draw another, in accordance with the music I'm playing. Is it possible for my program to wait a second between drawing each shape, but have the rest of the program still runs? (not pygame.time.delay())
I've tried http://fredericiana.com/2014/11/14/settimeout-python-delay/ which either didn't work, or I failed to implement it correctly
def spawnShapesGameOne(gameInPlay, gameInPlayOne,drawShapesOne):
if gameInPlay == True:
if drawShapesOne == True:
pygame.draw.rect(surface, GREEN,(w*.23, h*.25, w*.05,w*.05))
#Wait one second
pygame.draw.rect(surface, GREEN,(w*.73, h*.25, w*.05,w*.05))
#Wait one second
pygame.draw.rect(surface, GREEN,(w*.73, h*.65, w*.05,w*.05))
#Wait one second
pygame.draw.rect(surface, GREEN,(w*.23, h*.65, w*.05,w*.05))
you can do this non-blocking delay / schedule in general by using time module or any timer:
Note: I moved reset timer to main loop
import time
timer1sec = 0
def draw_myshape():
# your drawing code here
# main pygame loop
while True:
if time.time() - timer1sec >= 1: # if one seconds passed
draw_myshape()
timer1sec = time.time() # reset our timer
# rest of pygame code here
It's extremely painful to redraw each image everytime the screen is cleared.
import pygame
from pygame.locals import *
T = pygame.display.set_mode((500,500))
M = pygame.image.load("test.jpg")
X = 0
Y = 0
while True:
X += 1
Y += 1
# Other sprites are here which are also redrawn every time loop runs.
# Other code is here too, this is just a little part of it to help explain my problem
T.fill((0,0,0))
T.blit(M,(X,Y))
pygame.display.flip()
In the above code, I am loading the background image in M Variable, everytime I clear the screen to update the position of my sprites, I also have to redraw the background image which is causing severe FPS drops.
Anyway I can prevent the Background image from being cleared whenever I am using T.fill((0,0,0)) ?
First, try to convert the background image. Images should usually be converted with convert or convert_alpha to improve the performance.
M = pygame.image.load("test.jpg").convert()
Second, if the background image has the size of the screen you can omit the line T.fill((0,0,0)), since the background fills the screen anyway.
Third, if the background isn't scrolling and you only need to update some portions of the screen every frame, you can try to use pygame.display.update() instead of pygame.display.flip(). Pass a single rect or a list of rects to pygame.display.update to tell it which parts of the screen should be updated.
I'm not sure if these measures will improve the performance drastically. Pygame is rather slow because it still relies on software rendering.
Sidenote, use descriptive variable names instead of T, M, etc..
I have two files, one to generate a world, and another to run the main code. However, the main screen keeps crashing for no reason. I think the world gen may also be broken, but it does at least pass on valid data to the main code.
# Main loop.
while RUNNING:
# Fill the screen.
screen.fill((0,0,0))
# Event handling.
for eventa in event.get():
if eventa.type == QUIT:
RUNNING = f
screen.fill(SCREENCOLOR)
# Draw the world.
for tile in WORLD:
if tile.surface == None:
pass
else:
screen.blit(tile.surface,tile.location)
# Draw the character
screen.blit(PLAYER["image"],PLAYER["loc"])
# Pygame commands clear up.
clock.tick(FPS)
screen.flip()
This code doesn't even fill the screen with white. This may just be too much data to handle, sorry if it is.
World generator
Main code
Previous question
I'm fairly sure that you aren't inserting too many things onto the screen. I believe that the problem is far more simple. You have said screen.flip() However, a surface object has no attribute called flip. You must be confused with the function pygame.display.flip() If you use this instead, the game shall display its visual output.