Python Forum
2 days learning Python 3 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: 2 days learning Python 3 (/thread-22179.html)



2 days learning Python 3 - Donny - Nov-02-2019

Hi all.
My name is Don, I have never written any code but would love to learn Python in my old/late years!
I have just joined this forum, sorry for such a newbie question to start with.

I have now been learning Python from a book and some research on the net for 2 days now.
I have been following a book called "Hunting-python" and have hit a snag.The dea was to place a circle on the screen and by using the keyboard left & right keys, move the circle around the screen.
The task set was to add up and down capability. I have # the parts I have added trying to do this but have no idea.
The book goes step by step while teaching how to write a game, but it gives a task to complete but has no answer in the book!
The code I have is:

import pygame, sys
from pygame.locals import *
pygame.init()
pygame.display.set_caption("first program")
screen = pygame.display.set_mode((640,480))
xpos = 50
#ypos = 50
clock = pygame.time.Clock()
while 1:
	clock.tick(60)
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			sys.exit()
	pressed_keys = pygame.key.get_pressed()
	if pressed_keys[K_RIGHT]:
		xpos += 1
	if pressed_keys[K_LEFT]:
		xpos -= 1
	#if pressed_keys[K_UP]:
		ypos += 1
	#if pressed_keys[K_DOWN]:
		ypos -= 1

	screen.fill((255,255,255))
	pygame.draw.circle(screen,(0,255,0),(xpos,200),20)
	pygame.draw.rect(screen,(0,255,0),(100,100,40,30))
	pygame.display.update()
Can anyone help me please as I would rather resolve this before moving on.

Thank you


RE: 2 days learning Python 3 - menator01 - Nov-02-2019

You have this backwords
if your going up should be -= 1.
The screen starts from the top and goes down.
left top corner is 0
x axises left -to right
y axtsts top to bottom
your also missing the if event.type ==
Hope it helps
#if pressed_keys[K_UP]:
        ypos += 1
    #if pressed_keys[K_DOWN]:
        ypos -= 1



RE: 2 days learning Python 3 - Donny - Nov-02-2019

Thank you for your response menator01.

I have changed that around in the code but feel I need to also change something to match the:
pygame.draw.circle(screen,(0,255,0),(xpos,200),20)

Thanks

Don


RE: 2 days learning Python 3 - menator01 - Nov-02-2019

You'r also missing if event.type ==


RE: 2 days learning Python 3 - Donny - Nov-02-2019

Ok??

The book code has one for the QUIT but has nothing else.


RE: 2 days learning Python 3 - nilamo - Nov-07-2019

(Nov-02-2019, 12:18 PM)Donny Wrote: Thank you for your response menator01.

I have changed that around in the code but feel I need to also change something to match the:
pygame.draw.circle(screen,(0,255,0),(xpos,200),20)

Thanks

Don

https://www.pygame.org/docs/ref/draw.html#pygame.draw.circle
So the docs for pygame.draw.circle say the args are:
circle(surface, color, center, radius).
You've got
circle(screen, (0,255,0), (xpos, 200), 20)
The center of the circle you're currently drawing is (xpos, 200), which means it's always 200 pixels down from the top of the window. Your supposition was correct, it should be (xpos, ypos).


RE: 2 days learning Python 3 - Donny - Nov-08-2019

Thank you to you all for the helpfull replies. I have managed to resolve this now.
ypos=50
pygame.draw.circle(screen,(0,255,0),(xpos,ypos),20)
import pygame, sys
from pygame.locals import *
pygame.init()
pygame.display.set_caption("first program")
screen = pygame.display.set_mode((640,480))
xpos = 50
ypos = 50
clock = pygame.time.Clock()
while 1:
	clock.tick(60)
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			sys.exit()
	pressed_keys = pygame.key.get_pressed()
	if pressed_keys[K_RIGHT]:
		xpos += 1
	if pressed_keys[K_LEFT]:
		xpos -= 1
	if pressed_keys[K_UP]:
		ypos -= 1
	if pressed_keys[K_DOWN]:
		ypos += 1

	screen.fill((255,255,255))
	pygame.draw.circle(screen,(0,255,0),(xpos,ypos),20)
	
	pygame.display.update()