moving a rectangle when cursor is in an area - python

this script is supposed to move 2 rectangles whenever the cursor gets in one of them, i see them flashing sometimes, but they're not moving, they move right 30 and then they go back to 0
import pygame
pygame.init()
screen= pygame.display.set_mode((700,500))
while True:
ex = pygame.Rect(30,30,60,60)
exz= pygame.Rect(0,30,30,60)
for event in pygame.event.get():
if event.type == 256:
pygame.quit()
if event.type == 1024:
cursor_pos=pygame.mouse.get_pos()
print(cursor_pos[1].__str__()+"=y")
print(cursor_pos[0].__str__()+"=x")
print(exz.x.__str__()+"exz.x"+", "+exz.y.__str__()+"exz.y")
if(cursor_pos[0]+cursor_pos[1]) < ((exz.x+30)+exz.y*3) and (cursor_pos[0]+cursor_pos[1])>30 and cursor_pos[1]<=90 and cursor_pos[1]>=30:
exz.right+=30
ex.right+=30
print("exz:"+exz.x.__str__()+", "+exz.y.__str__())
print("exs:"+ex.x.__str__()+", "+ex.y.__str__())
pygame.display.set_caption("Cursor is in area")
else:
pygame.display.set_caption("Cursor is not in area")
pygame.draw.rect(screen,(255,0,0),ex)
pygame.draw.rect(screen,(0,255,0),exz)
pygame.display.update()
screen.fill((50,50,50))

your last 5 lines should be inside the while loop block, to update the screen.

I don't know if this is what you wanted, but:
import pygame
pygame.init()
screen= pygame.display.set_mode((700,500))
ex = pygame.Rect(30,30,60,60) #this has to be outside of the while loop, otherwise the position resets every time
exz= pygame.Rect(0,30,30,60) #same of ex
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: #I don't know what 256 and 1024 means, but this is way better
pygame.quit()
#This has to be outside the for event loop
cursor_pos=pygame.mouse.get_pos() #you shall update the cursor every fps, and not by a if statement
if ex.collidepoint(cursor_pos) or exz.collidepoint(cursor_pos): #Idk what you did here, but i think this is just way simpler
exz.x+=30
ex.x+=30
pygame.display.set_caption("Cursor is in area")
else:
pygame.display.set_caption("Cursor is not in area")
pygame.draw.rect(screen,(255,0,0),ex) #the draws methods were outside the while loop
pygame.draw.rect(screen,(0,255,0),exz)
pygame.display.update()
screen.fill((50,50,50))
This code moves the two rects whenever the mouse gets inside of one of them.
Your glitching is because ex and exz are inside the while loop, so you were re-setting the position every time. I removed the prints but those were not a problem

Related

How to move pieces in chess program

as a premice i would like to say that i'm totally new to programming and i'm not a computer science student so i'm sorry if my code makes you cringe, i recently had some python classes and enjoyed them so i wanted to deepen a little so i figured that a simple chess game would be fun to do.
As you can imagine i am using pygame.
As for now i "drew" a chessboard and i blitted the pieces in place, my idea is that i would get the coordinates of every click, if the coordinates are the same (or in range) of the blitted image the variable would update with the second click, how can i make it so that the system recognizes a "first" and "second" click.
import pygame as pg
import sys
pg.init()
schermo = pg.display.set_mode((640,540))
def coordinate():
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if event.type == pg.MOUSEBUTTONUP:
mx1, my1 = pg.mouse.get_pos()
print (mx1,my1)
return mx1,my1
pg.display.update()
this is how i get the coords
ctorrensx = (53,53)
[...omissis]
def move():
result = coordinate()
global ctorrensx
if result == ctorrensx:
ctorrensx = (200,200)
this was my first idea for the moving function, ctorrensx is an example i wanted to try on, they are the coords of the left black rook, once i would click on it i wanted it to move to the coords (200,200) but it's not happening.
this is my first time using stack overflow so i hope that i didn't create too much confusion on my question.
thank you all.
You can set a flag to determine whether the click is a first or second.
For example:
firstClick = True
while True:
for event in pg.event.get():
if event.type == pg.MOUSEBUTTONDOWN:
if(firstClick):
firstClick = False
#Code for first click
else:
#code for second click
firstClick = True #resets the flag

Interaction with AI in PyGame [duplicate]

