在黑色畫布裡移動白色小方塊,小遊戲

园糯發表於2024-11-16
import sys
import pygame

WIDTH=800
HEIGHT=600
SIZE=WIDTH,HEIGHT
pygame.init()
screen=pygame.display.set_mode(SIZE)

class Player:
	def __init__(self):
		self.surf = pygame.Surface((50,50))
		self.surf.fill("white")
		self.rect=self.surf.get_rect()
		# self.rect = self.surf.get_rect()

	def move(self):
		pressed_key = pygame.key.get_pressed()
		if pressed_key[pygame.K_DOWN]:
			self.rect.move_ip(0, 10)
		# player_rect.top +=10
		if pressed_key[pygame.K_UP]:
			self.rect.move_ip(0, -10)
		# player_rect.top -=10
		if pressed_key[pygame.K_RIGHT]:
			self.rect.move_ip(10, 0)
		# player_rect.right +=10
		if pressed_key[pygame.K_LEFT]:
			self.rect.move_ip(-10, 0)
		# player_rect.left -=10
		# 限制玩家在螢幕中移動
		if self.rect.right >= WIDTH:
			self.rect.right = WIDTH
		if self.rect.left <= 0:
			self.rect.left = 0
		if self.rect.bottom >= HEIGHT:
			self.rect.bottom = HEIGHT
		if self.rect.top <= 0:
			self.rect.top = 0

	def draw(self):
		screen.blit(self.surf,self.rect)

player=Player()
clock=pygame.time.Clock()
while True:
	for event in pygame.event.get():
		if event.type==pygame.QUIT:
			pygame.quit()
			sys.exit()
	# if event.type==pygame.KEYDOWN and event.key==pygame.K_DOWN:
	# player_rect.top +=10
	# move
	player.move()
	# draw
	screen.fill("black")
	player.draw()
	pygame.display.flip()
	clock.tick(60)

tkinter選項

#coding: utf-8
from tkinter import *
from tkinter import ttk
import tkinter.font as tkFont
root=Tk()
root.geometry('400x400')
root.title('20181201')
ft=tkFont.Font(family='宋體',size=12)
e1=StringVar()
fm1=Frame(root)
fm1.pack()
fm2=Frame(root)
fm2.pack()
Label(fm1,text='進貨方式',font=ft).pack()
comb1=ttk.Combobox(fm1,font=ft,textvariable=e1,width=14,state='readonly')
comb1.pack()
comb1['values']=('廠家進貨','經銷商進貨','自己生產')
comb1.current(0)
Label(fm2,text='文具型別',font=ft).pack()
listb1=Listbox(fm2,font=('宋體',12),selectmode=SINGLE,width=12,height=5,exportselection=False)
listb1.pack()
for item in ['鉛筆','毛筆','鋼筆','圓珠筆','中性筆']:
	listb1.insert(END,item)
listb1.select_set(0)#預設選第一個
mainloop()

相關文章