this is the code:
this is the pygame output
I have not been able to create a very simple shape/sprite.
The problem is that the code you are using to draw the red rectangle is after the while loop. Your program is stalling in the while loop and never reaches the drawing line.
Try moving:
pygame.draw.rect(stage, red, (x,y,width,height))
pygame.display.update()
to before the while running: statement
or if you want it to draw the rectangle every tick include that statement directly under the while running: statement
Indent the lines:
pygame.draw.rect(stage, red, (x,y,width,height))
pygame.display.update()
four spaces to the right. This will put them inside the for loop, and so will redraw the rectangle on every iteration of the loop.
Related
I can't get clamp to work. I thought clamp is to keep a rect inside of another, however the following try doesn't put the blue ball inside the red rect. Where is the problem?
import pygame
from pygame.locals import *
pygame.init()
TV=pygame.display.set_mode((400,400))
pygame.display.set_caption("Rect")
c=pygame.draw.circle(TV,(0,100,0),(150,100),100,1)
c1=pygame.draw.circle(TV,(0,0,200),(250,250),20,1)
r=pygame.draw.rect(TV,(100,0,0),c,1)
c1.clamp(r)
pygame.display.flip()
while True:
for e in pygame.event.get():
if e.type==QUIT:
pygame.quit()
clamp returns a new Rect without changing the source Rect itself.
If you want to change the source Rect, you'll have to use clamp_ip.
But this is not enough for you, because you draw the circle directly on the screen surface, and changing the resultung Rect does nothing:
You have to create a new Surface, draw your circle onto it and get a Rect from that Surface (or any other Rect you're going to use for drawing, but it's easiest to just use the get_rect() function).
Then do whatever you want with that Rect, e.g. use clamp_ip or get a new one with clamp, then use that Rect (or the new one) together with the blit function, e.g. TV.blit(the_new_surface, the_rect).
I am using processing to draw lines on canvas. My code is quite simple:
processing.line(line.origin.x, line.origin.y, line.destination.x, line.destination.y);`
But I get pixelated lines:
How do I draw non-pixelated precise lines?
If your line code is inside your draw function it will be pixelated unless you add a background color above it.
So you can add a background color in your draw function or you could instead put your line code in the setup function. Either should remove the pixelation.
here is my code:
import pygame
import sys
pygame.init()
FPS=30
clock=pygame.time.Clock()
white=(255,255,255) #background
screen=pygame.display.set_mode((480,320)) #screen size
pygame.display.set_caption("Zombie Game!")
mainsheet=pygame.image.load("C:\Python34\MyMan.png") #load image
sheet_size=mainsheet.get_size() #get size of img
steps_right=8 #first 8 pictures is moving to the right
jump=16 #next 8 (actually 5) is jumping
steps_left=24 #next 8 is moving to the left
cell_width=int(sheet_size[0]/8) #width of the one animation
cell_height=int(sheet_size[1]/4) #height of the one animation
cell_list=[]
for y in range(0,sheet_size[1],int(cell_height)): #looping through image
for x in range(0,sheet_size[0],int(cell_width)):
surface=pygame.Surface((cell_width,cell_height))
surface.blit(mainsheet,(0,0),(x,y,cell_width,cell_height))
cell_list.append(surface) #adding images to list
cell_position=0
running=True
pygame.key.set_repeat(50, 50) #if they hold the key
while running:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_RIGHT: #if to the right..
if cell_position <steps_right-1:
cell_position+=1
else:
cell_position=0
if event.key==pygame.K_LEFT:
if cell_position<steps_left-1 and cell_position>jump:
cell_position+=1
else:
cell_position=jump+1
if event.key==pygame.K_UP:
if cell_position<12 and cell_position>7:
cell_position+=1
else:
cell_position=8
screen.fill(white)
screen.blit(cell_list[cell_position],(100,10))
clock.tick(FPS)
pygame.display.update()
I know that maybe it isn't the best way to do it but it's my first try. But there are 2 problems(at least): 1)"Character" is moving(animated) but of course he doesn't move and I dont have any idea how to do that ,if it was only simple rectangle then I would,but this is a little bit complicated ..and 2) when I try to change the background to white instead of black,my character is in the small black square but background is white..thanks for help!!!! :-))
Notes:
"C:\Python34\MyMan.png": please start this with r"" or use double slashes. Otherwise, if you put this in a directory starting with n or something, things will break.
Background color is caused (presumably) by using a non-transparent image. png supports transparency. I think there might be a flag to send to Pygame to mark it as transparent, but I don't remember what it is.
For movement, look at the line screen.blit(cell_list[cell_position],(100,10)). That's an xy coordinate pair; change those from constants to variables, and change them in the event loop.
So I am working on a tiny little 2d voxxel game in Pygame, and I want the blocks that you place to be gravity affected, but in order to do this.. I need an entity to be able to collide "with itself" (as in another one of itself). Here is the collision code:
for block in blocklist:
if block.rect.colliderect(block.rect):
exit()
exit() is just to verify when the event returns positive, but unfortunately, whenever I create one block... it detects the collision with itself and exits.
What can I do to get around this?
Thanks!
-Sam
Your example just checks the first block in your list against itself - it's always going to collide.
You want to test each block in the list, against every other block in the list:
for block in blocklist:
if any(block.rect.colliderect(x.rect) for x in blocklist if x is not block):
exit()
I would like to draw a multiple segment line on a html 5 canvas with variable width per segment.
I tried something like (pseudocode):
beginpath()
setLineWidth(1)
lineTo(0,0)
...
setLineWidth(10)
lineTo(73, 44)
stroke()
but it seems stroke only takes the last linewidth() value? Is there another way?
I tried making every segment its own line, but that turned out really really show when drawing 1000+ segments.
You have to call stroke() on each segment, not at the very end and there is no other possiblity.
Probably will have to make each segment it's own line. That or figure out how to make a custom shape with the same sort of gradient.