I am currently developing a simple Tower of Hanoi animation in Pygame, that should show the correct solution of Tower of Hanoi, moving one piece per second.
However, in my hanoi solving algorithm, I'm trying to update the display and use pygame.time.wait() after each movement; and instead of updating one movement and waiting one second, the program waits the total number of movements amount of seconds and then displays the tower with all the movements done at once.
What I would like to know is if I am using the wait function wrongly or if there is any other useful function in this situation that I am missing.
Here's the code:
def hanoi(n, origin, destination, aux):
# solves the game with n pieces
if n == 1:
positions[0] = destination
# updates and waits
printBackground()
printPieces(positions)
pg.time.wait(1000)
else:
hanoi(n-1, origin, aux, destination)
positions[n-1] = destination
#updates and waits
printBackground()
printPieces(positions)
pg.time.wait(1000)
hanoi(n-1, aux, destination, origin)
and the loop:
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if running:
hanoi(numPieces, 0, 2, 1)
running = False
Thank you!
You need to seperate your algorithm from the drawing aspect of your code.
A simple way to update your code would be to use a coroutine that, at every step of your recursive hanoi function, gives the control back to the main loop, which in turn draws the screen, and gives control back to the hanoi coroutine every second.
Here's a simplified example that just counts down:
#-*- coding-utf8 -*-
import pygame
import pygame.freetype
pygame.init()
screen = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
font = pygame.freetype.SysFont(None, 30)
def hanoi(num):
# We calculated something and want to print it
# So we give control back to the main loop
yield num
# We go to the next step of the recursive algorithm
yield from hanoi(num-1) #
steps = hanoi(1000)
ticks = None
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
exit()
# step every second only
if not ticks or pygame.time.get_ticks() - ticks >= 1000:
ticks = pygame.time.get_ticks()
screen.fill((200, 200, 200))
# the value from the next step of the coroutine
value = str(next(steps))
# render stuff onto the screen
font.render_to(screen, (100, 100), value)
pygame.display.flip()
clock.tick(60)
In your code, you should replace
# updates and waits
printBackground()
printPieces(positions)
pg.time.wait(1000)
with yield positions to give control back to the main loop and
hanoi(n-1, aux, destination, origin)
with
yield from hanoi(n-1, aux, destination, origin)
to keep the coroutine running and call
...
screen.fill((200, 200, 200))
positions = next(steps)
printBackground()
printPieces(positions)
...
inside the if in the main loop.
(If the algorithm finish, it will raise a StopIterationException which you probably want to catch).

How to fix counter flashing

I am creating a very simple game to try and ease into things, but ran into a problem pretty early on.
I have tried re-positioning where the timer is updated, but it just keeps flashing/flickering.
def update_timer():
global timer, timerrect
# make timer text
displaytext(f"{counter}", 50, 50)
# dispay timer
pygame.display.flip()
return timer, timerrect
def displaytext(text, a, b):
x = font.render(text, True, BLACK, WHITE)
xrect = x.get_rect()
xrect.center = (a, b)
screen.blit(x, xrect)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.USEREVENT:
counter-=1
update_timer()
# not-so-elegant displaying of timer
try:
screen.blit(timer, timerrect)
pygame.display.flip()
except:
pygame.display.flip()
# some more code here
# draw all sprites
all_sprites.draw(screen)
# fps
clock.tick(60)
# update screen
pygame.display.flip()
As I said, the timer is constantly flickering. I'd like for it to not induce seizures /s.
Most of the time, when something is flickering in Pygame, there is one cause: You are calling pygame.display.flip() more than once per frame.
You should remove all calls to pygame.display.flip() except the one in your main loop (the one you have commented # update screen). This means that the display is updated only once per frame.
When there are additional flips the screen updates on top of itself, and some frames may not end up with the timer visible.
Also, it looks like your update_timer function would work, if you remove the extra flip. Then you would not need the "not-so-elegant" code that you probably used to try debugging this problem.

Pygame buttons not working

I have created some sort of menu navigation system in my game. All the screens are blitted in. The "Play" and "Quit" and "Controls" button works just fine but when I try to press menu from the controls screen, nothing happens. On the controls screen, you can faintly see the first menu screen from before. That might be the problem. I think that as the return to menu button is over the previous controls page button, it somehow is pressing the controls button from before. The button and menu segment of my code will be pasted here and the full thing will be pasted in a pastebin.
def text_to_button(msg,color,buttonx,buttony,buttonwidth,buttonheight,size = "small"):
textSurf, textRect = text_objects(msg,color,size)
textRect.center = ((buttonx + buttonwidth/2)), buttony+(buttonheight/2)
gameDisplay.blit(textSurf, textRect)
def button(text,x,y,width,height,inactive_color,active_color,size = "small",action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
#print(click)
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color,(x,y,width,height))
if click[0] == 1 and action != None:
if action == "quit":
pygame.quit()
quit()
if action == "controls":
game_controls()
if action == "play":
gameLoop()
if action == "main":
game_intro()
else:
pygame.draw.rect(gameDisplay, inactive_color,(x,y,width,height))
text_to_button(text,black,x,y,width,height,size)
def game_controls():
gcont = True
while gcont:
gameDisplay.blit(cont,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
button("Play",150,500,100,50,white,gray,"small",action = "play")
button("Main Menu",320,500,150,50,white,gray,"tiny", action = "main")
button("Quit",550,500,100,50,white,gray,"small", action = "quit")
pygame.display.update()
clock.tick(15)
def game_intro():
intro = True
while intro:
gameDisplay.blit(imggg,(0,0))
button("Play",150,500,100,50,white,gray,"small",action = "play")
button("ControLs",320,500,150,50,white,gray,"tiny", action = "controls")
button("Quit",550,500,100,50,white,gray,"small", action = "quit")
pygame.display.update()
clock.tick(15)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
intro = False
Full Code: https://pastebin.com/jrd82gkJ
You will have very hard time to debug your code in order to achieve the behavior you want for one simple reason:
The logic you use to switch between different screens providing different functionality is causing much trouble you can't directly see if you only run the game.
So you think: "oh ... how come the button doesn't work, there must be an issue with the button".
You are probably not aware of the fact that using functions having own while loops you go deeper and deeper into recursive calls with increasing recursion depth with each switch from one view to another - it is not how pygame is thought to be programmed.
I suggest you add some print() commands into your code to see in the console output that the code doesn't really do what you expect even if it appears to be OK at the first glance because it works.
Then I suggest you REWRITE your entire code so that you have one main while notGameExit: loop, and don't use any other looping in the helper functions. If you want use looping in your helper functions at least don't call from the helper functions another functions with own loops (and so on), but RETURN from them with an explicit return to avoid recursion.
If you leave the in the main loop called function with return your main loop will continue running and depending on some switches you can display in it different things on the screen and react differently to user actions.
Maybe looking at a minimal working pygame script showing "action" without usage of a loop you will gain better understanding and some deep "enlightenment" about how pygame works and then start a total rewrite of your game using another approach as this one you have used in the current code? Then come back with what you have achieved if you have further questions, but you won't probably have any, because it would be much easier to debug it yourself if the code will become more straightforward.
import pygame
pygame.init() # start PyGame (necessary because 'import pygame' doesn't start PyGame)
winDisplay = pygame.display.set_mode((1024, 768)) # set PyGame window size to 1024x768 pixel
pygame.display.set_caption("Minimal PyGame Test Script")
# Time in pygame is measured in milliseconds (1/1000 seconds) (defined by TIMER_RESOLUTION constant):
pygame.TIMER_RESOLUTION = 1000 # assure 1000 explicit, don't relay on default value
colorWhite = (255, 255, 255) # RGB color in Pygame format (valueRed=255, valueGreen=255, valueBlue=255)
colorRed = (255, 0, 0)
colorGreen = ( 0, 255, 0)
colorBlue = ( 0, 0, 255)
winDisplay.fill(colorWhite)
pygame.display.update()
pygame.time.wait(3000) # show the Pygame window for 3 seconds
winDisplay.fill(colorRed)
pygame.display.update()
pygame.time.wait(3000) # show the Pygame window for 3 seconds
winDisplay.fill(colorGreen)
pygame.display.update()
pygame.time.wait(3000) # show the Pygame window for 3 seconds
winDisplay.fill(colorBlue)
pygame.display.update()
pygame.time.wait(3000) # show the Pygame window for 3 seconds
winDisplay.fill(colorWhite)
pygame.display.update()
pygame.time.wait(3000) # show the Pygame window for 3 seconds

Pygame text input not on screen

I need to kb input onto a pygame screen
at the moment it appears on the idle shell
any advice would be appreciated.
This code is extracted from a larger program
mostly screen based but i need to input some
data (numeric) from the kb at times
import sys
import pygame
from pygame.locals import *
pygame.init()
N= ''
screen = pygame.display.set_mode((600,600))
font= pygame.font.Font(None,40)
screen.fill((255,255,255))
pygame.display.flip
pygame.display.update()
def score(C,y):
SetWnd = font.render( C,True,(0,0,255))
screen.blit(SetWnd, (15, 100+y))
pygame.display.update()
def start():
while True:
name=''
for evt in pygame.event.get():
if evt.type == KEYDOWN:
if evt.unicode.isalnum(): # unicode
name+=evt.unicode
print name,
elif evt.key == K_BACKSPACE:
name = name[:-1]
print name,
elif evt.key == K_RETURN:
return N
elif evt.type == QUIT:
pygame.quit()
sys.exit()
def Pchange(c,y):
block = font.render(N, True, (0,0,0))
rect = block.get_rect()
rect.move_ip(75,100 + y)
screen.blit(block,rect)
pygame.display.flip()
score('wind', 0)
score('elev',20)
N = start()
Pchange(N,0)
Pchange(N,20)
Firstly you draw the score twice, which i assume works well.
The problem lies in you start function.
You are not calling any draw or update function in your while loop.
In your event foreach, you add a digit to name, and exit the while loop when enter is pressed. Then you draw twice with Pchange, but you are the function does not use the right parameters. You have:
def Pchange(c,y):
block = font.render(N, True, (0,0,0))
you are using the global N which is ''. So to fix that, you need to change N to c.
The next problem is that the game quits right after pressing enter. Since you pasted only a part of the program, this might not be the case. If it is, make another while loop, and just wait for the ESC key to call pygame.quit() and sys.exit()

Categories

Resources